Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Questo esempio illustra come scrivere uno snap-in di Windows PowerShell che registra cmdlet specifici.
Con questo tipo di snap-in, è possibile specificare i cmdlet, i provider, i tipi o i formati da registrare. Per altre informazioni su come scrivere uno snap-in che registra tutti i cmdlet e i provider in un assembly, vedere Scrittura di uno snap-in di Windows PowerShell.
Per scrivere uno snap-in di Windows PowerShell che registra cmdlet specifici.
Aggiungere l'attributo RunInstallerAttribute.
Creare una classe pubblica che deriva dalla classe System.Management.Automation.CustomPSSnapIn.
In questo esempio il nome della classe è "CustomPSSnapinTest".
Aggiungere una proprietà pubblica per il nome dello snap-in (obbligatorio). Quando si assegnano nomi agli snap-in, non usare uno dei caratteri seguenti:
#,.,,,(,),{,},[,],&,-,/,\,$,;,:,",',<,>,|,?,@,`,*In questo esempio il nome dello snap-in è "CustomPSSnapInTest".
Aggiungere una proprietà pubblica per il fornitore dello snap-in (obbligatorio).
In questo esempio il fornitore è "Microsoft".
Aggiungere una proprietà pubblica per la risorsa fornitore dello snap-in (facoltativo).
In questo esempio la risorsa fornitore è "CustomPSSnapInTest,Microsoft".
Aggiungere una proprietà pubblica per la descrizione dello snap-in (obbligatorio).
In questo esempio la descrizione è: "Si tratta di uno snap-in di Windows PowerShell personalizzato che include i cmdlet
Test-HelloWorldeTest-CustomSnapinTest".Aggiungere una proprietà pubblica per la risorsa di descrizione dello snap-in (facoltativo).
In questo esempio la risorsa fornitore è:
CustomPSSnapInTest, snap-in di Windows PowerShell personalizzato che include i cmdlet Test-HelloWorld e Test-CustomSnapinTest".
Specificare i cmdlet che appartengono allo snap-in personalizzato (facoltativo) usando la classe System.Management.Automation.Runspaces.CmdletConfigurationEntry. Le informazioni aggiunte qui includono il nome del cmdlet, il relativo tipo .NET e il nome del file della Guida del cmdlet (il formato del nome del file della Guida del cmdlet deve essere
name.dll-help.xml).In questo esempio vengono aggiunti i cmdlet Test-HelloWorld e TestCustomSnapinTest.
Specificare i provider che appartengono allo snap-in personalizzato (facoltativo).
In questo esempio non vengono specificati provider.
Specificare i tipi che appartengono allo snap-in personalizzato (facoltativo).
Questo esempio non specifica alcun tipo.
Specificare i formati che appartengono allo snap-in personalizzato (facoltativo).
In questo esempio non vengono specificati formati.
Esempio
Questo esempio illustra come scrivere uno snap-in personalizzato di Windows PowerShell che può essere usato per registrare i cmdlet Test-HelloWorld e Test-CustomSnapinTest. Tenere presente che in questo esempio l'assembly completo potrebbe contenere altri cmdlet e provider che non verrebbero registrati da questo snap-in.
[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;
}
}
}
Per altre informazioni sulla registrazione di snap-in, vedere Come registrare cmdlet, provider e applicazioni host nella Guida per programmatori di Windows PowerShell.