3
votes

Invoking powershell.

Here is the c# code:

        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();
                }
            }
        }

The above code working fine while running in console application but throws exception while running in mvc application.

While running the line powerShellInstance.Invoke(); exception thrown as following:

Message:

Security error.

Strack trace: at System.Management.Automation.Runspaces.AsyncResult.EndInvoke() at System.Management.Automation.PowerShell.CoreInvokeRemoteHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings) 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 output, PSInvocationSettings settings) at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings) at System.Management.Automation.PowerShell.Invoke() at Syncfusion.Cluster.Manager.Base.Azure.AzureVmAutomation.CreateAzureVirtualMachines(String resourceGroupName, String location, Int32 numberOfDataNode, String vMUserName, String vMPassWord, String namenodeSize, String datanodeSize, String adUserName, String adPassWord, String powerShellScriptPath, String subscriptionId, String azureClusterPassword, String logfilepath, String domainName, String azureImageVersion, Boolean reInstall) in e:\svn\datascience\trunk\AzureAutomation10\Base\AzureVmAutomation.cs:line 122 at Syncfusion.Cluster.Manager.Controllers.AzureController.<>c__DisplayClass1b.b__f() in e:\svn\datascience\trunk\AzureAutomation10\Controllers\AzureController.cs:line 330

1

1 Answers

0
votes

Setting the default execution policy requires elevation:

To change the execution policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option.

However depending on what you're trying to do, you may be able to set the scope of the policy via the ExecutionPolicyScope parameter. The available values are:

CurrentUser
LocalMachine
MachinePolicy
Process
UserPolicy