Let us write a powershell custom Cmdlet …
Let us Write a Simple powershell Cmdlet.
The goal of this exercise is to understand minimum basics of writing a custom cmdlet. Thus as the tradition goes let us write a cmd-let which will print “Hello World”.
All it takes to write a cmdlet is
1. Write Code for Cmdlet functioning.
2. Write Code to Snap-In this Cmdlet.
3. Compile above piece of code.
4. Install Above Assembly.
5. Add PS Snap In to powershell
6. Run this Cmdlet.
1. Write Code for Cmdlet functioning.
/* A simple cmdlet */
using System;
using System.Management.Automation;
using System.IO;
[Cmdlet (VerbsCommunications.Write, "HelloWorld")]
public class PrintHelloCommand : Cmdlet
{
public PrintHelloCommand()
{
Console.WriteLine("Hello World");
}
}
Write this piece of code into a cs file. Let us call that cs file PrintHello.cs
A bit of explanation …
This is very much usual program. A simple Hello world program.
This doesn’t have Main, as for Cmdlets we don’t require Main, we will simply compile to dll (assembly) and follow other instructions to make this integrate and work with powershell.
One striking difference to our normal Hello World program is use of Cmdlet attribute
[Cmdlet (VerbsCommunications.Write, "HelloWorld")]
Read more about this CmdletAttribute at here.
This simply gives information about three things.
The class following this attribute contains code for a Cmdlet.
The cmd will be called with Verb-Noun combination. first parameter VerbsCommunications.Write is verb. We can simply give “Write” but using VerbsCommunications.Write we are telling PS that this is a commonly used verb. At many places it is strongly emphasized to use common verbs unless otherwise required. The second parameter is noun. We are calling it HelloWorld. Thus once everything is over our custom Cmd-let will be called Write-HelloWorld
2. Write Code to Snap-In this Cmdlet.
/* Snapin for Write-Hello cmdlet */
using System.Management.Automation;
using System.ComponentModel;
[RunInstaller (true)]
public class WriteHelloSnapin : PSSnapIn
{
///<summary>
///
///</summary>
public WriteHelloSnapin() : base()
{
}
/// <summary>
/// The snap in name that is used for registration
/// </summary>
public override string Name
{
get
{
return "WriteHelloSnapin" ;
}
}
/// <summary>
/// Get Vendor of snap in
/// </summary>
public override string Vendor
{
get
{
return "chavakiran - personal";
}
}
/// <summary>
/// Get Description of Snap in
/// </summary>
public override string Description
{
get
{
return "Simply prints Hello World";
}
}
}
“System.Management.Automation” – is the namespace which requires System.Management.Automation.dll this comes with powershell SDK or windows SDK installation.
All the properties above are very straight forward.
Just make a note of RunInstaller attribute, and PSSnapIn class derivation, and default constructor.
3. Compile above piece of code.
Let us use csc.exe.
I have this csc.exe on my machine. I think this comes with .NET framework SDK.
simply run csc.exe file1.cs file2.cs /out:WriteHello.dll /target:library /debug:full /warn:4 /utf8output /r:
file1.cs and file2.cs are the files we stored above pieces of code. We can even store them in single file.
4. Install Above Assembly.
We use InstallUtil.exe for installation.
I have this InstallUtil.exe on my machine
I think this also comes with .NET framework SDK. (or .NET Framework)
Simply Run InstallUtil.exe WriteHello.dll
5. Add PS Snap In to powershell
From the powershell run
PS:> Get-PSSnapin -reg Wri*
This will display our WriteHelloSnapin details.
PS:> Add-PSSnapin WriteHelloSnapin
Use above command to add our snapin to PS.
6. Run this Cmdlet.
Now we can simply write Write-HelloWorld which will print Hello World.
PS> Write-HelloWorld
Hello World
—–
References:
Extend Windows PowerShell With Custom Commands
Leave a Reply