共用方式為


撰寫自定義 Windows PowerShell 嵌入式管理單元

此範例示範如何撰寫註冊特定 Cmdlet 的 Windows PowerShell 嵌入式管理單元。

使用這種類型的嵌入式管理單元,您可以指定要註冊的 Cmdlet、提供者、類型或格式。 如需如何撰寫嵌入式管理單元以註冊元件中所有 Cmdlet 和提供者的詳細資訊,請參閱 撰寫 Windows PowerShell 嵌入式管理單元

若要撰寫註冊特定 Cmdlet 的 Windows PowerShell 嵌入式管理單元。

  1. 新增 RunInstallerAttribute 屬性。

  2. 建立衍生自 System.Management.Automation.CustomPSSnapIn 類別的公用類別。

    在此範例中,類別名稱為 「CustomPSSnapinTest」。。

  3. 為嵌入式管理單元的名稱新增公用屬性(必要)。 命名嵌入式管理單元時, 不使用下列任何字元:#.,(){}[]&-/\$;:"'<>|?@`*

    在此範例中,嵌入式管理單元的名稱是 「CustomPSSnapInTest」。。

  4. 為嵌入式管理單元的廠商新增公用屬性(必要)。

    在此範例中,廠商為「Microsoft」。

  5. 為嵌入式管理單元的廠商資源新增公用屬性(選擇性)。

    在此範例中,廠商資源為 「CustomPSSnapInTest,Microsoft」。。

  6. 為嵌入式管理單元的描述新增公用屬性(必要)。

    在此範例中,描述為:「這是包含 Test-HelloWorldTest-CustomSnapinTest Cmdlet 的自定義 Windows PowerShell 嵌入式管理單元。

  7. 為嵌入式管理單元的描述資源新增公用屬性(選擇性)。

    在此範例中,廠商資源為:

    CustomPSSnapInTest,這是包含 Test-HelloWorld 和 Test-CustomSnapinTest Cmdlet 的自定義 Windows PowerShell 嵌入式管理單元。

  8. 使用 System.Management.Automation.Runspaces.CmdletConfigurationEntry 類別,指定屬於自定義嵌入式管理單元的 Cmdlet。 這裡新增的資訊包括 Cmdlet 的名稱、其 .NET 類型,以及 Cmdlet 說明檔名稱(Cmdlet 說明檔的格式應 name.dll-help.xml)。

    此範例會新增 Test-HelloWorld 和 TestCustomSnapinTest Cmdlet。

  9. 指定屬於自定義嵌入式管理單元的提供者(選擇性)。

    此範例未指定任何提供者。

  10. 指定屬於自定義嵌入式管理單元的類型(選擇性)。

    此範例未指定任何類型。

  11. 指定屬於自定義嵌入式管理單元的格式(選擇性)。

    此範例未指定任何格式。

範例

此範例示範如何撰寫自定義 Windows PowerShell 嵌入式管理單元,以用來註冊 Test-HelloWorldTest-CustomSnapinTest Cmdlet。 請注意,在此範例中,完整元件可能包含其他 Cmdlet 和提供者,而這個嵌入式管理單元不會註冊。

[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 程式設計人員指南中的 如何註冊 Cmdlet、提供者和主應用程式

另請參閱

如何註冊 Cmdlet、提供者和主應用程式

Windows PowerShell Shell SDK