本文介绍了在 Powershell 中从文本文件导入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有几个 Powershell 脚本可以自动执行 DLL 传输,我想将变量从一个文本文件导入到各种脚本中.我的例子:
变量.txt$foo = "blarg"$bar = "其他 blarg"
然后我想做这样的事情:
Script.ps1导入变量.txtecho "$foo $bar"
解决方案
这可以通过 Dot Sourcing 来完成.
创建一个 .ps1 文件,在其中声明您的变量,然后点源文件.这会将文件中声明的所有变量带入全局范围.
示例:
Variables.ps1 的内容:
$foo = "blarg"$bar = "其他 blarg"
点源:
../变量.ps1写主机$foo $bar"I've got a couple of Powershell scripts that automate DLL transferal and I'd like to import the variables from one text file into the various scripts. My example:
Variables.txt
$foo = "blarg"
$bar = "other blarg"
And then I'd like to do something like this:
Script.ps1
Imports Variables.txt
echo "$foo $bar"
解决方案
This can be accomplished by Dot Sourcing.
Create a .ps1 file, declare your variables in it, then dot source the file. This will bring any variables declared in the file into the global scope.
Example:
Contents of Variables.ps1:
$foo = "blarg"
$bar = "other blarg"
Dot source it:
. ./Variables.ps1
Write-Host "$foo $bar"
这篇关于在 Powershell 中从文本文件导入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!