完成我通過循環的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