如何防止PowerShell更改环境变量?

时间:2019-01-14 16:06:12

标签: c# powershell

以下测试失败,因为PowerShell对象更改了调用者进程的路径:

using System;
using System.IO;
using System.Linq;
using System.Management.Automation;
using Microsoft.VisualStudio.TestTools.UnitTesting;


namespace Helpers.Tests.ShellHelper {
    [TestClass]
    public class PowerShellEnvAlteration_Tests {
        [TestMethod]
        public void TestPath() {
            var searchTarget = @"C:\LandingZone";

            using (PowerShell powerShell = PowerShell.Create()) {
                powerShell.Runspace.SessionStateProxy.SetVariable("env:Path",
                    $"{searchTarget}{Path.PathSeparator}{Environment.GetEnvironmentVariable("PATH")}");
            }

            var pathDirs = Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator);
            Assert.IsFalse(pathDirs.Contains(searchTarget));
        }
    }
}

如何预防?是否可以完全隔离此PowerShell对象/执行?

1 个答案:

答案 0 :(得分:1)

PetSerAl在评论中提供了关键指针:

由于环境变量本质上是[整个] 进程范围,因此您需要进程外运行空间< / strong>以获得所需的行为。

相比之下,PowerShell.Create()本身没有通过结果实例的.Runspace属性来显式分配运行空间,默认为进程内运行空间,并修改了环境该运行空间中的变量,也同样会影响在同一进程中运行的调用方。

要修改代码以使用进程外运行空间,请执行以下操作:

// ...
using System.Management.Automation.Runspaces;

// ...

// Create an out-of-process runspace...
using (var runspace = RunspaceFactory.CreateOutOfProcessRunspace(null))
{
  runspace.Open(); // ... open it ...
  using (PowerShell powerShell = PowerShell.Create())
  {
    powerShell.Runspace = runspace; // ... and assign it to the PowerShell instance.

    // Now setting $env:PATH only takes effect for the separate process
    // in which the runspace is hosted.
    // Note: `powerShell.Runspace.SessionStateProxy.SetVariable("env:Path", ...)` does 
    // does NOT work with OUT-OF-PROCESS runspaces, so a call to
    // `Set-Item env:PATH ...` is used to modify the other process' PATH env. var.
    // (Environment.SetEnvironmentVariable() is NOT an option, because
    //  it would modify the *calling* process' environment).
    powerShell.AddCommand("Set-Item")
      .AddParameter("LiteralPath", "env:Path")
      .AddParameter("Value", $"{searchTarget}{Path.PathSeparator}{Environment.GetEnvironmentVariable("Path")}")
      .Invoke();
    powerShell.Commands.Clear();

    // ...

  }

}

注意:上面使用了对Set-Item env:Path ...的调用,以便在进程外运行空间中修改$env:PATH,因为正如PetSerAl指出的,与 in-process < / em>运行空间,使用powerShell.Runspace.SessionStateProxy.SetVariable("env:Path", ...)会创建一个字面名为env:Path PowerShell 变量,而不是在Windows上修改 environment 变量PATH PowerShell v5.1 / PowerShell Core 6.2.0-preview.3;参见this GitHub issue