当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 编写自定义 Windows PowerShell 管理单元

作者:精品下载站 日期:2024-12-14 02:47:29 浏览:13 分类:玩电脑

编写自定义 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. 添加公共属性来描述管理单元(必需)。

    在此示例中,描述为:“这是一个自定义 Windows PowerShell 管理单元,其中包括 Test-HelloWorldTest-CustomSnapinTest cmdlet”。

  7. 为管理单元的描述资源添加公共属性(可选)。

    在此示例中,供应商资源是:

    CustomPSSnapInTest,这是一个自定义 Windows PowerShell 管理单元,其中包括 Test-HelloWorld 和 Test-CustomSnapinTest cmdlet”。

  8. 使用 System.Management.Automation.Runspaces.Cmdletconfigurationentry 类指定属于自定义管理单元(可选)的 cmdlet。此处添加的信息包括 cmdlet 的名称、其 .NET 类型和 cmdlet 帮助文件名(cmdlet 帮助文件名的格式应为 name.dll-help.xml)。

    此示例添加 Test-HelloWorld 和 TestCustomSnapinTest cmdlet。

  9. 指定属于自定义管理单元的提供程序(可选)。

    此示例未指定任何提供程序。

  10. 指定属于自定义管理单元的类型(可选)。

    此示例未指定任何类型。

  11. 指定属于自定义管理单元的格式(可选)。

    此示例未指定任何格式。

例子

此示例演示如何编写可用于注册 Test-HelloWorldTest-CustomSnapinTest cmdlet 的自定义 Windows PowerShell 管理单元。请注意,在此示例中,完整的程序集可能包含此管理单元不会注册的其他 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 外壳 SDK

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯