カスタム Windows PowerShell スナップインを記述する

この例では、特定のコマンドレットを登録するWindows PowerShellを作成する方法を示します。

この種類のスナップインでは、登録するコマンドレット、プロバイダー、型、または形式を指定します。 アセンブリ内のすべてのコマンドレットとプロバイダーを登録するスナップインを作成する方法の詳細については、「Windows PowerShell スナップインの作成」を参照してください

特定のコマンドレットWindows PowerShellを登録するスナップインを作成するには

  1. RunInstallerAttribute 属性を追加します。

  2. System.Management.Automation.Custompssnapin クラスから派生するパブリック クラスを作成します。

    この例では、クラス名は "CustomPSSnapinTest" です。

  3. スナップインの名前のパブリック プロパティを追加します (必須)。 スナップインに名前を付ける場合は、 # . , ( ) { } [ ] & - / \ $ ; : " ' < > | ? @ `` ```*

    この例では、スナップインの名前は "CustomPSSnapInTest" です。

  4. スナップインのベンダーのパブリック プロパティを追加します (必須)。

    この例では、ベンダーは "Microsoft" です。

  5. スナップインのベンダー リソースのパブリック プロパティを追加します (省略可能)。

    この例では、ベンダー リソースは "CustomPSSnapInTest,Microsoft" です。

  6. スナップインの説明のパブリック プロパティを追加します (必須)。

    この例の説明は次のとおりです。"これは、 コマンドレットと コマンドレットWindows PowerShellを含むカスタム のスナップイン Test-HelloWorld Test-CustomSnapinTest です。

  7. スナップインの説明リソースのパブリック プロパティを追加します (省略可能)。

    この例では、ベンダー リソースは次のようになります。

    CustomPSSnapInTest: これは、Windows PowerShellコマンドレットとコマンドレットを含むカスタム Test-HelloWorldスナップTest-CustomSnapinTestです。

  8. System.Management.Automation.Runspaces.Cmdletconfigurationentryクラスを使用して、カスタム スナップイン (省略可能) に属するコマンドレットを指定します。 ここで追加する情報には、コマンドレットの名前、その .NET 型、およびコマンドレットのヘルプ ファイル名 (コマンドレットのヘルプ ファイル名の形式は である必要があります) が含まれます name.dll-help.xml

    この例では、Test-HelloWorld TestCustomSnapinTest コマンドレットを追加します。

  9. カスタム スナップインに属するプロバイダーを指定します (省略可能)。

    この例では、プロバイダーを指定する必要があります。

  10. カスタム スナップインに属する型を指定します (省略可能)。

    この例では、型は指定できません。

  11. カスタム スナップインに属する形式を指定します (省略可能)。

    この例では、形式は指定ありません。

この例では、 および コマンドレットの登録に使用Windows PowerShellカスタム カスタム スナップショット スナップインを作成する Test-HelloWorld 方法 Test-CustomSnapinTest を示します。 この例では、完全なアセンブリに、このスナップインで登録されない他のコマンドレットとプロバイダーが含まれている可能性があります。

[RunInstaller(true)]
public class CustomPSSnapinTest : CustomPSSnapIn
{
  /// <summary>
  /// Creates an instance of CustomPSSnapInTest class.
  /// </summary>
  public CustomPSSnapinTest()
          : base()
  {
  }

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

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

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

  /// <summary>
  /// Specify a description of the custom PowerShell snap-in.
  /// </summary>
  public override string Description
  {
    get
    {
      return "This is a custom PowerShell snap-in that includes the Test-HelloWorld and Test-CustomSnapinTest cmdlets.";
    }
  }

  /// <summary>
  /// Specify the localization resource information for the description.
  /// Use the format: resourceBaseName,Description.
  /// </summary>
  public override string DescriptionResource
  {
    get
    {
        return "CustomPSSnapInTest,This is a custom PowerShell snap-in that includes the Test-HelloWorld and Test-CustomSnapinTest cmdlets.";
    }
  }

  /// <summary>
  /// Specify the cmdlets that belong to this custom PowerShell snap-in.
  /// </summary>
  private Collection<CmdletConfigurationEntry> _cmdlets;
  public override Collection<CmdletConfigurationEntry> Cmdlets
  {
    get
    {
      if (_cmdlets == null)
      {
        _cmdlets = new Collection<CmdletConfigurationEntry>();
        _cmdlets.Add(new CmdletConfigurationEntry("test-customsnapintest", typeof(TestCustomSnapinTest), "TestCmdletHelp.dll-help.xml"));
        _cmdlets.Add(new CmdletConfigurationEntry("test-helloworld", typeof(TestHelloWorld), "HelloWorldHelp.dll-help.xml"));
      }

      return _cmdlets;
    }
  }

  /// <summary>
  /// Specify the providers that belong to this custom PowerShell snap-in.
  /// </summary>
  private Collection<ProviderConfigurationEntry> _providers;
  public override Collection<ProviderConfigurationEntry> Providers
  {
    get
    {
      if (_providers == null)
      {
        _providers = new Collection<ProviderConfigurationEntry>();
      }

      return _providers;
    }
  }

  /// <summary>
  /// Specify the types that belong to this custom PowerShell snap-in.
  /// </summary>
  private Collection<TypeConfigurationEntry> _types;
  public override Collection<TypeConfigurationEntry> Types
  {
    get
    {
      if (_types == null)
      {
        _types = new Collection<TypeConfigurationEntry>();
      }

      return _types;
    }
  }

  /// <summary>
  /// Specify the formats that belong to this custom PowerShell snap-in.
  /// </summary>
  private Collection<FormatConfigurationEntry> _formats;
  public override Collection<FormatConfigurationEntry> Formats
  {
    get
    {
      if (_formats == null)
      {
        _formats = new Collection<FormatConfigurationEntry>();
      }

      return _formats;
    }
  }
}

スナップインの登録の詳細については、「Windows PowerShell プログラマーガイド」の「コマンドレット、プロバイダー、およびホスト アプリケーションを登録する方法」を参照してください

参照

コマンドレット、プロバイダー、およびホスト アプリケーションを登録する方法

Windows PowerShell シェル SDK