Powershell调用Powershell脚本

Col*_*nic 6 powershell

& powershell .\other.ps1and 之间的行为& .\other.ps1有何不同?

编辑:特别是,如果出现错误,它们有何不同other.ps1

Joe*_*oey 12

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

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

PS> & .\x.ps1
bar
Run Code Online (Sandbox Code Playgroud)