我一直在努力找出修復我的腳本的方法。對于在 CSV 中有空單元格的任何用戶,我不斷收到“替換”錯誤。
示例:我們有一些用戶沒有手機。該用戶的手機單元格將為空白。
我得到的“替換”只發生在對每個欄位都沒有價值的用戶身上。
我已經鏈接了下面 csv 列的影像。
我看過一些關于解決這個問題的帖子,但似乎沒有一個與我的情況相符。任何幫助將不勝感激。
謝謝,
$TestUsers = Import-Csv -Path C:\Users\test.admin\Documents\ADDummyTestUsers.csv
foreach( $user in $testusers )
{ Set-ADUser -Identity $user.sAMAccountName `
-Title $user.Title `
-Description $user.Title `
-OfficePhone $user.TelephoneNumber `
-MobilePhone $user.Mobile `
-Fax $user.facismileTelephoneNumber `
-StreetAddress $user.streetAddress `
-City $user.city `
-State $user.st `
-PostalCode $user.postalCode `
}
csv列
uj5u.com熱心網友回復:
在這種情況下,您可以-Instance在Set-ADUser.
$TestUsers = Import-Csv -Path C:\Users\test.admin\Documents\ADDummyTestUsers.csv
foreach( $user in $testusers )
{
$ADUser = Get-ADUser -Identity $user.sAMAccountName
$ADUser.Title = $user.Title
$ADUser.Description = $user.Title
$ADUser.OfficePhone = $user.TelephoneNumber
$ADUser.MobilePhone = $user.Mobile
$ADUser.Fax = $user.facismileTelephoneNumber
$ADUser.StreetAddress = $user.streetAddress
$ADUser.City = $user.city
$ADUser.State = $user.st
$ADUser.PostalCode = $user.postalCode
Set-ADUser -Instance $ADUser
}
uj5u.com熱心網友回復:
OP 所指的“替換”錯誤類似于:
Set-ADUser : 替換 At line:15 char:5
Set-ADUser -Instance $User~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidOperation: (:) [Set-ADUser], ADInvalidOperationException
- FullQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser
我建議在嘗試設定之前簡單地測驗一個值。我在下面演示了這一點,同時也稍微改變了方法(使用實體)
$TestUsers = Import-Csv -Path C:\tmp\file.csv
$TestUsers | ForEach-Object {
$User = $null
$User = Get-ADUser -Identity $_.samaccountname -Properties description,telephonenumber,mobile,facsimiletelephoneNumber,streetaddress,city,st,postalcode
if ('*' -like $_.description) {$User.description = $_.description}
if ('*' -like $_.telephonenumber) {$User.telephonenumber = $_.telephonenumber}
if ('*' -like $_.mobile) {$User.mobile = $_.mobile}
if ('*' -like $_.facsimiletelephoneNumber) {$User.facsimiletelephoneNumber = $_.facsimiletelephoneNumber}
if ('*' -like $_.streetaddress) {$User.streetaddress = $_.streetaddress}
if ('*' -like $_.city) {$User.city = $_.city}
if ('*' -like $_.st) {$User.st = $_.st}
if ('*' -like $_.postalcode) {$User.postalcode = $_.postalcode}
Set-ADUser -Instance $User
}
uj5u.com熱心網友回復:
我將首先測驗是否存在具有該 SamAccountName 的用戶(如您所說,該欄位在 CSV 檔案中永遠不會為空),如果是,則構建一個僅包含非空或空白屬性的Splatting Hashtable。
通過使用 Set-ADUser cmdlet 的引數,您還可以擺脫代碼中那些討厭的反引號:
$testusers = Import-Csv -Path 'C:\Users\test.admin\Documents\ADDummyTestUsers.csv'
foreach ($user in $testusers) {
$ADUser = Get-ADUser -Filter "SamAccountName -eq '$($user.sAMAccountName)'" -ErrorAction SilentlyContinue
if (!$ADUser) {
Write-Warning "User $($user.sAMAccountName) does not exist"
continue # skip this one and proceed with the next
}
# create a hashtable for splatting
$splat = @{}
# only add fields that have content
if (![string]::IsNullOrWhiteSpace($user.Title)) {
$splat['Title'] = $user.Title
$splat['Description'] = $user.Title
}
if (![string]::IsNullOrWhiteSpace($user.TelephoneNumber)) { $splat['OfficePhone'] = $user.TelephoneNumber }
if (![string]::IsNullOrWhiteSpace($user.Mobile)) { $splat['MobilePhone'] = $user.Mobile }
if (![string]::IsNullOrWhiteSpace($user.facismileTelephoneNumber)) { $splat['Fax'] = $user.facismileTelephoneNumber }
if (![string]::IsNullOrWhiteSpace($user.streetAddress)) { $splat['StreetAddress'] = $user.streetAddress }
if (![string]::IsNullOrWhiteSpace($user.city)) { $splat['City'] = $user.city }
if (![string]::IsNullOrWhiteSpace($user.st)) { $splat['State'] = $user.st }
if (![string]::IsNullOrWhiteSpace($user.postalCode)) { $splat['PostalCode'] = $user.postalCode }
# now set the properties on the user
$ADUser | Set-ADUser @splat
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/321812.html
