【问题标题】:Exception thrown as "Security error" while invoking powershell调用 powershell 时抛出的异常为“安全错误”
【发布时间】:2017-03-15 15:12:39
【问题描述】:

调用 powershell。

这里是c#代码:

        using (PowerShellProcessInstance instance = new PowerShellProcessInstance(new Version(4, 0), null, null, false))
        {
            using (var runspace = RunspaceFactory.CreateOutOfProcessRunspace(new TypeTable(new string[0]), instance))
            {
                runspace.Open();
                using (PowerShell powerShellInstance = PowerShell.Create())
                {
                    powerShellInstance.Runspace = runspace;
                    powerShellInstance.AddCommand("Set-ExecutionPolicy").AddArgument("Unrestricted");
                    powerShellInstance.Invoke();
                    var pipeLineDetail = new PipeLineDetail();
                    var addParametersToScriptFile = new Command(scriptFile);
                    addParametersToScriptFile.Parameters.Add(new CommandParameter("Parameter", parameter));
                    var pipeline = powerShellInstance.Runspace.CreatePipeline();
                    pipeline.Invoke();
                    powerShellInstance.Dispose();
                }
            }
        }

上述代码在控制台应用程序中运行正常,但在 mvc 应用程序中运行时抛出异常。

运行powerShellInstance.Invoke();行时抛出如下异常:

消息:

安全错误。

轨迹轨迹: 在 System.Management.Automation.Runspaces.AsyncResult.EndInvoke() 在 System.Management.Automation.PowerShell.CoreInvokeRemoteHelper[TInput,TOutput](PSDataCollection`1 输入,PSDataCollection`1 输出,PSInvocationSettings 设置) 在 System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 输入,PSDataCollection`1 输出,PSInvocationSettings 设置) 在 System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable 输入,PSDataCollection`1 输出,PSInvocationSettings 设置) 在 System.Management.Automation.PowerShell.Invoke(IEnumerable 输入,PSInvocationSettings 设置) 在 System.Management.Automation.PowerShell.Invoke() 在 Syncfusion.Cluster.Manager.Base.Azure.Azure.AzureVmAutomation.CreateAzureVirtualMachines(字符串 resourceGroupName,字符串位置,Int32 numberOfDataNode,字符串 vMUserName,字符串 vMPassWord,字符串 namenodeSize,字符串 datanodeSize,字符串 adUserName,字符串 adPassWord,字符串 powerShellScriptPath,字符串 subscriptionId,字符串 azureClusterPassword , 字符串 logfilepath, 字符串 domainName, 字符串 azureImageVersion, Boolean reInstall) 在 e:\svn\datascience\trunk\AzureAutomation10\Base\AzureVmAutomation.cs:line 122 在 Syncfusion.Cluster.Manager.Controllers.AzureController.c__DisplayClass1b.b__f() 在 e:\svn\datascience\trunk\AzureAutomation10\Controllers\AzureController.cs:line 330

【问题讨论】:

    标签: c# .net powershell


    【解决方案1】:

    设置默认执行策略requires elevation

    要更改默认 (LocalMachine) 范围的执行策略,请使用“以管理员身份运行”选项启动 Windows PowerShell。

    但是,根据您要执行的操作,您可以通过 ExecutionPolicyScope 参数设置策略范围。可用的值为:

    当前用户
    本地机器
    机器策略
    工艺
    用户策略

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多