Modify attributes in AD via PowerShell (no Quest)

好久不见. 提交于 2020-01-06 03:42:13

问题


Say I have users and their physicalDeliveryOfficeName attribute, called Office in AD is set to New York, and others say Chicago.

I want to setup a script that will loop through all users.

If physicalDeliveryOfficeName = Chicago  
Set address properties   
Street: 8888 Chicago Lane  
City: Chicago  
State: IL  
Zip: 60066  
Country: United States

else if physicalDeliveryOfficeName = New York  
Set address properties  
Street: 9999 New York Lane
City: New York
State: NY
Zip: 11111
Country: United States

I can't seem to find out where to start.. any pointers?


回答1:


Assuming you have PowerShell v2.0, you can use the built-in Active Directory module, in particular, the Get-ADUser command followed by Set-ADUser, something like:

Get-ADUser -Filter {Office -eq "Chicago"} | Set-ADUser -StreetAddress "8888 Chicago Lane City" -City "Chicago" -State "IL" -PostalCode "60066" -Country "US"

The full list of available attributes and some examples are available by following the links above or via the Get-Help cmdlet.

If you're not on PowerShell v2.0 and can't upgrade for some reason, you can use the .NET System.DirectoryServices namespace and associated classes, where you should be able to follow reasonably closely the MSDN examples, e.g. this for updating and this example for searching. Additionally, Stackoverflow has numerous examples, though this one looks particularly promising on a quick review.

Also, I missed the Microsoft example of searching using PowerShell and System.DirectoryServices.




回答2:


I tweaked the above to add/change Address info of employees moving from one location to another. In the sample below of what I did, of course I changed the address of John Doe. But this is a one line powershell command line that worked great for those who have not learned scripts yet:

get-aduser -filter {SamAccountName -eq "jdoe"} | Set-ADUser -Office "New York" -StreetAddress "123 N Main St" -city "New York" -State "NY" -PostalCode "10044" -Country "US"


来源:https://stackoverflow.com/questions/6603519/modify-attributes-in-ad-via-powershell-no-quest

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