7

There are so many questions and answers about how to execute a powershell script from C# code, but I would like to know how exactly the runspace/pipeline performs the execution of the .ps1 file.

The context in which I am asking is this: I've built a basic asp.net front end for a powershell script that sets up a new website. It creates the site in IIS and sets up wwwroot folders, log folders, an AD user and everything else that it needs. It works perfectly when I run it as an admin in powershell. However, when I run it through my C# pipeline, it breaks the pipe with a parameter binding exception.

I'm not after an answer to this specific problem, I'm just looking for an explanation of how the pipeline executes my file so that I can have a better understanding of what exactly is going on.

2 Answers 2

4

It's not a complete explanation of how the runspace works, but I've written a blog post covering the main bits.

Also a snippet from one of the msdn pages on how powershell works:

The Powershell runtime is invoked as a runspace and will, by default, process commands synchronously and deliver the results of processing to the host application so that they can be returned to the user.

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

Comments

1

Have you tried using ILSpy or opening the class in Class View to see the code for the class? I do this all the time to see how Microsoft implements a particular class. I would paste the code here if it was a short answer, but there is a lot to the System.Management.Automation.Runspaces or Microsoft.Powershell namespaces. Take a look for yourself, and you will be able to see exactly how Microsoft implements the execution of Powershell scripts.

Hope this helps.

1 Comment

Thanks Adam, I gave this a go but it didn't provide any extra information on how the file gets executed. Most of the methods were along these lines: public Collection<PSObject> Invoke() { using (Pipeline._trace.TraceMethod()) return this.Invoke((IEnumerable) null); } public abstract Collection<PSObject> Invoke(IEnumerable input);

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.