如何使用PowerShell执行CMD文件以自动设置环境变量?

时间:2019-04-03 06:09:29

标签: powershell cmd command-line

我想自动设置一个环境变量,而不先执行cmd,然后回显环境以显示环境变量的值。

我尝试了此代码,我将值存储为要设置为环境变量的cmd文件。如果要显示环境值,则必须首先执行cmd文件。

Param(
    [Parameter(Mandatory)] [string] $FilePath,
    [Parameter(Mandatory)] [string] $Key,
    [Parameter(Mandatory)] [string] $Variable_Name,
    [Parameter(Mandatory)] [string] $Store
)

$Found = $False
$FileContents = switch -File $FilePath {
    $Key    { $Found = $True }
    default {
        if ($Found) {
            $_.Substring(1);
            break
        }
    }
}
$FileContents

$Path_Store = $Store
$Output = "set" + " " + $Variable_Name + "=" + $FileContents |
          Out-File $Path_Store\$Variable_Name.cmd -Encoding Ascii

我的期望是,一旦执行PowerShell脚本,就可以直接显示环境变量。

0 个答案:

没有答案