简体   繁体   English

通过 Powershell 创建环境变量

[英]Creating Environment Variable Through Powershell

I am trying to create Environment variable for machine through powershell and passing the variables at run-time but It's not taking the variables.我正在尝试通过 powershell 为机器创建环境变量并在运行时传递变量,但它没有使用变量。 Powershell file: env_variable.ps1 Powershell 文件:env_variable.ps1

Param (
  [Parameter(Mandatory=$True)]  [String] $VAULT_TEXT,
  [Parameter(Mandatory=$True)]  [String] $VAL
 )
[System.Environment]::SetEnvironmentVariable("$VAULT_TEXT","$VAL",[System.EnvironmentVariableTarget]::Machine)

Trying to trigger through Powershell:尝试通过 Powershell 触发:

$vault_text='IAndAAuth'
$val = get-content 'C:\vault\service_acct_pass'
C:\vault\env_variable.ps1 -VAULT_TEXT "$vault_text" -VAL "$val"

Result is coming like this: Key:VAULT_TEXT, Value:VAL in environments value.结果是这样的:Key:VAULT_TEXT, Value:VAL 在环境值中。

Can you please help me in this.你能帮我解决这个问题吗?

Your method seems right and should be working.你的方法似乎是正确的,应该有效。 If you like try doing it the old way by creating a registry key for you environment variable.如果您喜欢尝试通过为您的环境变量创建注册表项来使用旧方法。

User用户

New-ItemProperty "HKCU:\Envrionment" -Name "your variable name" -Value "your value"

Machine机器

New-ItemProperty "HKLM:\System\CurrentControlSet\Control\Session Manager\Environment" -Name "your variable name" -Value "your value"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM