在所有最實用的 Cmdlet 中,Get-Member 便是其中一個,它會顯示命令所傳回 .NET Framework 物件的相關資訊。相關資訊包括了物件的類型、屬性和方法。
若要使用 Get-Member,請使用管線運算子 (|) 將命令結果傳送給 Get-Member。例如:
get-service | get-member
這個命令指出 Get-Service 實際上是傳回 System.ServiceProcess.ServiceController 物件的集合,每個物件分別代表電腦上的每一個服務。
TypeName: System.ServiceProcess.ServiceController Name MemberType Definition ---- ---------- ---------- Name AliasProperty Name = ServiceName add_Disposed Method System.Void add_Disposed(EventHandler value) Close Method System.Void Close() Continue Method System.Void Continue() CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType) Dispose Method System.Void Dispose() Equals Method System.Boolean Equals(Object obj) ExecuteCommand Method System.Void ExecuteCommand(Int32 command) get_CanPauseAndContinue Method System.Boolean get_CanPauseAndContinue() get_CanShutdown Method System.Boolean get_CanShutdown() get_CanStop Method System.Boolean get_CanStop() get_Container Method System.ComponentModel.IContainer get_Container() get_DependentServices Method System.ServiceProcess.ServiceController[] get_DependentServices() get_DisplayName Method System.String get_DisplayName() get_MachineName Method System.String get_MachineName() get_ServiceHandle Method System.Runtime.InteropServices.SafeHandle get_ServiceHandle() get_ServiceName Method System.String get_ServiceName() get_ServicesDependedOn Method System.ServiceProcess.ServiceController[] get_ServicesDependedOn() get_ServiceType Method System.ServiceProcess.ServiceType get_ServiceType() get_Site Method System.ComponentModel.ISite get_Site() get_Status Method System.ServiceProcess.ServiceControllerStatus get_Status() GetHashCode Method System.Int32 GetHashCode() GetLifetimeService Method System.Object GetLifetimeService() GetType Method System.Type GetType() InitializeLifetimeService Method System.Object InitializeLifetimeService() Pause Method System.Void Pause() Refresh Method System.Void Refresh() remove_Disposed Method System.Void remove_Disposed(EventHandler value) set_DisplayName Method System.Void set_DisplayName(String value) set_MachineName Method System.Void set_MachineName(String value) set_ServiceName Method System.Void set_ServiceName(String value) set_Site Method System.Void set_Site(ISite value) Start Method System.Void Start(), System.Void Start(String[] args) Stop Method System.Void Stop() ToString Method System.String ToString() WaitForStatus Method System.Void WaitForStatus(ServiceControllerStatus desiredStatus), System.Voi... CanPauseAndContinue Property System.Boolean CanPauseAndContinue {get;} CanShutdown Property System.Boolean CanShutdown {get;} CanStop Property System.Boolean CanStop {get;} Container Property System.ComponentModel.IContainer Container {get;} DependentServices Property System.ServiceProcess.ServiceController[] DependentServices {get;} DisplayName Property System.String DisplayName {get;set;} MachineName Property System.String MachineName {get;set;} ServiceHandle Property System.Runtime.InteropServices.SafeHandle ServiceHandle {get;} ServiceName Property System.String ServiceName {get;set;} ServicesDependedOn Property System.ServiceProcess.ServiceController[] ServicesDependedOn {get;} ServiceType Property System.ServiceProcess.ServiceType ServiceType {get;} Site Property System.ComponentModel.ISite Site {get;set;} Status Property System.ServiceProcess.ServiceControllerStatus Status {get;}
這份資訊看起來非常技術性,但其實是一份很實用的資料。
-
「型別名稱」(如 "System.ServiceProcess.ServiceController") 會告訴您該 Cmdlet 所傳回的 .NET 物件類型。如需這個 .NET 類別中所包含物件的詳細資訊,請在 MSDN 的 [搜尋] 文字方塊中貼上此型別名稱。關聯的 MSDN 主題包括在這個類別中物件屬性和方法的相關資訊,其中包括 Get-Service 傳回的物件。
-
Property 型別代表物件的屬性。每個屬性的值就是該服務物件的相關資訊。例如,ServiceController 物件具有 CanPauseAndContinue 屬性。這個屬性的 MSDN 描述會說明此屬性可指定該服務是否可以暫停和恢復。
若要列出特定服務的屬性值,請輸入:
例如:(get-service <service-name>).<property-name>
若要顯示列出 Alerter 服務中 CanPauseAndContinue 屬性之名稱和值的清單,請輸入:(get-service alerter).canpauseandcontinue
若要顯示 Alerter 服務所有屬性的值清單,請輸入:get-service alerter | format-list -property name, CanPauseAndContinue
若要顯示列出所有服務中 CanPauseAndContinue 屬性之名稱和值的表格,請輸入:get-service alerter | format-list -property *
get-service | format-table -property name, CanPauseAndContinue
-
Method 型別表示物件的方法,意即,您可以在該物件上執行的動作。例如,ServiceController 物件具有可用來停止服務的 Stop 方法。
若要呼叫服務物件的方法,請使用下列格式 (請務必加入括號)。
(get-service <service-name>).<method-name>() For example, (get-service schedule).stop()
如需 Get-Member 命令的詳細資訊,請輸入:
get-help get-member -detailed