วิธีแทนที่วิธีการประมวลผลอินพุต
ตัวอย่างเหล่านี้แสดงวิธีการเขียนทับวิธีการประมวลผลอินพุตภายใน cmdlet วิธีการเหล่านี้ใช้เพื่อดำเนินการต่อไปนี้:
วิธีการ System.Management.Automation.Cmdlet.BeginProcessing ใช้เพื่อดำเนินการเริ่มต้นระบบครั้งเดียวที่ถูกต้องสำหรับวัตถุทั้งหมดที่ประมวลผลโดย cmdlet รันไทม์ของ Windows PowerShell เรียกใช้วิธีนี้เพียงครั้งเดียว
วิธีการ System.Management.Automation.Cmdlet.ProcessRecord ใช้ในการประมวลผลวัตถุที่ส่งผ่านไปยัง cmdlet รันไทม์ของ Windows PowerShell เรียกวิธีนี้สำหรับแต่ละวัตถุที่ส่งผ่านไปยัง cmdlet
วิธีการ System.Management.Automation.Cmdlet.EndProcessing ใช้เพื่อดำเนินการประมวลผลภายหลังเพียงครั้งเดียว รันไทม์ของ Windows PowerShell เรียกใช้วิธีนี้เพียงครั้งเดียว
เมื่อต้องการแทนวิธีการBeginProcessing
- ประกาศการแทนที่ที่มีการป้องกันของเมธอด System.Management.Automation.Cmdlet.BeginProcessing
คลาสต่อไปนี้จะพิมพ์ข้อความตัวอย่าง เมื่อต้องการใช้คลาสนี้ ให้เปลี่ยนคำกริยาและคำนามในแอตทริบิวต์ Cmdlet เปลี่ยนชื่อของคลาสเพื่อให้สะท้อนถึงคำกริยาและคำนามใหม่ จากนั้นเพิ่มฟังก์ชันการทำงานที่คุณต้องการเพื่อแทนที่ System.Management.Automation.Cmdlet.BeginProcessing วิธี.
[Cmdlet(VerbsDiagnostic.Test, "BeginProcessingClass")]
public class TestBeginProcessingClassTemplate : Cmdlet
{
// Override the BeginProcessing method to add preprocessing
//operations to the cmdlet.
protected override void BeginProcessing()
{
// Replace the WriteObject method with the logic required
// by your cmdlet. It is used here to generate the following
// output:
// "This is a test of the BeginProcessing template."
WriteObject("This is a test of the BeginProcessing template.");
}
}
เพื่อแทนที่วิธีการ ProcessRecord
- ประกาศการแทนที่ที่มีการป้องกันของเมธอด System.Management.Automation.Cmdlet.ProcessRecord
คลาสต่อไปนี้จะพิมพ์ข้อความตัวอย่าง เมื่อต้องการใช้คลาสนี้ ให้เปลี่ยนคำกริยาและคำนามในแอตทริบิวต์ Cmdlet เปลี่ยนชื่อของคลาสเพื่อให้สะท้อนถึงคำกริยาและคำนามใหม่ จากนั้นเพิ่มฟังก์ชันการทำงานที่คุณต้องการเพื่อแทนที่ System.Management.Automation.Cmdlet.ProcessRecord วิธี.
[Cmdlet(VerbsDiagnostic.Test, "ProcessRecordClass")]
public class TestProcessRecordClassTemplate : Cmdlet
{
// Override the ProcessRecord method to add processing
//operations to the cmdlet.
protected override void ProcessRecord()
{
// Replace the WriteObject method with the logic required
// by your cmdlet. It is used here to generate the following
// output:
// "This is a test of the ProcessRecord template."
WriteObject("This is a test of the ProcessRecord template.");
}
}
เมื่อต้องการแทนที่วิธีการEndProcessing
- ประกาศการแทนที่ที่มีการป้องกันของเมธอด System.Management.Automation.Cmdlet.EndProcessing
คลาสต่อไปนี้จะพิมพ์ตัวอย่าง เมื่อต้องการใช้คลาสนี้ ให้เปลี่ยนคำกริยาและคำนามในแอตทริบิวต์ Cmdlet เปลี่ยนชื่อของคลาสเพื่อให้สะท้อนถึงคำกริยาและคำนามใหม่ จากนั้นเพิ่มฟังก์ชันการทำงานที่คุณต้องการเพื่อแทนที่ System.Management.Automation.Cmdlet.EndProcessing วิธี.
[Cmdlet(VerbsDiagnostic.Test, "EndProcessingClass")]
public class TestEndProcessingClassTemplate : Cmdlet
{
// Override the EndProcessing method to add postprocessing
//operations to the cmdlet.
protected override void EndProcessing()
{
// Replace the WriteObject method with the logic required
// by your cmdlet. It is used here to generate the following
// output:
// "This is a test of the BeginProcessing template."
WriteObject("This is a test of the EndProcessing template.");
}
}
ดูเพิ่มเติม
System.Management.Automation.Cmdlet.BeginProcessing
System.Management.Automation.Cmdlet.EndProcessing
System.Management.Automation.Cmdlet.ProcessRecord
การเขียน Windows PowerShell Cmdlet