0

我在 Excel 2019 表中有以下数据格式,名称为:testdata.xslx

在此处输入图像描述

在这种情况下,我试图读取 column2 信息并启动对 Active Directory 的调用以获取 EmailAddress,然后使用该值更新 Column3 (EmailAddress) 并使用 Powershell 脚本保存 Excel 工作表。

这是我的powershell代码:

function GetEmailFByLoginFromAD([string]$strfilePath, [string]$strSheetName) {
    # Validation for the input parameters
    if ([string]::IsNullOrEmpty($strfilePath)) {
        throw "Please provide correct file path."
    }
    
    if ([string]::IsNullOrEmpty($strSheetName)) {
        throw "Please provide correct Source sheetname."
    }
    
    
    # Open excel file and use specific sheet
    $objExcel = New-Object -ComObject "Excel.Application"
    
    $WorkBook = $objExcel.Workbooks.Open($strfilePath)
          
    $objExcel.Visible = $false
    
    if ($strSheetName -eq "") {
        $worksheet = $WorkBook.sheets.Item(1)
    }
    else {
        $worksheet = $WorkBook.sheets.Item($strSheetName)
    }      
    
    
             
    # loop for each row of the excel file
    $intRowMax = ($worksheet.UsedRange.Rows).count
    for ($intRow = 1 ; $intRow -le $intRowMax ; $intRow++) {        
        $login = $worksheet.cells.item($intRow + 1, 2).value2
        
        if (![string]::IsNullOrWhiteSpace($login)) {
        
            # Active Directory 

            # Logic to get the EmailAddress from Active Directory
    
            $Result = "EmailAddress from Active Directory" 
            
            if (![string]::IsNullOrWhiteSpace($Result)) {
                
                Write-Host "Validation of Input parameters Started..." + $Result -ForegroundColor Green

                $worksheet.cells.item($intRow + 1, 3).Value2 = $Result                

                
            }
            
        }        
    }
    $WorkBook.Save()      
    $WorkBook.close()
    $objexcel.quit()
}


GetEmailFByLoginFromAD "C:\Temp\testdata.xlsx" "Sample"

任何人都可以帮助我提供他们的指导来解决这个问题。

4

1 回答 1

0

经过进一步分析,我发现 $Result.Properties.mail 的 gettype() 值不是字符串类型。我将值转换为字符串并相应地更新了代码,然后它开始工作。

这是最新的代码:

function GetEmailFByLoginFromAD([string]$strfilePath, [string]$strSheetName) {
    # Validation for the input parameters
    if ([string]::IsNullOrEmpty($strfilePath)) {
        throw "Please provide correct file path."
    }
    
    if ([string]::IsNullOrEmpty($strSheetName)) {
        throw "Please provide correct Source sheetname."
    }
    
    
    # Open excel file and use specific sheet
    $objExcel = New-Object -ComObject "Excel.Application"
    
    $WorkBook = $objExcel.Workbooks.Open($strfilePath)
          
    $objExcel.Visible = $false
    
    if ($strSheetName -eq "") {
        $worksheet = $WorkBook.sheets.Item(1)
    }
    else {
        $worksheet = $WorkBook.sheets.Item($strSheetName)
    }      
    
    
             
    # loop for each row of the excel file
    $intRowMax = ($worksheet.UsedRange.Rows).count
    for ($intRow = 1 ; $intRow -le $intRowMax ; $intRow++) {        
        $login = $worksheet.cells.item($intRow + 1, 2).value2
        
        if (![string]::IsNullOrWhiteSpace($login)) {
        
            # Active Directory 

            # Logic to get the EmailAddress from Active Directory
    
            $Result = Object containing the user details from Active Directory

        $emailAddress = $Result.Properties.mail 
            
            if (![string]::IsNullOrWhiteSpace($Result)) {
                
                Write-Host "Validation of Input parameters Started..." + $Result -ForegroundColor Green

                $worksheet.cells.item($intRow + 1, 3).Value2 = $emailAddress[0]                

                Write-Host "Updated EmailAddress Value..." $worksheet.Cells.Item($intRow + 1, 6).Value2 -ForegroundColor Green
            }
            
        }        
    }
    $WorkBook.Save()      
    $WorkBook.close()
    $objexcel.quit()
}

GetEmailFByLoginFromAD "C:\Temp\testdata.xlsx" "Sample"
于 2021-01-05T08:18:51.233 回答