Sunday, July 29, 2018

How to execute PowerShell script or cmdlets from C# code?


Sometimes you need to execute PowerShell script or commands from C# code. In this article I’ll explain various methods to execute PowerShell scripts and commands from C# code.

You can execute PowerShell scripts using PowerShell object available in ‘System.Management.Automation’ namespace. This assembly is available in Nuget for download. You can create PowerShell instance and assign script file or command which you would like to execute. You can also get output from PowerShell command after execution and read data from PSObject object.

Let’s have look on below example which executes PowerShell scripts from C# code.

Code –

using System;
using System.Management.Automation;
using System.Collections.ObjectModel;

static void Main(string[] args)
{
    using (PowerShell PowerShellInst = PowerShell.Create())
    {
        string criteria = "system*";
        PowerShellInst.AddScript("Get-Service -DisplayName " + criteria);
        Collection<PSObject> PSOutput = PowerShellInst.Invoke();
        foreach (PSObject obj in PSOutput)
        {
            if (obj != null)
            {
                Console.Write(obj.Properties["Status"].Value.ToString() + " - ");
                Console.WriteLine(obj.Properties["DisplayName"].Value.ToString());
            }
        }
        Console.WriteLine("Done");
        Console.Read();
    }
}

Output –


Now I want to execute below PowerShell script file using C# code.
Code –

using System;
using System.Management.Automation;
using System.Collections.ObjectModel;

static void Main(string[] args)
{
    //Execute PS1 (PowerShell script) file
    using (PowerShell PowerShellInst = PowerShell.Create())
    {
        string path = System.IO.Path.GetDirectoryName(@"C:\Temp\") + "\\Get-EventLog.ps1";
        if (!string.IsNullOrEmpty(path))
            PowerShellInst.AddScript(System.IO.File.ReadAllText(path));

        Collection<PSObject> PSOutput = PowerShellInst.Invoke();
        foreach (PSObject obj in PSOutput)
        {
            if (obj != null)
            {
                Console.Write(obj.Properties["EntryType"].Value.ToString() + " - ");
                Console.Write(obj.Properties["Source"].Value.ToString() + " - ");
                Console.WriteLine(obj.Properties["Message"].Value.ToString() + " - ");
            }
        }
        Console.WriteLine("Done");
        Console.Read();
    }
}

Output –

Command Prompt - You can execute PowerShell scripts and command using PowerShell.exe like below in command prompt.

Similarly you can invoke PowerShell process from C# code and pass command as argument. See below example how execute PowerShell process from code and pass cmdlet as argument.

Code –

using System;
using System.Management.Automation;
using System.Collections.ObjectModel;

static void Main(string[] args)
{
    //execute powershell cmdlets or scripts using command arguments as process
    ProcessStartInfo processInfo = new ProcessStartInfo();
    processInfo.FileName = @"powershell.exe";
    //execute powershell script using script file
    //processInfo.Arguments = @"& {c:\temp\Get-EventLog.ps1}";
    //execute powershell command
    processInfo.Arguments = @"& {Get-EventLog -LogName Application -Newest 10 -EntryType Information | Select EntryType, Message}";
    processInfo.RedirectStandardError = true;
    processInfo.RedirectStandardOutput = true;
    processInfo.UseShellExecute = false;
    processInfo.CreateNoWindow = true;

    //start powershell process using process start info
    Process process = new Process();
    process.StartInfo = processInfo;
    process.Start();

    //read output
    Console.WriteLine("Output - {0}", process.StandardOutput.ReadToEnd());
    //read errors
    Console.WriteLine("Errors - {0}", process.StandardError.ReadToEnd());
    Console.Read();
}

Output –

You can download code from Gist.

Thank you for reading this article. Please leave your feedback in comments below.

Reference –

See Also –

Sunday, July 8, 2018

State Pattern


State design pattern is used to modify behavior of an object whenever internal state changes. State Pattern falls under behavioral pattern of GOF (Gang of Four) patterns.

When to use –


State pattern used when state of an object changes based on some action or condition at run-time without changing the interface. State pattern used to alter the behavior of an object as its internal state changes. This pattern used for some complex decision making program which represents multiple states.

Major components of State pattern –


Context – The clients of the State design pattern use the context class directly. Clients do not have access to state objects. Context class holds State objects that changes behavior according to its state.
State – This is an abstract class.
Concrete State – This class inherited from State class. This class provides real functionality that used by Context object. This class provides behavior to check and change state based on condition/action.

Let’s have a look on below example of State design pattern.

Code –
//state
public abstract class State
{
    public BankAccount Account { get; set; }
    public double Balance { get; set; }

    public abstract void Deposit(double amount);
    public abstract void Withdraw(double amount);
}
//concrete state
public class Normal : State
{
    public Normal(State state)
    {
        Balance = state.Balance;
        Account = state.Account;
    }
    public Normal(double balance, BankAccount account)
    {
        Balance = balance;
        Account = account;
    }
    public override void Deposit(double amount)
    {
        Balance += amount;
        CheckState();
    }

    public override void Withdraw(double amount)
    {
        Balance -= amount;
        CheckState();
    }
    void CheckState()
    {
        if (Balance > 1000)
            Account.State = new Classic(this);
    }
}
//concrete state
public class Classic : State
{
    public Classic(State state)
    {
        Balance = state.Balance;
        Account = state.Account;
    }
    public Classic(double balance, BankAccount account)
    {
        Balance = balance;
        Account = account;
    }
    public override void Deposit(double amount)
    {
        Balance += amount;
        CheckState();
    }
    public override void Withdraw(double amount)
    {
        Balance -= amount;
        CheckState();
    }
    void CheckState()
    {
        if (Balance < 1000)
            Account.State = new Normal(this);
        else if (Balance > 2000)
            Account.State = new Platinum(this);
    }
}
//concrete state
public class Platinum : State
{
    public Platinum(State state)
    {
        Balance = state.Balance;
        Account = state.Account;
    }
    public Platinum(double balance, BankAccount account)
    {
        Balance = balance;
        Account = account;
    }
    public override void Deposit(double amount)
    {
        Balance += amount;
        CheckState();
    }
    public override void Withdraw(double amount)
    {
        Balance -= amount;
        CheckState();
    }
    void CheckState()
    {
        if (Balance < 2000)
            Account.State = new Classic(this);
        else if (Balance < 1000)
            Account.State = new Normal(this);
    }
}
//context
public class BankAccount
{
    public State State { get; set; }
    public string Name { get; set; }
    public BankAccount(string name)
    {
        Name = name;
        State = new Normal(0, this);
    }
    public double Balance
    {
        get { return State.Balance; }
    }
    public void Deposit(double amount)
    {
        State.Deposit(amount);
        Console.WriteLine("Deposited - {0}", amount);
        Console.WriteLine("Balance - {0}", Balance);
        Console.WriteLine("Account Status - {0}", State.GetType().Name);
        Console.WriteLine(new string('-', 50));
    }
    public void Withdraw(double amount)
    {
        State.Withdraw(amount);
        Console.WriteLine("Withdrawn - {0}", amount);
        Console.WriteLine("Balance - {0}", Balance);
        Console.WriteLine("Account Status - {0}", State.GetType().Name);
        Console.WriteLine(new string('-', 50));
    }
}
class Program
{
    //entry point
    static void Main(string[] args)
    {
        BankAccount account = new BankAccount("Mitesh Sureja");
        account.Deposit(500);
        account.Deposit(600);
        account.Deposit(1000);
        account.Withdraw(500);
        account.Withdraw(1500);
        Console.Read();
    }

}

Output –


Above example demonstrates change in bank account status based on its remaining balance. In this example, BankAccount class is context class and Normal, Classic, Platinum are various classes of concrete state. The CheckState method of concrete state classes responsible to check condition and change states accordingly on each action like Deposit or Withdraw. Based on available balance in bank account, the status automatically changes between Normal, Classic and Platinum.


You can download full code from Gist.

I hope this article helps you to know more about State Design Pattern. Please leave your feedback in comments section below.

References –

See Also –