【发布时间】:2017-02-15 06:28:13
【问题描述】:
我正在执行一个在远程服务器上执行批处理脚本的 PowerShell 脚本。但在 PowerShell 脚本中,我无法处理批处理脚本中可能发生的任何故障。批处理脚本末尾有exit %ERROR_CODE%。
请告诉我如何在调用 PowerShell 脚本的批处理脚本中发现任何错误。
我的 PowerShell 脚本是这样的:
$DBServer = $args[0]
$CustName = $args[1]
$FullBackupPath = $args[2]
$command = "cmd.exe /c DbBackupBatch.cmd " + $FullBackupPath + " " + $CustName
$script = 'Invoke-Expression -Command "' + $command + '"'
$scriptblock = [scriptblock]::Create($script)
try {
Invoke-Command -ComputerName $DBServer -Authentication NegotiateWithImplicitCredential -ErrorAction Stop -ScriptBlock $scriptblock
exit 0
} catch {
$message = $_.Exception.Message
Write-Host $_.Exception.Message
# While executing a Java programs, we get message as below -
# Picked up JAVA_TOOL_OPTIONS: -Xms512m -Xmx512m
# This message is treated as error message by PowerShell, though it is not an error
if (($message.Length -lt 50) -and ($message.Contains('Picked up JAVA_TOOL_OPTIONS:'))) {
exit 0
} else {
Write-Host $_.Exception.Message
exit 1
}
}
【问题讨论】:
标签: powershell