ค้นหาเว็บไซต์

การเขียน Windows PowerShell Snap-in


ตัวอย่างนี้แสดงวิธีการเขียนสแน็ปอิน Windows PowerShell ที่สามารถใช้เพื่อลงทะเบียน cmdlets และผู้ให้บริการ Windows PowerShell ทั้งหมดในแอสเซมบลี

ด้วยสแน็ปอินประเภทนี้ คุณไม่ได้เลือก cmdlets และผู้ให้บริการที่คุณต้องการลงทะเบียน หากต้องการเขียนสแนปอินที่ให้คุณเลือกสิ่งที่ลงทะเบียนไว้ โปรดดูที่ การเขียนสแนปอิน Windows PowerShell แบบกำหนดเอง

การเขียน Windows PowerShell Snap-in

  1. เพิ่มแอตทริบิวต์ RunInstallerAttribute

  2. สร้างคลาสสาธารณะที่ได้มาจากคลาส System.Management.Automation.PSSnapIn

    ในตัวอย่างนี้ ชื่อคลาสคือ "GetProcPSSnapIn01"

  3. เพิ่มคุณสมบัติสาธารณะสำหรับชื่อของสแน็ปอิน (จำเป็น) เมื่อตั้งชื่อสแนปอิน ห้ามใช้อักขระใด ๆ ต่อไปนี้: #, ., ,, ( , <รหัส>), <รหัส>{, <รหัส>}, <รหัส>[, <รหัส>], <รหัส>& , <รหัส>-, <รหัส>/, <รหัส>\, <รหัส>$, <รหัส>;, <รหัส>:, <รหัส>", <รหัส>', <รหัส><, <รหัส>>, <รหัส>|, ?, @, `, *

    ในตัวอย่างนี้ ชื่อของสแน็ปอินคือ "GetProcPSSnapIn01"

  4. เพิ่มทรัพย์สินสาธารณะสำหรับผู้จัดจำหน่ายสแน็ปอิน (จำเป็น)

    ในตัวอย่างนี้ ผู้จัดจำหน่ายคือ "Microsoft"

  5. เพิ่มคุณสมบัติสาธารณะสำหรับทรัพยากรผู้จัดจำหน่ายของสแน็ปอิน (ไม่จำเป็น)

    ในตัวอย่างนี้ ทรัพยากรของผู้จัดจำหน่ายคือ "GetProcPSSnapIn01,Microsoft"

  6. เพิ่มคุณสมบัติสาธารณะสำหรับคำอธิบายของสแน็ปอิน (จำเป็น)

    ในตัวอย่างนี้ คำอธิบายคือ "นี่คือสแน็ปอิน Windows PowerShell ที่ลงทะเบียน get-proc cmdlet"

  7. เพิ่มคุณสมบัติสาธารณะสำหรับทรัพยากรคำอธิบายของสแน็ปอิน (ไม่จำเป็น)

    ในตัวอย่างนี้ ทรัพยากรของผู้จัดจำหน่ายคือ "GetProcPSSnapIn01 นี่คือสแน็ปอิน Windows PowerShell ที่ลงทะเบียน get-proc cmdlet"

ตัวอย่าง

ตัวอย่างนี้แสดงวิธีการเขียนสแน็ปอิน Windows PowerShell ที่สามารถใช้เพื่อลงทะเบียน Get-Proc cmdlet ในเชลล์ Windows PowerShell โปรดทราบว่าในตัวอย่างนี้ แอสเซมบลีที่สมบูรณ์จะประกอบด้วยเฉพาะคลาสสแน็ปอิน GetProcPSSnapIn01 และคลาส cmdlet Get-Proc

[RunInstaller(true)]
public class GetProcPSSnapIn01 : PSSnapIn
{
  /// <summary>
  /// Create an instance of the GetProcPSSnapIn01 class.
  /// </summary>
  public GetProcPSSnapIn01()
         : base()
  {
  }

  /// <summary>
  /// Specify the name of the PowerShell snap-in.
  /// </summary>
  public override string Name
  {
    get
    {
      return "GetProcPSSnapIn01";
    }
  }

  /// <summary>
  /// Specify the vendor for the PowerShell snap-in.
  /// </summary>
  public override string Vendor
  {
    get
    {
      return "Microsoft";
    }
  }

  /// <summary>
  /// Specify the localization resource information for the vendor.
  /// Use the format: resourceBaseName,VendorName.
  /// </summary>
  public override string VendorResource
  {
    get
    {
      return "GetProcPSSnapIn01,Microsoft";
    }
  }

  /// <summary>
  /// Specify a description of the PowerShell snap-in.
  /// </summary>
  public override string Description
  {
    get
    {
      return "This is a PowerShell snap-in that includes the get-proc cmdlet.";
    }
  }

  /// <summary>
  /// Specify the localization resource information for the description.
  /// Use the format: resourceBaseName,Description.
  /// </summary>
  public override string DescriptionResource
  {
    get
    {
      return "GetProcPSSnapIn01,This is a PowerShell snap-in that includes the get-proc cmdlet.";
    }
  }
}

ดูเพิ่มเติม

วิธีลงทะเบียน Cmdlets ผู้ให้บริการ และแอปพลิเคชันโฮสต์

Windows PowerShell เชลล์ SDK

บทความที่เกี่ยวข้อง