Aracılığıyla paylaş


Özel Windows PowerShell Ek Bileşeni Yazma

Bu örnekte, belirli cmdlet'leri kaydeden bir Windows PowerShell ek bileşeninin nasıl yazıldığı gösterilmektedir.

Bu tür bir ek bileşenle, hangi cmdlet'lerin, sağlayıcıların, türlerin veya biçimlerin kaydedileceğini belirtirsiniz. Derlemedeki tüm cmdlet'leri ve sağlayıcıları kaydeden bir ek bileşen yazma hakkında daha fazla bilgi için bkz. Windows PowerShell Ek Bileşeni Yazma.

Belirli cmdlet'leri kaydeden bir Windows PowerShell Ek Bileşeni yazmak için.

  1. RunInstallerAttribute özniteliğini ekleyin.

  2. System.Management.Automation.CustomPSSnapIn sınıfından türetilen bir genel sınıf oluşturun.

    Bu örnekte sınıf adı "CustomPSSnapinTest" şeklindedir.

  3. Ek bileşenin adı için bir ortak özellik ekleyin (gerekli). Ek bileşenleri adlandırırken, şu karakterlerden hiçbirini kullanmayın: #, ., ,, (, ), {, }, [, ], &, -, /, \, $, ;, :, ", ', <, >, |, ?, @, `, *

    Bu örnekte, ek bileşenin adı "CustomPSSnapInTest" şeklindedir.

  4. Ek bileşenin satıcısı için bir genel özellik ekleyin (gerekli).

    Bu örnekte satıcı "Microsoft" şeklindedir.

  5. Ek bileşenin satıcı kaynağı için bir genel özellik ekleyin (isteğe bağlı).

    Bu örnekte, satıcı kaynağı "CustomPSSnapInTest,Microsoft" şeklindedir.

  6. Ek bileşenin açıklaması için bir ortak özellik ekleyin (gerekli).

    Bu örnekte açıklama şu şekildedir: "Bu, Test-HelloWorld ve Test-CustomSnapinTest cmdlet'lerini içeren özel bir Windows PowerShell ek bileşenidir".

  7. Ek bileşenin açıklama kaynağı için bir ortak özellik ekleyin (isteğe bağlı).

    Bu örnekte satıcı kaynağı:

    CustomPSSnapInTest, Bu, Test-HelloWorld ve Test-CustomSnapinTest cmdlet'lerini içeren özel bir Windows PowerShell ek bileşenidir".

  8. System.Management.Automation.Runspaces.CmdletConfigurationEntry sınıfını kullanarak özel ek bileşene (isteğe bağlı) ait cmdlet'leri belirtin. Buraya eklenen bilgiler cmdlet'in adını, .NET türünü ve cmdlet Yardım dosyası adını içerir (cmdlet Yardım dosya adının biçimi name.dll-help.xmlolmalıdır).

    Bu örnek Test-HelloWorld ve TestCustomSnapinTest cmdlet'lerini ekler.

  9. Özel ek bileşene ait sağlayıcıları belirtin (isteğe bağlı).

    Bu örnek hiçbir sağlayıcı belirtmez.

  10. Özel ek bileşene ait türleri belirtin (isteğe bağlı).

    Bu örnek herhangi bir tür belirtmez.

  11. Özel ek bileşene ait biçimleri belirtin (isteğe bağlı).

    Bu örnek herhangi bir biçim belirtmez.

Örnek

Bu örnekte, Test-HelloWorld ve Test-CustomSnapinTest cmdlet'lerini kaydetmek için kullanılabilecek özel bir Windows PowerShell ek bileşeninin nasıl yazılacağı gösterilmektedir. Bu örnekte, tüm derlemenin bu ek bileşen tarafından kaydedilmeyecek diğer cmdlet'leri ve sağlayıcıları içerebileceğini unutmayın.

[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;
    }
  }
}

Ek bileşenleri kaydetme hakkında daha fazla bilgi için windows powershell programcı kılavuzu cmdlet'leri, sağlayıcıları ve konak uygulamalarını kaydetme bölümüne bakın.

Ayrıca Bkz.

Cmdlet'leri, Sağlayıcıları ve Konak Uygulamalarını Kaydetme

Windows PowerShell Shell SDK