【发布时间】: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