此示例演示如何编写注册特定 cmdlet 的 Windows PowerShell 管理单元。
使用此类型的管理单元,可以指定要注册的 cmdlet、提供程序、类型或格式。 有关如何编写注册程序集中所有 cmdlet 和提供程序的管理单元的详细信息,请参阅 编写 Windows PowerShell 管理单元。
编写注册特定 cmdlet 的 Windows PowerShell 管理单元。
添加 RunInstallerAttribute 属性。
创建派生自 System.Management.Automation.CustomPSSnapIn 类的公共类。
在此示例中,类名称为“CustomPSSnapinTest”。
为管理单元的名称添加公共属性(必需)。 命名管理单元时, 请勿使用以下任何字符:
#
、.
、,
、(
、)
、{
、}
、[
、]
、&
、-
、/
、\
、$
、;
、:
、"
、'
、<
、>
、|
、?
、@
、`
、*
在此示例中,管理单元的名称为“CustomPSSnapInTest”。
为管理单元的供应商添加公共属性(必需)。
在此示例中,供应商为“Microsoft”。
为管理单元的供应商资源添加公共属性(可选)。
在此示例中,供应商资源为“CustomPSSnapInTest,Microsoft”。
为管理单元的说明添加公共属性(必需)。
在此示例中,说明为:“这是包含
Test-HelloWorld
和Test-CustomSnapinTest
cmdlet 的自定义 Windows PowerShell 管理单元”。为管理单元的说明资源添加公共属性(可选)。
在此示例中,供应商资源为:
CustomPSSnapInTest,这是一个自定义 Windows PowerShell 管理单元,其中包括 Test-HelloWorld 和 Test-CustomSnapinTest cmdlet”。
使用 System.Management.Automation.Runspaces.CmdletConfigurationEntry 类指定属于自定义管理单元(可选)的 cmdlet。 此处添加的信息包括 cmdlet 的名称、其 .NET 类型和 cmdlet 帮助文件名(cmdlet 帮助文件名的格式应
name.dll-help.xml
)。此示例添加 Test-HelloWorld 和 TestCustomSnapinTest cmdlet。
指定属于自定义管理单元的提供程序(可选)。
此示例未指定任何提供程序。
指定属于自定义管理单元的类型(可选)。
此示例未指定任何类型。
指定属于自定义管理单元的格式(可选)。
此示例不指定任何格式。
示例
此示例演示如何编写可用于注册 Test-HelloWorld
和 Test-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、提供程序和主机应用程序。