Monday, December 10, 2012

Running PowerShell script from C# / VB.Net

Recently I had to take series of PowerShell sessions to one of the non-programmers team in my current organization. Actually its the build and installation team who wanted to automate their installation using PowerShell. It was a new experience to me as most of them are non programmers or haven't coded for years. So I started with very basics such as what is programming, conditional branching statements if ... else,switch case ,looping constructs such as while,do ... while, for, for each, functions, passing arguments to functions etc to creating and running dynamic scripts.Actually I would not have been chosen for teaching  basics of programming as I am working as Junior architect. By the by I got promoted as Junior Architect after the last performance review which is a rare designation with very less people around the world. Since the technology is not familiar to most of the programmers I had to be the trainer.

Whether I took session or not has nothing to do with this post. But there is a small relation. When I explained one of my .Net colleague about new PowerShell training to installation team and the power of PowerShell, he told me one statement. "Yes bringing PowerShell into installation is good so that, we can call that PowerShell code from our .Net code". I said "why we need to call PowerShell from .Net because PowerShell is from .Net and it uses .Net code to accomplish the tasks. Why should we go back again to C# .Net to call the script. We can execute the PS scripts directly like an exe". Then he told. "We are .Net devs and PS needs to be started from our code.What is stopping us?"

It was a partial joke .But I started thinking about it seriously .Will there be any benefits, if we call PowerShell from our code? It seems yes. We can leverage scripting to our application. But before that I need to confirm whether it is callable from C# / VB.Net code. Initial google gave me somewhat big code block which is   using RunSpace and all. But it seems we just need to create PowerShell object to execute simple scripts.

Adding reference to PowerShell assembly

You need to add reference to the below assembly to create the object of PowerShell class.

<InstallDrive>:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll

Invoke PowerShell script from C#

It is again a matter of calling the invoke method in PowerShell class. But be sure that you had added the script using the AddScript method available in the same class.

The RunPowerShellScript method


         private  static  IEnumerable  RunPowerShellScript(string  script)
         {
                 PowerShell  ps = PowerShell.Create();
                 ps.AddScript(script);
                 Collection <PSObject > result= ps.Invoke();
                 foreach  (PSObject  psObj in  result)
                 {
                     yield  return  (psObj);
                 }
         }
 
Simple isn't it.This uses the LocalRunspace to execute the script .ie the local machine where this C# .Net code is running

Calling RunPowerShellScript method


             foreach  (var  v in  RunPowerShellScript("get-alias" ))
                 Console .WriteLine(v);

I am looking for some practical scenarios where we can use this. Hopefully can find one soon.

No comments: