简体   繁体   English

如何在配置文件中定义的 PowerShell 脚本中设置环境变量?

[英]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.

相关问题 如何为Powershell脚本传递或设置环境变量? - How To Pass in or Set Environment Variable for Powershell script? PowerShell脚本,用于备份所有环境变量并将其保存在文件中 - PowerShell script to take backup of all environment variables & save it in a file 使用 Powershell 脚本启动的批处理文件设置环境变量 - Setting environment variables with batch file lauched by Powershell script 在 powershell 脚本中设置环境变量,从 a.bat 文件中读取它们 - Setting environment variables in powershell script reading them from a .bat file 如何从powershell中为bash设置环境变量? - How to set environment variables for bash from within powershell? Powershell 用于从带分隔符的文本文件设置多个变量的脚本 - Powershell script to set multiple variables from a delimited text file 即使在环境变量中定义了模块路径后,Powershell模块也未加载 - Powershell Module not loading even after the module path is defined in environment variables 如何从 Powershell 脚本文件访问 Azure DevOps 预定义变量 - How to access Azure DevOps Predefined variables from Powershell script file 从.ps1脚本设置环境变量 - Set Environment Variables From a .ps1 Script 检查是否已加载Powershell脚本-环境文件 - Check if powershell script is loaded - environment file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM