Click to Rate and Give Feedback
MSDN
MSDN Library
Windows PowerShell
Registering Cmdlets
 Writing a Windows PowerShell Snap-i...
Writing a Windows PowerShell Snap-in

This example shows how to write a Windows Powershell snap-in that can be used to register all the cmdlets and Windows PowerShell providers in an assembly.

With this type of snap-in, you do not select which cmdlets and providers you want to register. To write a snap-in that allows you to select what is registered, see Writing a Custom Windows PowerShell Snap-in.

Writing a Windows PowerShell Snap-in

  1. Add the RunInstallerAttribute attribute.

  2. Create a public class that derives from the PSSnapIn class.

    In this example, the class name is "GetProcPSSnapIn01".

  3. Add a public property for the name of the snap-in (required). When naming snap-ins, do not use any of the following characters: # . , ( ) { } [ ] & - /\ $ ; : ” ’ < > | ? @ ` *

    In this example, the name of the snap-in is "GetProcPSSnapIn01".

  4. Add a public property for the vendor of the snap-in (required).

    In this example, the vendor is "Microsoft".

  5. Add a public property for the vendor resource of the snap-in (optional).

    In this example, the vendor resource is "GetProcPSSnapIn01,Microsoft".

  6. Add a public property for the description of the snap-in (required).

    In this example, the description is "This is a Windows PowerShell snap-in that registers the get-proc cmdlet".

  7. Add a public property for the description resource of the snap-in (optional).

    In this example, the vendor resource is "GetProcPSSnapIn01,This is a Windows PowerShell snap-in that registers the get-proc cmdlet".

Example

This example shows how to write a Windows Powershell snap-in that can be used to register the Get-Proc cmdlet in the Windows PowerShell shell. Be aware that in this example, the complete assembly would contain only the GetProcPSSnapIn01 snap-in class and the Get-Proc cmdlet class.

C#
[RunInstaller(true)]
public class GetProcPSSnapIn01 : PSSnapIn
{
  /// <summary>
  /// Create an instance of the GetProcPSSnapIn01 class.
  /// </summary>
  public GetProcPSSnapIn01()
         : base()
  {
  }

  /// <summary>
  /// Specify the name of the PowerShell snap-in.
  /// </summary>
  public override string Name
  {
    get
    {
      return "GetProcPSSnapIn01";
    }
  }

  /// <summary>
  /// Specify the vendor for the PowerShell snap-in.
  /// </summary>
  public override string Vendor
  {
    get
    {
      return "Microsoft";
    }
  }

  /// <summary>
  /// Specify the localization resource information for the vendor. 
  /// Use the format: resourceBaseName,VendorName. 
  /// </summary>
  public override string VendorResource
  {
    get
    {
      return "GetProcPSSnapIn01,Microsoft";
    }
  }

  /// <summary>
  /// Specify a description of the PowerShell snap-in.
  /// </summary>
  public override string Description
  {
    get
    {
      return "This is a PowerShell snap-in that includes the get-proc cmdlet.";
    }
  }

  /// <summary>
  /// Specify the localization resource information for the description. 
  /// Use the format: resourceBaseName,Description. 
  /// </summary>
  public override string DescriptionResource
  {
    get
    {
      return "GetProcPSSnapIn01,This is a PowerShell snap-in that includes the get-proc cmdlet.";
    }
  }
}

See Also



Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
What is DescriptionResource?      johnls ... abdullah ahmad almalki   |   Edit   |   Show History

It's not clear why the above example doesn't just have DescriptionResource defined as:

return "GetProcPSSnapIn01," + Description;

which works just fine.

Not a valid C# file....      JeremyMc ... abdullah ahmad almalki   |   Edit   |   Show History
From this google group post: http://groups.google.com/group/microsoft.public.windows.powershell/browse_thread/thread/725962ef855ca921/0198ad905e7b3268#0198ad905e7b3268

junker37 wrote:
> I'm trying to compile a PSSnapIn using these directions:
> http://msdn2.microsoft.com/en-us/library/ms714644(VS.85).aspx

> I've taken the PSSnapIn found here: http://msdn2.microsoft.com/en-us/library/ms714453.aspx
> and named it GetProcSample.cs

The example is missing a few things to be valid C# source code (and to
actually compile):

At the start:
using System;
using System.Management.Automation;
using System.ComponentModel;
namespace foo_test <--Whatever you want
{

At the end:

}

That should get you going...

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com


Updated C# Sample      Thomas Lee ... abdullah ahmad almalki   |   Edit   |   Show History

// Snap-in.cs
// Creates a simple snampin.
// Updated sample for MSDN Library
// Corrected C# Sample for http://msdn2.microsoft.com/en-us/library/ms714453.aspx
// This sample compiles!

// updated by: thomas lee - tfl@psp.co.uk
//


using System;
using System.Management.Automation;
using System.ComponentModel;

namespace PSSnapIn01.SAMPLE

{

[RunInstaller(true)]
public class GetProcPSSnapIn01 : PSSnapIn
{
/// <summary>
/// Create an instance of the GetProcPSSnapIn01 class.
/// </summary>
public GetProcPSSnapIn01()
: base()
{
}

/// <summary>
/// Specify the name of the PowerShell snap-in.
/// </summary>
public override string Name
{
get
{
return "GetProcPSSnapIn01";
}
}

/// <summary>
/// Specify the vendor for the PowerShell snap-in.
/// </summary>
public override string Vendor
{
get
{
return "Microsoft";
}
}

/// <summary>
/// Specify the localization resource information for the vendor.
/// Use the format: resourceBaseName,VendorName.
/// </summary>
public override string VendorResource
{
get
{
return "GetProcPSSnapIn01,Microsoft";
}
}

/// <summary>
/// Specify a description of the PowerShell snap-in.
/// </summary>
public override string Description
{
get
{
return "This is a PowerShell snap-in that includes the get-proc cmdlet.";
}
}

/// <summary>
/// Specify the localization resource information for the description.
/// Use the format: resourceBaseName,Description.
/// </summary>
public override string DescriptionResource
{
get
{
return "GetProcPSSnapIn01,This is a PowerShell snap-in that includes the get-proc cmdlet.";
}
}
}

}
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker