我正在执行一个在远程服务器上执行批处理脚本的PowerShell脚本。但在PowerShell脚本中,我无法处理批处理脚本中可能发生的任何故障。批处理脚本最后有exit %ERROR_CODE%
。在远程服务器上执行批处理脚本的PowerShell脚本
请让我知道如何捕获调用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
}
}
我试着用以下 - $ remotereturnvalue =调用命令-ComputerName $ DBSERVER -Authentication NegotiateWithImplicitCredential -ErrorAction停止-ScriptBlock {$ CMD =启动进程 “的cmd.exe” -ArgumentList “DbBackupBatch.cmd + $ $ FullBackupPath的CustName” $ cmdexitcode = $ cmd.Exitcode 如果($ cmdexitcode -eq 0){ 出口0 } 否则{ 出口1 } } – Himanshu