开发者

Modify attributes in AD via PowerShell (no Quest)

开发者 https://www.devze.com 2023-03-18 18:07 出处:网络
Say I have users and their physicalDeliveryOfficeName attribute, called Office in AD is set to New York, and others say Chicago.

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 pr开发者_开发技巧operties  
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?


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消