Cmdlet 可以撰寫數種可由 Windows PowerShell 執行時間向使用者顯示的訊息。 這些訊息包含下列類型:
包含一般使用者資訊的詳細資訊訊息。
包含疑難排解資訊的訊息。
警告訊息,其中包含 Cmdlet 即將執行的作業可能會產生非預期結果的通知。
進度報告訊息,其中包含在執行需要很長時間的作業時,Cmdlet 已完成多少工作的相關資訊。
您的 Cmdlet 可以寫入的訊息數目,或您的 Cmdlet 所寫入的訊息類型沒有任何限制。 每個訊息都是藉由在 Cmdlet 的輸入處理方法內進行特定呼叫來撰寫。
定義 Cmdlet
Cmdlet 建立的第一個步驟,一律會命名 Cmdlet 並宣告可執行 Cmdlet 的 .NET 類別。 任何種類的 Cmdlet 都可以從其輸入處理方法寫入使用者通知;因此,一般而言,您可以使用任何指示 Cmdlet 所執行之系統修改的動詞來命名此 Cmdlet。 如需已核准 Cmdlet 動詞命令的詳細資訊,請參閱 Cmdlet 動詞命令名稱。
Stop-Proc Cmdlet 是設計來修改系統;因此,.NET 類別的 CmdletAttribute 宣告必須包含 SupportsShouldProcess attribute 關鍵字,並設定為 true 。
下列程式碼是此 Stop-Proc Cmdlet 類別的定義。 如需有關此定義的詳細資訊,請參閱 建立修改系統的 Cmdlet。
[Cmdlet(VerbsLifecycle.Stop, "proc",
SupportsShouldProcess = true)]
public class StopProcCommand : Cmdlet
定義修改系統的參數
Stop-Proc Cmdlet 會定義三個參數: Name 、 Force 和 PassThru 。 如需定義這些參數的詳細資訊,請參閱 建立修改系統的 Cmdlet。
以下是 Stop-Proc Cmdlet 的參數宣告。
[Parameter(
Position = 0,
Mandatory = true,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true
)]
public string[] Name
{
get { return processNames; }
set { processNames = value; }
}
private string[] processNames;
/// <summary>
/// Specify the Force parameter that allows the user to override
/// the ShouldContinue call to force the stop operation. This
/// parameter should always be used with caution.
/// </summary>
[Parameter]
public SwitchParameter Force
{
get { return force; }
set { force = value; }
}
private bool force;
/// <summary>
/// Specify the PassThru parameter that allows the user to specify
/// that the cmdlet should pass the process object down the pipeline
/// after the process has been stopped.
/// </summary>
[Parameter]
public SwitchParameter PassThru
{
get { return passThru; }
set { passThru = value; }
}
private bool passThru;
覆寫輸入處理方法
您的 Cmdlet 必須覆寫輸入處理方法,最常使用的方法就是 ProcessRecord。 此 Stop-Proc Cmdlet 會覆寫 ProcessRecord 輸入處理方法。 在此 Stop-Proc Cmdlet 的執行中,會進行呼叫以寫入詳細訊息、偵錯工具訊息和警告訊息。
注意
如需這個方法如何呼叫 ShouldProcess 和 ShouldContinue 方法的詳細資訊,請參閱 建立可修改系統的 Cmdlet (可修改系統)。
寫入詳細資訊訊息
WriteVerbose方法是用來寫入與特定錯誤狀況不相關的一般使用者層級資訊。 系統管理員接著可以使用該資訊繼續處理其他命令。 此外,使用這個方法所撰寫的任何資訊都應該依照需要當地語系化。
此 Stop-Proc Cmdlet 中的下列程式碼會從ProcessRecord方法的覆寫中,顯示兩個WriteVerbose方法的呼叫:。
message = String.Format("Attempting to stop process \"{0}\".", name);
WriteVerbose(message);
message = String.Format("Stopped process \"{0}\", pid {1}.",
processName, process.Id);
WriteVerbose(message);
寫入調試訊息
WriteDebug方法是用來撰寫可用於疑難排解 Cmdlet 操作的偵錯工具訊息(debug)。 從輸入處理方法進行呼叫。
注意
Windows PowerShell 也會定義同時 Debug 提供詳細資訊和偵錯工具資訊的參數。 如果您的 Cmdlet 支援此參數,則不需要在呼叫WriteVerbose的相同程式碼中呼叫WriteDebug ,而不需要呼叫。
範例 Stop-Proc Cmdlet 中的下列兩個程式碼區段會顯示ProcessRecord方法的覆寫中WriteDebug方法的呼叫,以進行呼叫的方法。
在呼叫 ShouldProcess 之前,會立即寫入此偵錯工具訊息。
message =
String.Format("Acquired name for pid {0} : \"{1}\"",
process.Id, processName);
WriteDebug(message);
在呼叫 WriteObject 之前,會立即寫入此偵錯工具訊息。
message =
String.Format("Writing process \"{0}\" to pipeline",
processName);
WriteDebug(message);
WriteObject(process);
Windows PowerShell 會自動將WriteDebug呼叫傳送至追蹤基礎結構和 Cmdlet。 這可讓您不需要在 Cmdlet 內執行任何額外的開發工作,即可將方法呼叫追蹤至主控應用程式、檔案或偵錯工具。 下列命令列專案會執行追蹤作業。
PS> 追蹤-運算式停止-進程-檔進程:命令停止-進程記事本
寫入警告訊息
當 Cmdlet 即將執行可能會產生非預期結果的作業(例如,覆寫唯讀檔案)時,會使用 WriteWarning 方法來寫入警告訊息。
下列範例中的程式碼 Stop-Proc Cmdlet 會從ProcessRecord方法的覆寫中,顯示WriteWarning方法的呼叫程式碼的呼叫方法。
if (criticalProcess)
{
message =
String.Format("Stopping the critical process \"{0}\".",
processName);
WriteWarning(message);
} // if (criticalProcess...
寫入進度訊息
當 Cmdlet 作業需要較長的時間才能完成時,會使用 WriteProgress 來寫入進度訊息。 對 WriteProgress 的呼叫會傳遞傳送至主控應用程式的 Progressrecord 物件,以轉譯給使用者的應用程式使用。
注意
這個 Stop-Proc Cmdlet 不會包含 WriteProgress 方法的呼叫,而不包含呼叫。
下列程式碼是嘗試複製專案之 Cmdlet 所寫入的進度訊息範例。
int myId = 0;
string myActivity = "Copy-item: Copying *.* to c:\abc";
string myStatus = "Copying file bar.txt";
ProgressRecord pr = new ProgressRecord(myId, myActivity, myStatus);
WriteProgress(pr);
pr.RecordType = ProgressRecordType.Completed;
WriteProgress(pr);
程式碼範例
如需完整的 c # 範例程式碼,請參閱 StopProcessSample02 範例。
定義物件類型和格式
Windows PowerShell 使用 .net 物件在 Cmdlet 之間傳遞資訊。 因此,Cmdlet 可能需要定義自己的類型,或 Cmdlet 可能需要擴充另一個 Cmdlet 所提供的現有類型。 如需定義新型別或擴充現有類型的詳細資訊,請參閱 擴充物件類型和格式。
建立 Cmdlet
在執行 Cmdlet 之後,必須透過 Windows PowerShell 嵌入式管理單元向 Windows PowerShell 註冊。 如需註冊 Cmdlet 的詳細資訊,請參閱 如何註冊 Cmdlet、提供者和主機應用程式。
測試 Cmdlet
當您的 Cmdlet 註冊 Windows PowerShell 時,您可以在命令列上執行它來進行測試。 讓我們測試範例 Stop-Proc Cmdlet。 如需從命令列使用 Cmdlet 的詳細資訊,請參閱Windows PowerShell 的開始使用。
下列命令列專案會使用 Stop-Proc 來停止名為 "NOTEPAD" 的進程、提供詳細語音總機,以及列印偵錯工具資訊。
PS> stop-proc -Name notepad -Verbose -Debug即會出現下列輸出。
VERBOSE: Attempting to stop process " notepad ". DEBUG: Acquired name for pid 5584 : "notepad" Confirm Continue with this operation? [Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help (default is "Y"): Y Confirm Are you sure you want to perform this action? Performing operation "stop-proc" on Target "notepad (5584)". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y VERBOSE: Stopped process "notepad", pid 5584.