我正在嘗試使用 powershell 從 CSV 檔案更新 AD 用戶屬性:title、physicalDeliveryOfficeName 和部門。我是powershell的新手,所以我需要一些幫助。(請,并提前感謝)所以,這個想法是匹配的過濾器是 displayName 屬性,我使用的腳本是:
Import-Module ActiveDirectory
$csv = Import-Csv C:\Temp\AD_import_test.csv
foreach ($line in $csv) {
$displayName = $line.displayName
Get-ADUser -Filter {displayName -eq $displayName} |
Set-ADUser -Title $($csv.title) -department $($csv.Department) -physicalDeliveryOfficeName $($csv.physicalDeliveryOfficeName) }
但腳本回傳錯誤:
Set-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Title'. Specified method is not supported.
At line:6 char:19
Set-ADUser -Title $($csv.title) -department $($csv.Department) -physi ...
~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADUser
Set-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Title'. Specified method is not supported.
At line:6 char:19
Set-ADUser -Title $($csv.title) -department $($csv.Department) -physi ...
~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADUser
uj5u.com熱心網友回復:
您的代碼對我來說似乎很好,除了set-aduser您需要替換$csv為$line
Import-Module ActiveDirectory
$csv = Import-Csv C:\Temp\AD_import_test.csv
foreach ($line in $csv) {
$displayName = $line.displayName
Get-ADUser -Filter {displayName -eq $displayName} |
Set-ADUser -Title $($line.title) -department $($line.Department) -physicalDeliveryOfficeName $($line.physicalDeliveryOfficeName) }
uj5u.com熱心網友回復:
您使用錯誤的變數來分配這些值。在您的腳本中 $csv 包含整個 .csv 檔案,但在您的 foreach 行中,您將每一行加載到 $line 中,這是您需要設定值的內容。
所以本質上,$($csv.title) 等于所有標題欄位,而不僅僅是您感興趣的那個。這就是為什么您會收到錯誤訊息,說它是 System.Object[] 而不是 System.String。 ..因為它不是一個單一的值。
我注意到你在 "$displayName = $line.displayName" 行上是正確的,所以它只是需要更新的 Set-ADUser 行,如:
Set-ADUser -Title $($line.title) -department $($line.Department) -physicalDeliveryOfficeName $($line.physicalDeliveryOfficeName) }
編輯:另外,像這樣一個有用的技巧是添加一些 Write-Output 行來顯示變數內容是什么,這樣你就可以檢查它們是否符合你的期望,例如:
Write-Output $($line.title)
然后在 $line.title 的情況下應該向您顯示一個值,而如果您這樣做 $($csv.title) 它會回傳多個值并告訴您問題出在哪里。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/414549.html
標籤:
上一篇:清除函式中的文本框
