简体   繁体   English

使用调用命令调用.bat文件的Powershell错误处理

[英]Powershell error handling using invoke-command calling .bat file

I'm trying to use a TRY-CATCH block to capture the error of an invoke-command I'm using to call a remote .bat file on another server. 我正在尝试使用TRY-CATCH块来捕获我用来在另一台服务器上调用远程.bat文件的invoke-command的错误。

If the .bat file fails (it's executing a SP on the local SQL server to run a backup), I would like Powershell to know the .bat file had an error and go into my CATCH block. 如果.bat文件失败(正在本地SQL服务器上执行SP以运行备份),我希望Powershell知道.bat文件出错并进入我的CATCH块。

However, if the .bat file fails, it doesn't catch the error. 但是,如果.bat文件失败,则不会捕获该错误。 If the call to the .bat file fails (because I purpose renamed it to something that doesn't exist), it still does not go into my CATCH block. 如果对.bat文件的调用失败(因为我打算将其重命名为不存在的名称),则它仍不会进入我的CATCH块。

What am I missing? 我想念什么?

$scriptblock = {\\RemoteServerA\run.bat}
   try
   {
      Invoke-command –ComputerName $TargetComp -ScriptBlock $scriptblock -Credential $cred
   }
   catch
   {
      send-mailMessage -to "me@mail.ca" -subject "Remote .bat file failed" -from "you@mail.ca" -body ".bat file failed" -SmtpServer "smtp.me.ca" -credential $cred
   }

I even tried it using an if ... else block, and by this way it was a bit better. 我什至使用if ... else块尝试了它,这样效果更好。 It would fail if the call to the .bat file failed because I renamed it to something that doesn't exist. 如果对.bat文件的调用失败是失败的,因为我将其重命名为不存在的文件。 However if the contents of the .bat file failed (ie the SP failed), it would not recognize that failure, but seem like it sees the call to the .bat file was successful, and not fail. 但是,如果.bat文件的内容失败(即SP失败),它将无法识别该失败,但是看起来它看到对.bat文件的调用成功了,并且没有失败。

This was a more complete set of code, since my goal is to execute a 2nd batch file on the remote server upon successful completion of the first batch file on the remote server. 这是一组更完整的代码,因为我的目标是在成功完成远程服务器上的第一个批处理文件后在远程服务器上执行第二个批处理文件。

Invoke-command –ComputerName $TargetComp –ScriptBlock $scriptblock -Credential $cred

if ($? -eq "True")
   {Invoke-command –ComputerName $TargetComp –ScriptBlock $scriptblock2 –credential $cred
   $output2 = $?

   if ($output2 -eq "True")
      {send-mailMessage -to "me@mail.ca" -subject "Backup Successful" -from "me@mail.ca" -body "Yippee" -SmtpServer "smtp.me" -credential $cred
      Write-Host "Backup Successful"
      }
   else {send-mailMessage -to "me@mail.ca" -subject "BackupDB failed" -from "me@mail.ca" -body "BackupDB failed" -SmtpServer "smtp.me" -credential $cred
      Write-Host "Backup Failed" + $output2
      }
   }
else {send-mailMessage -to "me@mail.ca" -subject "DatabaseCheckDB failed" -from "me@mail.ca" -body "DatabaseCheckDB failed" -SmtpServer "smtp.me" -credential $cred}

Other option is to handel it like this: 另一种选择是像这样处理它:
The invoked script into Try, Catch and Finally. 调用的脚本分为“尝试”,“捕获”和“最终”。

$scriptblock = {
    Try {
        $strErrorMessage = $null
        $strTrace = $null

        $strTrace += "Start work"
        #Do Work
        $strTrace += "Work ended with LASTEXITCODE: '$LASTEXITCODE '"

    }
    Catch {
        $strErrorMessage = "Work Failed. Error: $($($error[0].ToString()) $($error[0].InvocationInfo.PositionMessage))"
    }
    Finally {
        $objReturnData = "" | Select-Object -Property strErrorMessage, strTrace
        $objReturnData.strErrorMessage = $strErrorMessage
        $objReturnData.strTrace = $strTrace
    }
    Return $objReturnData
}


And invoke it like this: 并像这样调用它:

$objReturnData = Invoke-command –ComputerName $TargetComp -ScriptBlock $scriptblock -Credential $cred


Then return the result. 然后返回结果。

$objReturnData.strTrace
$objReturnData.strErrorMessage 


Now you can use If / Else to look if the job failed. 现在,您可以使用If / Else查看作业是否失败。

Thanks for the quick help everyone, I think I was overthinking what I was trying to. 感谢大家的快速帮助,我认为我想得太多了。

Essentially there are 2 .bat files on my remote server, each calling a single stored procedure. 基本上,我的远程服务器上有2个.bat文件,每个文件都调用一个存储过程。

Now, instead of calling the .bat files, I am going to try a different approach and just call the SP directly. 现在,我将不尝试调用.bat文件,而是尝试另一种方法,而直接调用SP。 Upon successful completion of the first SP, call the 2nd. 成功完成第一个SP后,调用第二个。 If not, then fail and send an email. 如果不是,则失败并发送电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM