Schreiben eines benutzerdefinierten Windows PowerShell-Snap-Ins
In diesem Beispiel wird gezeigt, wie sie ein Windows PowerShell-Snap-In schreiben, das bestimmte Cmdlets registriert.
Mit diesem Snap-In-Typ geben Sie an, welche Cmdlets, Anbieter, Typen oder Formate registriert werden. Weitere Informationen zum Schreiben eines Snap-Ins, das alle Cmdlets und Anbieter in einer Assembly registriert, finden Sie unter Schreiben eines Windows PowerShell Snap-Ins.
Zum Schreiben eines Windows PowerShell Snap-Ins, das bestimmte Cmdlets registriert.
Fügen Sie das Attribut RunInstallerAttribute hinzu.
Erstellen Sie eine öffentliche Klasse, die von der System.Management.Automation.Custompssnapin-Klasse ableitungt.
In diesem Beispiel ist der Klassenname "CustomPSSnapinTest".
Fügen Sie eine öffentliche Eigenschaft für den Namen des Snap-Ins hinzu (erforderlich). Verwenden Sie beim Benennen von Snap-Ins keines der folgenden Zeichen:
#
, , , , , , ,.
, ,,
(
)
{
, , ,}
[
]
&
-
/
\
$
;
,:
"
'
<
>
|
?
@
`
.*
In diesem Beispiel ist der Name des Snap-Ins "CustomPSSnapInTest".
Fügen Sie eine öffentliche Eigenschaft für den Anbieter des Snap-Ins hinzu (erforderlich).
In diesem Beispiel ist der Anbieter "Microsoft".
Fügen Sie eine öffentliche Eigenschaft für die Anbieterressource des Snap-Ins hinzu (optional).
In diesem Beispiel ist die Anbieterressource "CustomPSSnapInTest,Microsoft".
Fügen Sie eine öffentliche Eigenschaft für die Beschreibung des Snap-Ins hinzu (erforderlich).
In diesem Beispiel ist die Beschreibung: "Dies ist ein benutzerdefiniertes Windows PowerShell-Snap-In, das die
Test-HelloWorld
Cmdlets undTest-CustomSnapinTest
enthält."Fügen Sie eine öffentliche Eigenschaft für die Beschreibungsressource des Snap-Ins hinzu (optional).
In diesem Beispiel ist die Anbieterressource:
CustomPSSnapInTest: Dies ist ein benutzerdefiniertes Windows PowerShell-Snap-In, das die cmdlets Test-HelloWorld und Test-CustomSnapinTest enthält."
Geben Sie mithilfe der System.Management.Automation.Runspaces.Cmdletconfigurationentry-Klasse die Cmdlets an, die zum benutzerdefinierten Snap-In gehören (optional). Die hier hinzugefügten Informationen umfassen den Namen des Cmdlets, seinen .NET-Typ und den Namen der Cmdlet-Hilfedatei (das Format der Cmdlet-Hilfedatei sollte
name.dll-help.xml
sein).In diesem Beispiel werden die cmdlets Test-HelloWorld testCustomSnapinTest hinzugefügt.
Geben Sie die Anbieter an, die zum benutzerdefinierten Snap-In gehören (optional).
In diesem Beispiel werden keine Anbieter angegeben.
Geben Sie die Typen an, die zum benutzerdefinierten Snap-In gehören (optional).
In diesem Beispiel werden keine Typen angegeben.
Geben Sie die Formate an, die zum benutzerdefinierten Snap-In gehören (optional).
In diesem Beispiel werden keine Formate angegeben.
Beispiel
In diesem Beispiel wird gezeigt, wie sie ein benutzerdefiniertes Windows PowerShell-Snap-In schreiben, das zum Registrieren der Cmdlets und Test-HelloWorld
Test-CustomSnapinTest
verwendet werden kann. Beachten Sie, dass die vollständige Assembly in diesem Beispiel andere Cmdlets und Anbieter enthalten könnte, die von diesem Snap-In nicht registriert werden würden.
[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;
}
}
}
Weitere Informationen zum Registrieren von Snap-Ins finden Sie unter Registrieren von Cmdlets, Anbietern und Hostanwendungen im Windows PowerShell-Programmiererhandbuch.