我有以下脚本:
#Script uses quest powershell commandlets which can be downloaded for free from quest website # http://www.quest.com/powershell/activeroles-server.aspx #Specify the OU you want to search for inactive accounts $SearchOU=“OU=Sites,DC=nl,DC=example,DC=com" #Specify the OU you want to move your inactive computer accounts to #$DestinationOU=“CN=Computers,DC=**,DC=example,DC=com" #Specify the number of days that computers have been inactive for $NumOfDaysInactiveFor = 100 #Specify the description to set on the computer account $Today = Get-Date $Description = "Account disabled due to inactivity on $Today" Get-QADComputer -InactiveFor $NumOfDaysInactiveFor -SizeLimit 0 -SearchRoot $searchOU -IncludedProperties ParentContainerDN | foreach { $computer = $_.ComputerName $SourceOU = $_.DN #Remove the commented # from the next line if you want to set the description to be the source OU #$Description = "SourceOU was $SourceOu" Set-QADComputer $computer -Description $Description Disable-QADComputer $computer #Move-QADObject $computer -NewParentContainer $destinationOU }
我想要做的是排除SearchOU中的一个或多个OU,这可能吗? 我不知道如何能够pipe理这个。
我使用以下SearchOU = OU=Sites,DC=nl,DC=example,DC=com 。 我想排除下面的OU例如= OU=Warehouses,OU=*,OU=Sites,DC=*,DC=example,DC=com
通过-LdapFilter选项,您可以设置区分大小写的LDAPsearchfilter。 但是,不支持使用例如(!ou=WareHouses)对DN进行AFAIK筛选…
典型的方法是一个嵌套循环,首先search范围为一级,列出OU,然后在名称不匹配的所有OU中进行search*,ou=WareHouses,*
在伪代码中:
for COUNTRY in DC=*.DC=example,DC=com do for OrgUnit in OU=*,DC=$COUNTRY,DC=example,DC=com do if ( $OrgUnit != *WareHouses* ) { Get-QADComputer -SearchRoot $OrgUnit .... } done done
这是对HBruijn更详细的回应。 我在下面的代码中添加了一行代码,用于过滤出您指定的“仓库”OU。 这可以被编辑为具有多个OU或调整正则expression式以适应您的需要。 你有很多方法可以做到这一点。
#Specify the OU you want to search for inactive accounts $SearchOU=“OU=Sites,DC=nl,DC=example,DC=com" #Specify the OU you want to move your inactive computer accounts to #$DestinationOU=“CN=Computers,DC=**,DC=example,DC=com" #Specify the number of days that computers have been inactive for $NumOfDaysInactiveFor = 100 #Specify the description to set on the computer account $Today = Get-Date $Description = "Account disabled due to inactivity on $Today" Get-QADComputer -InactiveFor $NumOfDaysInactiveFor -SizeLimit 0 -SearchRoot $searchOU -IncludedProperties ParentContainerDN | foreach { #Filter out OUs we don't care about if ( $_.DN -notmatch 'OU=Warehouses,OU=.*,OU=Sites,DC=.*,DC=example,DC=com') { $computer = $_.ComputerName $SourceOU = $_.DN #Remove the commented # from the next line if you want to set the description to be the source OU #$Description = "SourceOU was $SourceOu" Set-QADComputer $computer -Description $Description Disable-QADComputer $computer #Move-QADObject $computer -NewParentContainer $destinationOU } }