Load variables from another powershell script

你说的曾经没有我的故事 提交于 2019-11-28 22:18:21
zdan

The variables declared in Variables.ps1 are at "Script Scope". That is you can not see them outside of the scope of the script that declares them. One way to bring the variables in Variables.ps1 to the scope of main.ps1 is to "dot source" Variables.ps1. This, in effect, runs Variables.ps1 at the scope of main.ps1. To do this, just stick a period and space before your invocation of the script:

. .\Variables.ps1
$var1
$var2
# var.ps1
$Global:var1 = "1"
$Global:var2 = "2"

This works. Whether it's better or worse than "dot sourcing" probably depends on your specific requirements.

PS > .\var.ps1
PS > $var1
1
PS > $var2
2
PS >

Just to ensure correctness ... try this... in main.ps1

echo "Test"
. .\Variables.ps1
echo $var1
echo $var2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!