完成我通过循环的samaccountnames名单和执行几个动作:尝试,美中不足的是:做什么,如果TRY成功
# Disabling user
try {
Disable-QADUser $user | Out-Null
} catch [exception] {
"Disable-QADuser: " + $($_.Exception.Message) | out-file $logfile -append
write-host " - Error disabling useraccount." -fore yellow
}
# Set informative description
try {
Set-QADuser $user -Description "Disabled $now" | Out-Null
} catch [exception] {
"Set-QADuser: " + $($_.Exception.Message)| out-file $logfile -append
write-host " - Error setting informative description in AD." -fore yellow
}
但我怎么输出的东西,如果命令成功完成?类似于
write-host "User $user disabled"
"User $user disabled" | out-file $logfile -append
非常感谢所有帮助/指示!
编辑 我注意到,我可以使用tee-object
发送输出到文件以及控制台..这样我就不必有行“三通”输出:)
那么您可以直接在try子句中的所有语句之后将变量设置为true。一个例外会跳过,因此它不会被设置(或者,如果您在退出try和catch语句时将'try'设置为'try',那么它将保持为false,然后测试它的值并相应地执行一些操作。 – deed02392 2012-03-28 10:21:25
太棒了提示,但不是我以后:)我投你的回应,因为我相信我会在稍后使用它! – Sune 2012-03-28 10:59:19