小编alb*_*bal的帖子

从C#启用PowerShell的执行策略

我有一个MVC4页面,它调用了一个powershell.但是我遇到了问题,因为我使用的模块没有签名,所以我必须启用Unrestricted策略.如何强制powershell child使用Unrestricted Policy.我在我的脚本中启用了它,但它被忽略了.此外,当我尝试在代码中设置策略时,会抛出异常.

    using (Runspace myRunSpace = RunspaceFactory.CreateRunspace())
    {
        myRunSpace.Open();

        using (PowerShell powerShell = PowerShell.Create())
        {
            powerShell.Runspace = myRunSpace;
            powerShell.AddCommand("Set-ExecutionPolicy").AddArgument("Unrestricted");
            powerShell.AddScript(script);

            objectRetVal = powerShell.Invoke();
        }
    }
Run Code Online (Sandbox Code Playgroud)

谢谢,Al

c# powershell

1
推荐指数
3
解决办法
7468
查看次数

标签 统计

c# ×1

powershell ×1