使用Powershell调用Powershell脚本

6

使用& powershell .\other.ps1& .\other.ps1的行为有何区别?

特别是,如果other.ps1中出现错误,它们之间有什么不同?

1个回答

12

在前一种情况下,您会获得另一个PowerShell进程,脚本无法读取当前会话中定义的变量。

PS> $foo = 'bar'
PS> 'Write-Host $foo'|Set-Content x.ps1
PS> & powershell .\x.ps1

PS> & .\x.ps1
bar

1
@ColonelPanic:就像变量一样,异常也不会跨进程边界。更改x.ps1的内容为throw error,并比较try { powershell.exe ./x.ps1; "success? $?; $LastExitCode" } catch { "caught $_; success? $?; $LastExitCode" }try { ./x.ps1; "success? $?; $LastExitCode" } catch { "caught $_; success? $?; $LastExitCode" }的输出。 - Emperor XLII

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接