且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Try/catch 在 Powershell 控制台中不起作用,但在 ISE 中有效

更新时间:2023-02-06 11:13:00

您正在运行外部命令.这些通常不会抛出 PowerShell 异常.改为评估 $LASTEXITCODE:

You're running external commands. Those usually don't throw PowerShell exceptions. Evaluate $LASTEXITCODE instead:

$output = & schtasks.exe /S $server1 /delete /tn Task_InstallUpdates /f 2>&1
if ($LASTEXITCODE -ne 0) {
    LogWrite "$server1 : Error deleting scheduled task - $output"
}

# create scheduled task
$output = & schtasks.exe $arguments 2>&1
if ($LASTEXITCODE -ne 0) {
    LogWrite "$server1 : Error creating scheduled task - $output"
}