次の方法で共有


カスタム Windows PowerShell スナップインの作成

この例では、特定のコマンドレットを登録する Windows PowerShell スナップインを記述する方法を示します。

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

特定のコマンドレットを登録する Windows PowerShell スナップインを記述します。

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

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

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

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

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

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

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

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

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

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

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

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

    この例では、ベンダー リソースは次のとおりです。

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

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

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

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

    この例では、プロバイダーは指定しません。

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

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

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

    この例では、書式は指定しません。

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

[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