【发布时间】:2020-05-26 23:41:32
【问题描述】:
我正在使用 C# 执行 powershell 脚本,但是当我尝试在 powershell 脚本中获取环境变量时它不起作用。
如果我使用控制台手动运行 powershell 脚本,它可以工作。
您对如何解决此问题有任何想法吗?
C# 代码示例:
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
// set directory for ps
if (!string.IsNullOrEmpty(sessionPath))
runspace.SessionStateProxy.Path.SetLocation(sessionPath);
Pipeline pipeline = runspace.CreatePipeline();
Command command = new Command(script, false);
pipeline.Commands.Add(command);
pipeline.Commands.Add("Out-String");
pipeline.InvokeAsync();
脚本powershell:
$path = $env:PATH;
write-output $path # this is empty ! $env does seems to exist in the runspace ?
【问题讨论】:
-
我通过使用脚本 here 找到了一种解决方法,但我更愿意找到如何配置运行空间以便设置 Env 变量。
标签: c# powershell