Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
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 sollen. 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.
So schreiben Sie ein Windows PowerShell-Snap-In, das bestimmte Cmdlets registriert.
Fügen Sie das RunInstallerAttribute-Attribut hinzu.
Erstellen Sie eine öffentliche Klasse, die von der System.Management.Automation.CustomPSSnapIn Klasse abgeleitet wird.
In diesem Beispiel lautet der Klassenname "CustomPSSnapinTest".
Fügen Sie eine öffentliche Eigenschaft für den Namen des Snap-Ins hinzu (erforderlich). Beim Benennen von Snap-Ins verwenden Sie keine der folgenden Zeichen:
#,.,,,(,),{,},[,],&,-,/, ,\,$,;,:,",',<,>,|,?,@,`,*In diesem Beispiel lautet der Name des Snap-Ins "CustomPSSnapInTest".
Fügen Sie eine öffentliche Eigenschaft für den Anbieter des Snap-Ins hinzu (erforderlich).
In diesem Beispiel lautet der Anbieter "Microsoft".
Fügen Sie eine öffentliche Eigenschaft für die Lieferantenressource des Snap-Ins hinzu (optional).
In diesem Beispiel lautet die Lieferantenressource "CustomPSSnapInTest,Microsoft".
Fügen Sie eine öffentliche Eigenschaft für die Beschreibung des Snap-Ins hinzu (erforderlich).
In diesem Beispiel lautet die Beschreibung: "Dies ist ein benutzerdefiniertes Windows PowerShell-Snap-In, das die
Test-HelloWorldundTest-CustomSnapinTestCmdlets enthält".Fügen Sie eine öffentliche Eigenschaft für die Beschreibungsressource des Snap-Ins hinzu (optional).
In diesem Beispiel lautet die Lieferantenressource:
CustomPSSnapInTest, This is a custom Windows PowerShell snap-in that includes the Test-HelloWorld and Test-CustomSnapinTest cmdlets".
Geben Sie die Cmdlets an, die zum benutzerdefinierten Snap-In (optional) gehören, mithilfe der System.Management.Automation.Runspaces.CmdletConfigurationEntry Klasse. Die hier hinzugefügten Informationen umfassen den Namen des Cmdlets, den .NET-Typ und den Namen der Cmdlet-Hilfedatei (das Format des Cmdlet-Hilfedateinamens sollte
name.dll-help.xmlsein).In diesem Beispiel werden die Cmdlets Test-HelloWorld und 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, mit dem die Test-HelloWorld- und Test-CustomSnapinTest-Cmdlets registriert werden können. Beachten Sie, dass in diesem Beispiel die vollständige Assembly andere Cmdlets und Anbieter enthalten könnte, die von diesem Snap-In nicht registriert 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-Programmierhandbuch.