抛出的异常为“安全错误”;调用powershell时
本文关键字:安全错误 调用 powershell 错误 安全 异常 | 更新日期: 2023-09-27 18:16:07
调用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[input, TOutput](PSDataCollection ' 1输入,PSDataCollection ' 1输出,PSInvocationSettings设置)在System.Management.Automation.PowerShell。CoreInvoke[input, TOutput](PSDataCollection ' 1输入,PSDataCollection ' 1输出,PSInvocationSettings设置)在System.Management.Automation.PowerShell。CoreInvoke[TOutput](IEnumerable输入,PSDataCollection ' 1输出,PSInvocationSettings设置)在System.Management.Automation.PowerShell。调用(IEnumerable输入,PSInvocationSettings设置)在System.Management.Automation.PowerShell.Invoke ()在Syncfusion.Cluster.Manager.Base.Azure.AzureVmAutomation。CreateAzureVirtualMachines(字符串resourceGroupName,字符串位置,Int32 numberOfDataNode,字符串vMUserName,字符串vMPassWord,字符串namenodeSize,字符串datanodeSize,字符串adUserName,字符串adPassWord,字符串powerShellScriptPath,字符串subscriptionId,字符串azureClusterPassword,字符串logfilepath,字符串domainName,字符串azureImageVersion, Boolean重新安装)在e:'svn'datascience'trunk'AzureAutomation10'Base'AzureVmAutomation.cs: 122行at Syncfusion.Cluster.Manager.Controllers.AzureController.<>c__DisplayClass1b.b__f() ine:'svn'datascience'trunk'AzureAutomation10'Controllers'AzureController.cs:line 330
设置默认执行策略需要提升:
要更改默认(LocalMachine)范围的执行策略,请启动Windows PowerShell,并设置"以管理员身份运行"选项。
但是,根据您想要做的事情,您可能能够通过ExecutionPolicyScope参数设置策略的范围。取值为:
CurrentUser
LocalMachine
MachinePolicy
流程
UserPolicy