PowerShell停止脚本捕获来自另一个脚本

时间:2016-03-03 16:12:51

标签: shell powershell batch-file powershell-v4.0

我遇到了问题...我有两个ps1 PowerShell脚本:

第一个捕捉到这样的错误:

$F = "Hola2"
try {
  if ($F -contains "Hola") { write-host "ok" }
  else {
    write-error "Word is not Hola"
    return
  }
}
catch {
  throw
  break
}

另一个叫第一个像这样:

$F1 = "Hola2"

try {
   .\sub1.ps1
   if ($F1 -contains "G") { write-host "ok" }
   else {
     write-error "Word is not Hola2"
     return
   }
}
catch {
  throw
  #Write-Warning "Caught: $_"
}

但是当我执行第二个脚本时,显示错误但它没有停止,继续然后向我显示第二个错误。我想停止第一个脚本错误。

你能帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

调用脚本就是您正在做的事情。它在自己的环境中运行。外部脚本永远不会知道它的异常。

相反,通过点源代码在外部上下文中运行第二个脚本:

. .\sub1.ps1

第一个点.充当包含,并且好像该文件的代码嵌入在该行中。

相关问题