简体   繁体   中英

How does a C# runspace/pipeline execute a powershell script?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM