Friday, 14 October 2016

Require PowerShell Modules to Excute CmdLets in C#

Add the following Powershell library as a reference:
C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0

Import the following libraries in C#:
  • using System.Management.Automation;
  • using System.Management.Automation.Runspaces;


Create a C# method:
public void ExecuteScript(string fileName)
{
     try
     {
         string script = File.ReadAllText(fileName);
         var shell = PowerShell.Create();
         shell.Commands.AddScript(script);
         shell.Invoke();
     }
     catch (Exception) {
         // Exception Handling Code
     }
}


No comments:

SQL: Generate a range of numbers

SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n FROM       (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),      (VALU...