Powershell – 检查特定OU中的所有用户是否被禁用,如果不是则禁用

我试图寻找它,但我没有find任何东西。 有人可以帮我一个脚本的例子,将这样做吗?

像这样的东西:

Get-ADUser -Filter * -SearchBase "ou=TheOU,dc=contoso,dc=com" | Disable-ADAccount 

如果没有命令,则可能需要升级Powershell版本。


尝试/ Catch示例:

 $users = Get-ADUser -Filter * -SearchBase "ou=TheOU,dc=contoso,dc=com" ForEach ($user in $users) { Try { Disable-Account } Catch { Write-Output "$($user) is already disabled." } Finally { # Cleanup tasks, etc. } } 

要避免禁用已禁用的帐户,可以将其过滤掉:

 Get-ADUser -Filter {Enabled -eq $true} -SearchBase "ou=TheOU,dc=contoso,dc=com" ` | Disable-ADAccount