我將 AD 聯系人匯入新域的腳本失敗,因為某些值為 Null 和 Empty。我將聯系人匯出為 XML。如何使用 IF 陳述句來解決此問題以創建 AD 物件并在 Null 和 Empty 時跳過,但如果有值則填寫它?
匯入腳本
Import-Module ActiveDirectory
$import = Import-Clixml "C:\Users\Downloads\contacts.xml"
foreach($contact in $import){
$newContact=@{
path = "OU=Test,OU=domain,OU=B"
type = "Contact"
Name = $contact.name
otherAttributes = @{
givenname = $contact.givenName
sn = $contact.sn
mail = $contact.mail
displayName = $contact.displayName
cn = $contact.cn
co = $contact.co
company = $contact.company
l = $contact.l
mailNickname = $contact.mailNickname
telephoneNumber = $contact.telephoneNumber
st = $contact.st
streetAddress = $contact.streetAddress
postalcode = $contact.postalcode
physicalDeliveryOfficeName = $contact.physicalDeliveryOfficeName
mobile = $contact.mobile
}
}
New-ADObject @newContact
}
錯誤
New-ADObject : Cannot validate argument on parameter 'OtherAttributes'. The argument is null or an element of the argument collection contains a null value.
At C:\Users\Documents\Scripts\contacts-import.ps1:26 char:18
New-ADObject @newContact
~~~~~~~~~~~
CategoryInfo : InvalidData: (:) [New-ADObject], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.NewADObject
New-ADObject : Cannot validate argument on parameter 'OtherAttributes'. The argument is null or an element of the argument collection contains a null value.
At C:\Users\Documents\Scripts\contacts-import.ps1:26 char:18
New-ADObject @newContact
~~~~~~~~~~~
CategoryInfo : InvalidData: (:) [New-ADObject], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirecto
uj5u.com熱心網友回復:
一次構建一個OtherAttributes字典,并測驗對應的值是否實際存在:
# ...
Import-Module ActiveDirectory
$import = Import-Clixml "C:\Users\Downloads\contacts.xml"
$potentialAttributes = @(
'givenName'
'sn'
'mail'
'displayName'
'cn'
'co'
'company'
'l'
'mailNickname'
'telephoneNumber'
'st'
'streetAddress'
'postalcode'
'physicalDeliveryOfficeName'
'mobile'
)
foreach($contact in $import){
$newContact=@{
path = "OU=Test,OU=domain,OU=B"
type = "Contact"
Name = $contact.name
OtherAttributes = @{}
}
foreach($attributeName in $potentialAttributes){
if(-not [string]::IsNullOrEmpty($contact.$attributeName)){
$newContact['OtherAttributes'][$attributeName] = $contact.$attributeName
}
}
New-ADObject @newContact
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/507622.html
標籤:电源外壳
