我正在从 C# 运行 powershell 脚本。
string scriptPath = "/script/myscript.ps1";
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptPath);
Collection<PSObject> results = pipeline.Invoke();
例如,如果我的myscript.ps1文件如下;
$test=4
$test++
$test
如何获取变量test
执行脚本后的值。我需要将该值添加到我的 C# 程序中。
我知道我迟到了,但是在你的脚本中,你需要添加global:
在 Powershell 脚本中要返回的变量前面,例如:
$global:test = 4
在 Powershell 脚本中。在 C# 中,打开运行空间后,调用策略更改器,设置 pipline,您可以执行以下操作
var result = runspace.SessionStateProxy.PSVariable.GetValue("test");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)