了解对象:Get-Member
Get-Member是最有用的cmdlet之一,它显示命令返回的.NET对象的信息,包括对象的类型,属性和方法。
使用管道符将命令的结果传送给Get-Member,比如:
[code:1]Get-service | get-member[/code:1]
Get-Service实际了返回了一组Syste.SericeProcess.ServiceController对象,每一个都是对应系统得一个服务。
[code:1] 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)
GetHashCode Method System.Int32 GetHashCode()
GetLifetimeService Method System.Object GetLifetimeService()
GetType Method System.Type GetType()
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()
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;}[/code:1]
上面的信息看上去很技术化,其实是非常实用的。
类型名(typename, 如:"System.ServicesProcess.ServicesController")告诉你cmdlet返回的是何种类型的.NET对象。欲获取此.NET类中对象的信息,复制类型名到MSDN搜索。MSDN相关文档包含了该类中对象属性和方法的信息。
属性类型(Property types),代表对象的属性,每个属性的值都是服务对象的信息。比如,ServiceController对象具有CanPauseAndContinue属性。MSDN对此属性的解释为该服务是否可以被暂停和继续。
要列出某个特定服务的属性,输入:
[code:1](get-service <service-name>).<property-name>[/code:1]
比如:
[code:1](get-service alerter).canpauseandcontinue[/code:1]
以列表方式显示Alerter服务CanPauseAndContinue属性的名称和服务,输入:
[code:1]get-service alerter | format-list -property name, CanPauseAndContinue[/code:1]
以列表方式显示Alerter服务的所有属性的值:
[code:1]get-service alerter | format-list -property *[/code:1]
以表格形式显示所有服务CanPauseAndContinue属性的名称和值:
[code:1]get-service | format-table -property name, CanPauseAndContinue[/code:1]
方法类型(Method types)代表对象的方法,即你可以对该对象施加的行为。比如:ServiceController对象有Stop方法,可以让你停止服务。
要调用服务对象的行为,用以下格式,(务必包括括号):
[code:1](get-service <service-name>).<method-name>()[/code:1]
比如
[code:1](get-service schedule).stop()[/code:1]
停止schedule服务。