如何在powershell脚本中执行cmd文件? [英] How do I execute cmd file within powershell script?
本文介绍了如何在powershell脚本中执行cmd文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想自动设置环境变量,而不先执行cmd,然后回显环境以显示环境变量的值。
我试过这个代码,我将值存储为我想要设置为环境变量的cmd文件。如果我想显示环境值,我必须先执行cmd文件。
我尝试过:
I want to set an environment variable automatically, without executing the cmd first, then echo the environment to show the value of the environment variable.
I tried this code, I store the value as cmd file that I want to set as an environment variable. If I want to show the environment value, I have to execute the cmd file first.
What I have tried:
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脚本,我就可以直接显示环境变量
My expectation, once I execute the PowerShell script, I can directly show the environment variable
推荐答案
FilePath,
[参数(必填)] [string]
FilePath, [Parameter(Mandatory)] [string]
键,
[参数(必填)] [string]
Key, [Parameter(Mandatory)] [string]
Variable_Name,
[参数(强制性)] [string]
Variable_Name, [Parameter(Mandatory)] [string]
这篇关于如何在powershell脚本中执行cmd文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文