調用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