1

I need to execute a powershell script from my asp.net MVC Web application. My requirement is to create site collections dynamically. I have the script for it and it works perfectly.There are no arguments which are to be passed to the script. The code which I have been using has been displayed below:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();

RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);

Pipeline pipeline = runspace.CreatePipeline();

//Here's how you add a new script with arguments
Command myCommand = new Command(scriptfiellocation);

pipeline.Commands.Add(myCommand);
pipeline.Commands.Add("Out-String");

// Execute PowerShell script
var result = pipeline.Invoke();

On executing the code, when I check the count of variable result it gives the count as 1. However on checking my site, there is no site collection that has been created. I am not able to identify where I am going wrong as there is no run time error and the Invoke command also seems to be running properly.

Could anyone tell me where I might be going haywire ? Considering that the PowerShell script works perfectly when running through Management shell.

2
  • Could be a permissions issue. Does the account the site is running under have the required permissions? Check the pipeline's state after the Invoke operation. Commented Dec 16, 2015 at 14:00
  • The pipeline state appears as Completed once the operation is over. And yes the account has the required permissions as well. Any idea on what else could be causing the issue ? Commented Dec 16, 2015 at 14:36

2 Answers 2

3

I had to forego the pipeline approach as I was not able to figure out what the issue was. Also another problem with that approach is that it threw the error: "Get-SPWbTemplate is not recognized as an cmdlet". The following code worked perfectly fine for me and created the required site collections:

 PowerShell powershell = PowerShell.Create();
                    //RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
                    //Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration)
                    using (Runspace runspace = RunspaceFactory.CreateRunspace())
                    {
                        runspace.Open();
                        //RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
                        //scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
                        powershell.Runspace = runspace;
                       //powershell.Commands.AddScript("Add-PsSnapin Microsoft.SharePoint.PowerShell");
                        System.IO.StreamReader sr = new System.IO.StreamReader(scriptfilepath);
                        powershell.AddScript(sr.ReadToEnd());
                        //powershell.AddCommand("Out-String");
                        var results = powershell.Invoke();
                        if (powershell.Streams.Error.Count > 0)
                        {
                            // error records were written to the error stream.
                            // do something with the items found.
                        }
                    }

Also there was no requirement to set the execution policy.

Sign up to request clarification or add additional context in comments.

Comments

0

well don't know if its help but i never use pipeline to run Command shell not sure how that work.

But here a quick example

    Runspace RS = RunspaceFactory.CreateRunspace(myConnection);
    PowerShell PS = PowerShell.Create();

      PSCommand PScmd = new PSCommand();
        string cmdStr = "Enable-Mailbox -Identity " + username + " -Database DB01 -Alias " + aliasexample;
        PScmd.AddScript(cmdStr);

    try
    {
        RS.Open();
        PS.Runspace = RS;
        PS.Commands = PScmd;
        PS.Invoke();
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
    finally
    {
        RS.Dispose();
        RS = null;
        PS.Dispose();
        PS = null;
    }

with the try catch you can catch the error with debugging if something goes wrong. If i remember correctly i had to put ACL.exe for permission to file system so i can execute the commandshell you can do a quick search on google for it. Hope this help.

2 Comments

I did use the try catch block. However it never enters the catch block. Thus I am unable to figure out where it goes wrong!!!
like i say not sure how to use Pipeline so i can't tell you about that but by inspecting the class you might do this var error = pipeline.Error.Read() as Collection<ErrorRecord> than verify if its null then iterate over it to check error if not maybe something relate to permission or missing some file in your system like me i use Acl on my host to run powershell command permission. It can be tricky to do command shell in web app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.