我有一个bar.cmd
用单行命名的批处理文件:ECHO %Foo%
。
如何设置Foo
Powershell脚本,以便在调用& .\bar.cmd
时可以打印Bar
?
在PowerShell中设置环境变量:
Set-Item Env:foo "bar"
要么
$env:foo = "bar"
如果您想以其他方式做到这一点,请阅读此文章:
http://windowsitpro.com/powershell/take-charge-environment-variables-powershell
当您运行cmd.exe
以在PowerShell中执行Shell脚本(.bat
或.cmd
文件)时,该变量在的运行实例中设置,cmd.exe
但在该cmd.exe
实例终止时丢失。
解决方法:运行cmd.exe
shell脚本并输出它设置的所有环境变量,然后在当前PowerShell会话中设置这些变量。本文提供了一个简短的PowerShell函数,可以为您执行此操作:
# Invokes a Cmd.exe shell script and updates the environment.
function Invoke-CmdScript {
param(
[String] $scriptName
)
$cmdLine = """$scriptName"" $args & set"
& $Env:SystemRoot\system32\cmd.exe /c $cmdLine |
Select-String '^([^=]*)=(.*)$' | ForEach-Object {
$varName = $_.Matches[0].Groups[1].Value
$varValue = $_.Matches[0].Groups[2].Value
Set-Item Env:$varName $varValue
}
}
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras