【发布时间】:2019-11-05 05:12:35
【问题描述】:
我想从 .xlsx 批量更新我们在 DC 上的用户,但脚本应该跳过 xlsx 数据中的空值。
我已经尝试了几种方法,但都不起作用。
第一次尝试是用
hash.GetEnumerator() | foreach {
if ($_.Value -eq "") {
$hash.Remove($_.Key)
}
}
第二次尝试如下代码:
foreach ($h in $hash.Keys) {
if ($(hash.Item($h)) -eq "") {
$hash.Remove($h)
try {
Import-Module ActiveDirectory -ErrorAction Stop -WarningAction Stop
} catch {
Write-Host "ActiveDirectory module not found!!" -ForegroundColor Red -BackgroundColor Black
Write-Host "Script Aborted." -ForegroundColor Red -BackgroundColor Black
# Abort the script.
break
}
try {
$credential = Get-Credential
[Threading.Thread]::CurrentThread.CurrentCulture = 'en-US'
#Declare file path and sheet name
$file = "...\UpdateUsers_test.xlsx"
#Create an Excel.Application instance and open that file
$Excelobject = New-Object -ComObject Excel.Application
$Workbook = $Excelobject.Workbooks.Open($file)
$sheetName = "Sheet1"
$sheet = $Workbook.Worksheets.Item($sheetName)
#$objExcel.Visible=$true
#Count max row
$rowMax = ($sheet.UsedRange.Rows).Count
#Count max column
$colMax = ($sheet.UsedRange.Columns).Count
$hash = @{}
$server = "IP-Address"
#Specify starting positions
$row, $col = 1, 1
$updatedCount = 0
#loop for rows
for ($i=1; $i -le $rowMax-1; $i++) {
#loop for columns
for ($c=0; $c -le $colMax-1; $c++) {
#Get all columns values to a hash
$hash += @{$sheet.Cells.Item($row, $col+$c).Text = $sheet.Cells.Item($row+$i, $col+$c).Text}
foreach ($h in $hash.Keys) {
if ($(hash.Item($h)) -eq "") {
$hash.Remove($h)
}
#Create an object and assign hash keys as object property
$Object = New-Object -TypeName PSObject -Property $hash
#Get User via SamAccountname
$user = Get-ADUser -Filter "SamAccountName -eq '$($Object.UserName)'" -
SearchBase 'DC=Domain, DC=local' -Server $server -Credential $credential
#Set Users attribute with matched object attribute
$user | Set-ADUser -DisplayName $Object.Displayname `
-OfficePhone $Object.PhoneNumber `
-EmailAddress $Object.email `
-Postalcode $Object.Postalcode `
-City $Object.City `
-Streetaddress $Object.Streetaddress `
-Country $Object.Country `
-Office $Object.Office `
-Title $Object.Title `
-Company $Object.Company `
-Department $Object.Department `
-Manager $Object.Manager `
-Mobile $Object.Mobile `
-Fax $Object.Fax
#If you want to edit Object common name, you can remove enable two lines
below.
#$userguid = $user.ObjectGUID.Guid
#$user | Rename-ADObject -NewName $Object.DisplayName -Server $server -
Credential $credential
$hash = @{}
Write-Host $User.Name "- User attributes have been updated." -
ForegroundColor Yellow
Start-Sleep -s 1
$updatedCount += 1
}
Write-Host $updatedCount "Users have been updated" -ForegroundColor Green
#close excel file
$Excelobject.Quit()
}
catch {
Write-Error $_.Exception.ToString()
Read-Host -Prompt "The above error occured. Press Enter to exit."
}
【问题讨论】:
-
您好,欢迎来到 Stack Overflow。我认为如果您将 Excel 导出为 CSV 文件并将其用于输入,会容易得多。 Powershell 具有出色的 cmdlet 可用于处理 CSV,而使用 COM 对象解析 Excel 工作表可能非常困难..
-
“不起作用”是一个不充分的问题描述。你期望代码做什么,它实际上做什么?此外,您发布的最后一个代码 sn-p 有几个语法错误(主要但不完全是由于不正确的换行)。请确保您问题中的代码不会引入实际代码中不存在的新错误。推荐的过程是创建一个minimal reproducible example,验证代码是否显示与您的真实代码相同(错误)的行为,然后发布该代码以及该代码引发的错误。
标签: powershell active-directory