[英]How to set environment variables in a PowerShell script defined in a configuration file?
I have a simple preferences system in batch.我有一个简单的批量偏好系统。 The bat renames the.ini to a.bat and call it.
bat 将.ini 重命名为a.bat 并调用它。 The variables or options that in the.ini will be initialized in that way.
.ini 中的变量或选项将以这种方式初始化。 So I can use it to create a multilingual batch file.
所以我可以用它来创建一个多语言的批处理文件。 Now I started learning PowerShell.
现在我开始学习 PowerShell。 I want to have the same function.
我想拥有相同的 function。
My files:我的文件:
MyFile.bat - initialize the ini and make some other stuff...: MyFile.bat - 初始化 ini 并制作一些其他的东西......:
ren User-Generated\preferences.ini preferences.bat
call User-Generated\preferences.bat
ren User-Generated\preferences.bat preferences.ini
echo Your language is %locale%.
...
And here we have the preferences.ini - in her, there saved all options eg.在这里我们有preferences.ini - 在她那里,保存了所有选项,例如。 language:
语:
set locale=de
set cancelled=true
set firstrun=false
Now I can create the same preferences.ini for PowerShell:现在我可以为 PowerShell 创建相同的preferences.ini:
$locale="de"
$cancelled="true"
$firstrun="false"
My question:我的问题:
I need to call the ini with a PowerShell script.我需要使用 PowerShell 脚本调用 ini。 How to do that?
怎么做?
@mofi wrote, that we could it make with this for loop either: @mofi 写道,我们可以用这个 for 循环来做:
for /F "usebackq delims=" %%I in ("%~dp0User-Generated\preferences.ini") do set "%%I"
But how to "convert" it to PowerShell?但是如何将其“转换”为 PowerShell?
Thanks for your help!谢谢你的帮助!
JJB JJB
You define some environment variables by using set .您可以使用set定义一些环境变量。 You could do the same in PowerShell by using the $env .
您可以使用$env在 PowerShell 中执行相同的操作。
$env:locale="de"
$env:cancelled="true"
$env:firstrun="false"
To call your bat script, you could use the call operator .要调用你的 bat 脚本,你可以使用call operator 。
& 'cmd.exe' @('/c', 'call', 'LoadPreferences.bat')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.