Powershell调用Powershell脚本
问题描述:
& powershell .\other.ps1
和& .\other.ps1
之间的行为有何不同?Powershell调用Powershell脚本
编辑:特别是,如果other.ps1
有错误,它们又有什么不同?
答
你得到另一个PowerShell进程在前者的情况下和脚本无法读取当前会话定义的变量:
PS> $foo = 'bar'
PS> 'Write-Host $foo'|Set-Content x.ps1
PS> & powershell .\x.ps1
PS> & .\x.ps1
bar
感谢。错误处理也有差异吗? – 2012-07-09 11:45:45
@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”}'。 – 2012-08-18 13:27:06