2. Agenda
2
• Writing Custom CmdLets
• Installing Custom CmdLets
• UnInstalling Custom CmdLets
• PowerShell for .Net 4.0
• PowerShell CmdLets for Windows Azure
3. Writing Customized CmdLets
Modules
A module is a package of commands and
other items that you can use in Windows
PowerShell. After you run the setup
program or save the module to disk, you
can import the module into your Windows
PowerShell session and use the
commands and items. You can also use
modules to organize the cmdlets,
providers, functions, aliases, and other
commands that you create, and share
them with others.
SnapIn
A Windows PowerShell snap-in
(PSSnapin) is a dynamic link library (.dll)
that implements cmdlets and providers.
When you receive a snap-in, you need to
install it, and then you can add the cmdlets
and providers in the snap-in to your
Windows PowerShell session.
3
Windows PowerShell is a fully extensible environment. Anyone can write commands for PowerShell.
You can use commands that others write and share the commands that you write with others.
Commands are shared by using modules and snap-ins. Windows PowerShell modules and snap-ins
are packages that contain Windows PowerShell commands and other items.
4. Step 1: Load new Visual studio project and select PowerShellCmdLet template.
4
PowerShellCmdLet template and be downloaded from http://psvs2008.codeplex.com/
This template is in VS2008, However you can use the same template in VS2010 also.
5. Step 2: Add NewItem, Select PowerShell PSCmdLet template. Name it as “Get-Process”
5
6. Step 3: Replace the GetProcess.cs file with the below code
6
using System;
Using System.Management.Automation;
namespace DemoSnapIn
{
[Cmdlet(VerbsCommon.Get, "Proc")]
public class GetProcess : PSCmdlet
{
protected override void ProcessRecord()
{
try
{
WriteObject(System.Diagnostics.Process.GetProcesses(), true);
}
catch (Exception)
{
}
}
}
}
System.Management.Automation is the root namespace for Windows PowerShell. It contains classes, enumerations,
and interfaces
7. Step 4: Add another PowerShell PSCmdLet template and name it as “CustomProcess.cs”
7
8. Step 5: Replace the CustomProcess.cs file with the below code and build the source.
8
using System.ComponentModel;
using System.Management.Automation;
namespace DemoSnapIn
{
[RunInstaller(true)]
public class CustomProcess : PSSnapIn
{
public override string Description
{
get { return "Custom Command Lets"; }
}
public override string Name
{
get { return "DemoSnapIn"; }
}
public override string Vendor
{
get { return "Aditi Technology"; }
}
}
}
Specifies whether the Visual Studio Custom Action Installer or the Installutil.exe (Installer Tool) should be
invoked when the assembly is installed
9. Installing Custom CmdLets
• Execute the below commands in Powershell window
Start All Programs Accessories Windows PowerShell Windows PowerShell.exe
1. set-alias installutil $env:windirMicrosoft.NETFramework64v4.0.30319installutil
2. installutil "D:SaravananSample SRCMyDemoPowerShellDemoSnapInDemoSnapInbinDebugDemoSnapIn.dll"
3. Get-PSSnapIn –registered
4. Add-PSSnapIn DemoSnapIn
5. Get-Command -pssnapin DemoSnapIn
6. Get-Proc This will list all the process running in the current system.
9
10. UnInstalling Custom CmdLets
• Execute the below commands in Powershell window
Start All Programs Accessories Windows PowerShell Windows PowerShell.exe
1. set-alias installutil $env:windirMicrosoft.NETFramework64v4.0.30319installutil
2. $path = "D:SaravananSample SRCMyDemoPowerShellDemoSnapInDemoSnapInbinDebugDemoSnapIn.dll"
3. installutil -u $path.
4. Get-PSSnapIn –registered
DemoSnapIn has been
uninstalled
10
11. Configure PowerShell to use DotNet 4.0
You might get following error when you load a snap-in that is written by DotNet 4.0.
This is because by default, PowerShell uses DotNet version 2.0 CLR. To use powershell load DotNet 4.0 assemblies,
the following settings need to be added in PowerShell.exe.config under
C:WINDOWSsystem32WindowsPowerShellv1.0.
• <configuration>
• <startup useLegacyV2RuntimeActivationPolicy="true">
• <supportedRuntime version="v4.0.30319"/>
• <supportedRuntime version="v2.0.50727"/>
• </startup>
• </configuration>
Note: Create PowerShell.exe.config if this file not found.
11
12. PowerShell Cmdlets for Windows Azure
Windows azure cmdlets can be downloaded from the below link
http://wappowershell.codeplex.com/
A few Windows azure cmdlets
12
15. 15
References
Windows PowerShell Web site
PowerShell Features
TechNet Script Center Repository for PowerShell
PowerShell Tips
A-Z PowerShell 2.0 Commands
Windows PowerShell Cmdlet Help Topics
Windows PowerShell Quick reference
PowerShell Owner's Manual
PowerShell Tutorial
PowerShell.com
http://msdn.microsoft.com/en-us/library/ms714658.aspx
http://blogs.technet.com/b/heyscriptingguy/
http://en.wikipedia.org/wiki/Windows_PowerShell#Examples