我正在学习 PowerShell 为我的团队编写工具。我今天很忙,但我想通过删除 ForEach 循环中的 IF 语句来简化它,因为命令之间的唯一区别是参数-replace或-remove。
Write-Host "This script removes or replaces en masse users' msds-SyncServerURL property"
DO {
$TargetSSU=Read-Host "`nWhat msds-SyncServerURL do you want to replace or remove?"
$UsersToBeFixed=Get-ADUser -Filter {msds-SyncServerURL -like $TargetSSU} -Properties ('name','samaccountname','msDS-SyncServerURL')
IF ($UsersToBeFixed -eq $null) {
Write-Host "`nNo users appear to have $TargetSSU as their msds-SyncServerURL value. Please try again."
}
} UNTIL ($UsersToBeFixed -ne $null)
Write-Host "`n`nThe following users have $TargetSSU as their msds-SyncServerURL value:"
$UsersToBeFixed |select name,samaccountname,msds-syncserverurl|ft
DO …Run Code Online (Sandbox Code Playgroud)