我在 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"
任何人都可以帮助我提供他们的指导来解决这个问题。