通过PowerShell修改AD中的属性(无任务)

时间:2011-07-06 21:52:46

标签: powershell active-directory

假设我有用户及其physicalDeliveryOfficeName属性,在AD中称为Office,设置为纽约,其他人则说芝加哥。

我想设置一个循环遍历所有用户的脚本。

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

我似乎无法找到从哪里开始..任何指针?

2 个答案:

答案 0 :(得分:2)

假设你有PowerShell v2.0,你可以使用the built-in Active Directory module,特别是Get-ADUser命令后跟Set-ADUser,例如:

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

可以通过上面的链接或通过Get-Help cmdlet获取可用属性的完整列表和一些示例。

如果您没有使用PowerShell v2.0并且由于某种原因无法升级,则可以使用.NET System.DirectoryServices命名空间和关联的类,您应该能够合理地密切关注MSDN示例{ {3}}和e.g. this for updating。此外,Stackoverflow有很多例子,但this example for searching在快速审查中看起来特别有希望。

另外,我错过了this one

答案 1 :(得分:1)

我调整了以上内容以添加/更改员工从一个位置移动到另一个位置的地址信息。在我做的下面的示例中,当然我改变了John Doe的地址。但这是一个单行的PowerShell命令行,对那些尚未学习脚本的人来说非常有用:

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"