Power Shell CSV to AD

帅比萌擦擦* 提交于 2020-05-17 08:54:05

问题


Maybe someone can to help? I have a script it take parameters from scv and put them to AD, script work without mistakes but I`m does not have results from some reasone.
Please help!

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

example scv


回答1:


According to the docs, the -Identity parameter on Set-ADUser must be one of

  • A distinguished name
  • A GUID (objectGUID)
  • A security identifier (objectSid)
  • A SAM account name (sAMAccountName)

This means that you cannot use the DisplayName property from the CSV for this parameter.

Try:

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"
    }
}

Instead of -Add @{extensionattribute5=$_.extensionattribute5}, you may rather want -Replace @{extensionattribute5=$_.extensionattribute5}. This isn't clear in the question




回答2:


Try this:

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

Your code was broken. Bracing was incorrect. Also Extended attributes are added with a hash table using -Add parameter.



来源:https://stackoverflow.com/questions/60811166/power-shell-csv-to-ad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!