Try/catch 在 Powershell 控制台中不起作用,但在 ISE 中有效 [英] Try/catch not working in Powershell console, but works in ISE

查看:42
本文介绍了Try/catch 在 Powershell 控制台中不起作用,但在 ISE 中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 try/catch 部分的 Powershell 脚本,可以将错误写入日志文件.这在 Powershell ISE 中非常有效,但在 Powershell 控制台中则不然.这是脚本:

I have a Powershell script with try/catch parts to write the errors into a logfile. This works perfectly fine in Powershell ISE, but not in the Powershell console. Here's the script:

$serverfile = "C:\Serverlist.txt"
$Logfile = "C:\Deploy_Log.txt"

$startdate= "01/05/2016"
$starttime = 13

Set-StrictMode -Version latest
$ErrorActionPreference = "Stop"
$WarningPreference = "Stop"

function LogWrite {
    param([string]$logstring)
    Add-Content $Logfile -Value $logstring
}

function DeployTask {
    param([string]$server1)

    $arguments = '/Create', '/S', "$server1", '/RU', 'domain\user', '/RP', 'pa$$w0rd', '/SC', 'ONCE', '/st', "$starttime`:00:00", '/sd', "$startdate", '/TN', 'Task_InstallUpdates', '/TR', 'C:\patchday\InstallUpdates.bat', '/RL', 'HIGHEST', '/F'
    Write-Host "$startdate, $starttime`:00"

    if (Test-Connection $server1 -Quiet) {
        # delete scheduled task
        try {
            schtasks.exe /S $server1 /delete /tn Task_InstallUpdates /f
        } catch {
            $ErrorMessage = $_.Exception.Message
            LogWrite "$server1 : Error deleting scheduled task - $ErrorMessage"
        }

        # create scheduled task
        try {
            & schtasks.exe $arguments
        } catch {
            $ErrorMessage = $_.Exception.Message
            LogWrite "$server1 : Error creating scheduled task - $ErrorMessage"
        }
    } else {
        LogWrite "$server1 is not available"
    }
}

$servers = Get-Content -Path $serverfile
foreach ($server1 in $servers) {
    Write-Host $server1
    try {
        DeployTask $server1
    } catch {
        $ErrorMessage = $_.Exception.Message
        LogWrite "$server1 : $ErrorMessage"
    }
}

有什么想法吗?我已将 $ErrorActionPreference$WarningPreference 设置为停止"并启用了严格模式.在 ISE 中创建了日志文件,内容如下所示:

Any ideas? I have set the $ErrorActionPreference and $WarningPreference to "Stop" and enabled strict mode. In ISE the logfile is created and the content is something like this:

srv17:创建计划任务时出错 - 警告:任务可能无法运行,因为/ST 早于当前时间.
srv18:创建计划任务时出错 - 警告:任务可能无法运行,因为/ST 早于当前时间.

srv17: Error creating scheduled task - WARNING: Task may not run because /ST is earlier than current time.
srv18: Error creating scheduled task - WARNING: Task may not run because /ST is earlier than current time.

在 Powershell 控制台中,不会创建日志文件,而是在控制台中显示所有错误和警告.

In Powershell console, the logfile is NOT created and all errors and warnings show in the console instead.

推荐答案

您正在运行外部命令.这些通常不会抛出 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"
}

这篇关于Try/catch 在 Powershell 控制台中不起作用,但在 ISE 中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆