2016-11-02 74 views
3

調用powershell。在調用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();拋出異常如下:

消息:

安全錯誤。

Strack的跟蹤: 在System.Management.Automation.Runspaces.AsyncResult.EndInvoke() 在System.Management.Automation.PowerShell.CoreInvokeRemoteHelper [TInput,TOutput](PSDataCollection`1輸入,輸出PSDataCollection`1,PSInvocationSettings設置) at System.Management.Automation.PowerShell.CoreInvoke [TInput,TOutput](PSDataCollection`1 input,PSDataCollection`1 output,PSInvocationSettings settings) at System.Management.Automation.PowerShell.CoreInvoke [TOutput](IEnumerable input, PSDataCollection`1輸出,PSInvocationSettings設置) at System.Management.Automation.PowerShell.Invoke(IEnumerable input,PSInvocationSettings settings) at System.Management.Automation.PowerShell.Inv OKE() 在Syncfusion.Cluster.Manager.Base.Azure.AzureVmAutomation.CreateAzureVirtualMachines(字符串resourceGroupName,字符串位置,的Int32 numberOfDataNode,字符串vMUserName,字符串vMPassWord,字符串namenodeSize,字符串datanodeSize,字符串adUserName,字符串adPassWord,字符串powerShellScriptPath,字符串字符串azureClusterPassword,字符串日誌文件路徑,字符串域名,字符串azureImageVersion,布爾reInstall)在e:\ svn \ datascience \ trunk \ AzureAutomation10 \ Base \ AzureVmAutomation.cs:行122 在Syncfusion.Cluster.Manager.Controllers.AzureController。 <> c__DisplayClass1b.b__f()在E:\ SVN \數據科學\幹線\ AzureAutomation10 \ \控制器AzureController.cs:行330

回答

0

設置默認的執行策略requires elevation

要改變執行默認(LocalMachine)作用域的策略,使用「以管理員身份運行」選項啓動Windows PowerShell。

但是,根據您要做的事情,您可以通過參數ExecutionPolicyScope來設置策略的範圍。可用的值有:

CurrentUser
LOCALMACHINE
MachinePolicy
過程
UserPolicy

相關問題