關於參數集的注意事項
Windows PowerShell 會將參數集定義為一組一起運作的參數。 藉由將 Cmdlet 的參數分組,您可以建立單一 Cmdlet,根據使用者指定的參數群組來變更其功能。
使用兩個參數集來定義不同功能的 Cmdlet 範例是 Windows PowerShell 所提供的 Get-EventLog Cmdlet。 當使用者指定 List 或 LogName 參數時,這個 Cmdlet 會傳回不同的資訊。 如果指定了 LogName 參數,Cmdlet 會傳回指定事件記錄檔中事件的相關信息。 如果指定了 List 參數,Cmdlet 會傳回記錄檔本身的相關信息(而非包含的事件資訊)。 在此情況下,List 和 LogName 參數會識別兩個不同的參數集。
要記住參數集的兩個重要事項是,Windows PowerShell 運行時間只會針對特定輸入使用一個參數集,而且每個參數集必須至少有一個參數,該參數集必須至少有一個參數是唯一的。
為了說明最後一點,這個 Stop-Proc Cmdlet 會使用三個參數集:ProcessName、ProcessId和 InputObject。 每個參數集都有一個不在其他參數集中的參數。 參數集可以共用其他參數,但 Cmdlet 會使用唯一參數 ProcessName、ProcessId和 InputObject 來識別 Windows PowerShell 運行時間應該使用的參數集。
宣告 Cmdlet 類別
Cmdlet 建立的第一個步驟一律是命名 Cmdlet,並宣告實作 Cmdlet 的 .NET 類別。 針對此 Cmdlet,會使用生命周期動詞「停止」,因為 Cmdlet 會停止系統進程。 使用名詞名稱 「Proc」,因為 Cmdlet 適用於進程。 在下列宣告中,請注意 Cmdlet 動詞和名詞名稱會反映在 Cmdlet 類別的名稱中。
備註
如需已核准 Cmdlet 動詞名稱的詳細資訊,請參閱 Cmdlet 動詞名稱。
下列程式代碼是這個 Stop-Proc Cmdlet 的類別定義。
[Cmdlet(VerbsLifecycle.Stop, "Proc",
DefaultParameterSetName = "ProcessId",
SupportsShouldProcess = true)]
public class StopProcCommand : PSCmdlet
<Cmdlet(VerbsLifecycle.Stop, "Proc", DefaultParameterSetName:="ProcessId", _
SupportsShouldProcess:=True)> _
Public Class StopProcCommand
Inherits PSCmdlet
宣告 Cmdlet 的參數
此 Cmdlet 會定義 Cmdlet 輸入所需的三個參數(這些參數也會定義參數集),以及管理 Cmdlet 執行的 Force 參數,以及決定 Cmdlet 是否透過管線傳送輸出物件的 PassThru 參數。 根據預設,此 Cmdlet 不會透過管線傳遞物件。 如需這兩個參數的詳細資訊,請參閱 建立可修改系統的 Cmdlet。
宣告 Name 參數
這個輸入參數可讓使用者指定要停止的進程名稱。 請注意,System.Management.Automation.ParameterAttribute 屬性的 ParameterSetName 属性關鍵詞會指定此參數的 ProcessName 參數集。
[Parameter(
Position = 0,
ParameterSetName = "ProcessName",
Mandatory = true,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The name of one or more processes to stop. Wildcards are permitted."
)]
[Alias("ProcessName")]
public string[] Name
{
get { return processNames; }
set { processNames = value; }
}
private string[] processNames;
<Parameter(Position:=0, ParameterSetName:="ProcessName", _
Mandatory:=True, _
ValueFromPipeline:=True, ValueFromPipelineByPropertyName:=True, _
HelpMessage:="The name of one or more processes to stop. " & _
"Wildcards are permitted."), [Alias]("ProcessName")> _
Public Property Name() As String()
Get
Return processNames
End Get
Set(ByVal value As String())
processNames = value
End Set
End Property
Private processNames() As String
另請注意,別名 「ProcessName」 會提供給此參數。
宣告Id參數
這個輸入參數可讓使用者指定要停止的進程標識碼。 請注意,System.Management.Automation.ParameterAttribute 屬性的 ParameterSetName 属性關鍵詞會指定 ProcessId 參數集。
[Parameter(
ParameterSetName = "ProcessId",
Mandatory = true,
ValueFromPipelineByPropertyName = true,
ValueFromPipeline = true
)]
[Alias("ProcessId")]
public int[] Id
{
get { return processIds; }
set { processIds = value; }
}
private int[] processIds;
<Parameter(ParameterSetName:="ProcessId", _
Mandatory:=True, _
ValueFromPipelineByPropertyName:=True, _
ValueFromPipeline:=True), [Alias]("ProcessId")> _
Public Property Id() As Integer()
Get
Return processIds
End Get
Set(ByVal value As Integer())
processIds = value
End Set
End Property
Private processIds() As Integer
另請注意,別名 「ProcessId」 會提供給此參數。
宣告 InputObject 參數
此輸入參數可讓使用者指定輸入物件,其中包含要停止之進程的相關信息。 請注意,System.Management.Automation.ParameterAttribute 屬性的 ParameterSetName 属性關鍵詞會指定此參數的 InputObject 參數集。
[Parameter(
ParameterSetName = "InputObject",
Mandatory = true,
ValueFromPipeline = true)]
public Process[] InputObject
{
get { return inputObject; }
set { inputObject = value; }
}
private Process[] inputObject;
<Parameter(ParameterSetName:="InputObject", _
Mandatory:=True, ValueFromPipeline:=True)> _
Public Property InputObject() As Process()
Get
Return myInputObject
End Get
Set(ByVal value As Process())
myInputObject = value
End Set
End Property
Private myInputObject() As Process
另請注意,此參數沒有別名。
宣告多個參數集中的參數
雖然每個參數集都必須有唯一參數,但參數可以屬於多個參數集。 在這些情況下,請為共用參數提供 System.Management.Automation.ParameterAttribute 參數所屬的每個集合的屬性宣告。 如果參數位於所有參數集中,您只需要宣告參數屬性一次,而且不需要指定參數集名稱。
覆寫輸入處理方法
每個 Cmdlet 都必須覆寫輸入處理方法,通常是 System.Management.Automation.Cmdlet.ProcessRecord 方法。 在此 Cmdlet 中,會覆寫 System.Management.Automation.Cmdlet.ProcessRecord 方法,讓 Cmdlet 可以處理任意數目的進程。 它包含 Select 語句,它會根據使用者指定的參數集來呼叫不同的方法。
protected override void ProcessRecord()
{
switch (ParameterSetName)
{
case "ProcessName":
ProcessByName();
break;
case "ProcessId":
ProcessById();
break;
case "InputObject":
foreach (Process process in inputObject)
{
SafeStopProcess(process);
}
break;
default:
throw new ArgumentException("Bad ParameterSet Name");
} // switch (ParameterSetName...
} // ProcessRecord
Protected Overrides Sub ProcessRecord()
Select Case ParameterSetName
Case "ProcessName"
ProcessByName()
Case "ProcessId"
ProcessById()
Case "InputObject"
Dim process As Process
For Each process In myInputObject
SafeStopProcess(process)
Next process
Case Else
Throw New ArgumentException("Bad ParameterSet Name")
End Select
End Sub 'ProcessRecord ' ProcessRecord
這裡不會說明 Select 語句所呼叫的 Helper 方法,但您可以在下一節的完整程式代碼範例中看到其實作。
程式碼範例
如需完整的 C# 範例程式代碼,請參閱 StopProcessSample04 範例。
定義物件類型和格式設定
Windows PowerShell 會使用 .NET 物件在 Cmdlet 之間傳遞資訊。 因此,Cmdlet 可能需要定義自己的類型,或 Cmdlet 可能需要擴充另一個 Cmdlet 所提供的現有類型。 如需定義新類型或擴充現有類型的詳細資訊,請參閱 擴充物件類型和格式。
建置 Cmdlet
實作 Cmdlet 之後,您必須透過 Windows PowerShell 嵌入式管理單元向 Windows PowerShell 註冊它。 如需註冊 Cmdlet 的詳細資訊,請參閱 如何註冊 Cmdlet、提供者和主應用程式。
測試 Cmdlet
當您的 Cmdlet 已向 Windows PowerShell 註冊時,請在命令行上執行它來測試它。 以下是一些測試,示範如何使用 ProcessId 和 InputObject 參數來測試其參數集來停止進程。
啟動 Windows PowerShell 之後,請執行 Stop-Proc Cmdlet,並將
ProcessId參數設定為根據其識別元停止進程。 在此情況下,Cmdlet 會使用設定為停止進程的ProcessId參數。PS> Stop-Proc -Id 444 Confirm Are you sure you want to perform this action? Performing operation "Stop-Proc" on Target "notepad (444)". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y啟動 Windows PowerShell 之後,執行 Stop-Proc Cmdlet,並將
InputObject參數設定為停止Get-Process命令所擷取的記事本物件上的進程。PS> Get-Process notepad | Stop-Proc Confirm Are you sure you want to perform this action? Performing operation "Stop-Proc" on Target "notepad (444)". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): N
另請參閱
建立修改系統 的 Cmdlet