這些範例示範如何覆寫 Cmdlet 內的輸入處理方法。 這些方法是用來執行下列作業:
BeginProcessing方法是用來執行一次性啟動作業,這些作業對 Cmdlet 所處理的所有物件都是有效的。 Windows PowerShell 執行時間只會呼叫這個方法一次。
ProcessRecord方法是用來處理傳遞給指令程式的物件。 Windows PowerShell 執行時間會針對傳遞給 Cmdlet 的每個物件呼叫這個方法。
System.servicemodel 方法是 用來執行一次性的後置處理作業。 Windows PowerShell 執行時間只會呼叫這個方法一次。
覆寫 BeginProcessing 方法
- 宣告 BeginProcessing 方法的受保護覆寫。
下列類別會列印範例訊息。 若要使用此類別,請變更 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 方法
- 宣告 ProcessRecord 方法的受保護覆寫。
下列類別會列印範例訊息。 若要使用此類別,請變更 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.string 方法的 受保護覆寫。
下列類別會列印範例。 若要使用此類別,請變更 Cmdlet 屬性中的動詞和名詞、變更類別的名稱以反映新的動詞和名詞,然後將您需要的功能加入至 system.object 的覆寫方法。
[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