2011-07-06 36 views
1

假设我有用户和他们的physicalDeliveryOfficeName属性,名为Office中的AD设置为New York,其他人称芝加哥。通过PowerShell修改AD中的属性(没有任务)

我想设置一个脚本,将通过所有用户循环。

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

假设你有PowerShell的2.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 2.0,由于某种原因不能升级,您可以使用.NET System.DirectoryServices命名空间以及相关的类,在这里你应该能够合理地紧跟MSDN的例子,e.g. this for updatingthis example for searching。此外,Stackoverflow有很多例子,但this one看起来特别有希望在快速审查。

另外,我错过了Microsoft example of searching using PowerShell and System.DirectoryServices

+0

哇谢谢你!它是PowerShell 2.0 – Mike

+0

此解决方案仅适用于标准AD架构。我如何在没有Quest的情况下修改自定义属性?谢谢 –

+0

@AlexYeung:这应该是一个单独的问题。 AFAIK,如果你不想使用Quest,你会被困在.NET类中。链接在上面的答案中给出。 – ig0774

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"