AddPsSnapin的替代方案,用于在运行空间

时间:2016-12-21 16:06:11

标签: c# powershell

我正在尝试测试C#Cmdlet与PowerShell管道的交互。

当我尝试注册Cmdlet的管理单元时,出现错误(No snap-ins have been registered for Windows PowerShell version 4.)。我怀疑这是因为我的Windows帐户无权注册管理单元。

代码:

using (Runspace runspace = RunspaceFactory.CreateRunspace(config))
{
    PSSnapInException warning;

    // error generated on this line
    config.AddPSSnapIn("PsEnterprise.PsEnterpriseSnapIn", out warning);

    runspace.Open();

    using (Pipeline pipeline = runspace.CreatePipeline())
    {

        // arrange
        Command command = new Command("New-PsDrive");
        command.Parameters.Add("Name", "DEV");
        command.Parameters.Add("PsProvider", "MyProvider");
        command.Parameters.Add("Root", "DEV:\\");

        pipeline.Commands.Add(command);

        // act
        Collection<PSObject> actual = pipeline.Invoke();

        // assert
        Assert.AreEqual<int>(1, actual.Count);
        Assert.IsNotNull(actual[0]);
        Assert.IsInstanceOfType(actual, typeof(MyDriveInfo));

    } // Pipeline
} // Runspace

是否有其他方法可以引用Cmdlet以便以这种方式进行测试?

0 个答案:

没有答案