Power Shell CSV到AD

时间:2020-03-23 09:50:40

标签: powershell active-directory powershell-2.0 powershell-3.0

也许有人可以提供帮助? 我有一个脚本,它从scv获取参数并将它们放到AD中,脚本工作没有错误,但是我没有某种原因的结果。
请帮忙!

Import-CSV -Path "$home\desktop\Scripts\test4.scv" | ForEach-Object -process {Write-Host $_ }
{Set-ADuser|]= -Identity $_.DisplayName -extensionattribute5 $_.extensionattribute5}

示例scv

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试一下:

Import-CSV -Path "$home\desktop\Scripts\test4.scv" | ForEach {
  Write-Host $_ 
  Set-ADuser -Identity $_.DisplayName -Add @{extensionattribute5=$_.extensionattribute5}
}

您的代码已损坏。支撑不正确。还可以使用-Add参数将扩展属性与哈希表一起添加。

答案 1 :(得分:0)

根据the docs-Identity上的Set-ADUser参数必须为

之一
  • 专有名称
  • 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"
    }
}

您可能希望使用-Add @{extensionattribute5=$_.extensionattribute5}而不是-Replace @{extensionattribute5=$_.extensionattribute5}。这个问题不清楚