对于powershell执行另一个powershell,“&”之间有什么区别?和“。”

时间:2015-05-27 01:18:19

标签: powershell execution

像:

& another.ps1

. another.ps1

他们的区别是什么? 我希望获取另一个PowerShell脚本的输出文本,同时不想导入另一个脚本中定义的内部函数。我可以使用什么声明?我发现甚至“&”命令将自动导入其他powershell脚本的所有函数定义。

谢谢。

1 个答案:

答案 0 :(得分:6)

区别在于范围。 &将在其自己的范围内运行脚本。 .将在当前范围内运行脚本。

$ErrorView = 'CategoryView'
$x = 'Test'

. { Get-variable x -Scope 0 }
& { Get-Variable x -Scope 0 }


Name                           Value                                                                      
----                           -----                                                                      
x                              Test                                                                       
ObjectNotFound: (x:String) [Get-Variable], ItemNotFoundException

在第一个示例中,脚本是点源到当前范围,$ x在范围0处可见。

在第二个示例中,使用&amp ;;调用脚本。在范围0中看不到运算符和$ x。

相关问题