【问题标题】:Power Shell CSV to ADPowershell CSV 到 AD
【发布时间】:2020-07-03 18:35:54
【问题描述】:
也许有人可以帮忙?
我有一个脚本,它从 scv 获取参数并将它们放入 AD,脚本工作没有错误,但我没有从某种原因得到结果。
请帮忙!
Import-CSV -Path "$home\desktop\Scripts\test4.scv" | ForEach-Object -process {Write-Host $_ }
{Set-ADuser|]= -Identity $_.DisplayName -extensionattribute5 $_.extensionattribute5}
示例 scv
【问题讨论】:
标签:
powershell
active-directory
powershell-2.0
powershell-3.0
【解决方案1】:
试试这个:
Import-CSV -Path "$home\desktop\Scripts\test4.scv" | ForEach {
Write-Host $_
Set-ADuser -Identity $_.DisplayName -Add @{extensionattribute5=$_.extensionattribute5}
}
您的代码已损坏。支撑不正确。使用 -Add 参数还可以通过哈希表添加扩展属性。
【解决方案2】:
根据the docs,Set-ADUser上的-Identity参数必须是其中之一
- 专有名称
- 一个 GUID (objectGUID)
- 安全标识符 (objectSid)
- SAM 帐户名 (sAMAccountName)
这意味着您不能将 CSV 中的 DisplayName 属性用于此参数。
试试:
Import-CSV -Path "$home\desktop\Scripts\test4.scv" | ForEach-Object {
$user = Get-ADUser -Filter "DisplayName -eq '$($_.DisplayName)'" -Properties DisplayName -ErrorAction SilentlyContinue
if ($user) {
Write-Host "Setting extensionattribute5 property for user $($_.DisplayName)"
$user | Set-ADuser -Add @{extensionattribute5=$_.extensionattribute5}
}
else {
Write-Warning "User $($_.DisplayName) could not be found"
}
}
你可能更喜欢-Replace @{extensionattribute5=$_.extensionattribute5},而不是-Add @{extensionattribute5=$_.extensionattribute5}。这在问题中不清楚