vik*_*ata 1 powershell execution
喜欢:
& another.ps1
Run Code Online (Sandbox Code Playgroud)
和
. another.ps1
Run Code Online (Sandbox Code Playgroud)
他们的区别是什么?我希望获取另一个PowerShell脚本的输出文本,同时不想导入另一个脚本中定义的内部函数.我可以使用什么声明?我发现即使"&"命令也会自动导入其他powershell脚本的所有函数定义.
谢谢.
差异在于范围. &将在其自己的范围内运行脚本. .将在当前范围内运行脚本.
$ErrorView = 'CategoryView'
$x = 'Test'
. { Get-variable x -Scope 0 }
& { Get-Variable x -Scope 0 }
Name Value
---- -----
x Test
ObjectNotFound: (x:String) [Get-Variable], ItemNotFoundException
Run Code Online (Sandbox Code Playgroud)
在第一个示例中,脚本是点源到当前范围,$ x在范围0处可见.
在第二个示例中,使用&运算符调用脚本,并且在作用域0处看不到$ x.
| 归档时间: |
|
| 查看次数: |
46 次 |
| 最近记录: |