如何在PowerShell中获取该服务可用的所有属性和方法?

若要显示可用于get-service cmdlet的所有属性和方法,您需要管道传输Get-Member(别名gm)。MemberType'Property'用于显示特定的属性,例如机器名,服务名等。使用MemberType'Method'可以对对象执行特定的操作,例如,启动,停止,暂停服务等。

命令

下面的命令用于显示Get-Service的所有成员(属性,方法)。

Get-Service | Get-Member

输出结果

Name                         MemberType
----                         ----------
Name                      AliasProperty
RequiredServices          AliasProperty
Disposed                          Event
Close                            Method
Continue                         Method
CreateObjRef                     Method
Dispose                          Method
Equals                           Method
ExecuteCommand                   Method
GetHashCode                      Method
GetLifetimeService               Method
GetType                          Method
InitializeLifetimeService        Method
Pause                            Method
Refresh                          Method
Start                            Method
Stop                             Method
WaitForStatus                    Method
CanPauseAndContinue            Property
CanShutdown                    Property
CanStop                        Property
Container                      Property
DependentServices              Property
DisplayName                    Property
MachineName                    Property
ServiceHandle                  Property
ServiceName                    Property
ServicesDependedOn             Property
ServiceType                    Property
Site                           Property
StartType                      Property
Status                         Property
ToString                   ScriptMethod

命令

仅获取属性。

Get-Service | Get-Member | where{$_.MemberType -eq "Property"}

输出结果

Name                MemberType
----                ----------
CanPauseAndContinue   Property
CanShutdown           Property
CanStop               Property
Container             Property
DependentServices     Property
DisplayName           Property
MachineName           Property
ServiceHandle         Property
ServiceName           Property
ServicesDependedOn    Property
ServiceType           Property
Site                  Property
StartType             Property
Status                Property

命令

仅获得方法。

输出结果

Get-Service | Get-Member | where{$_.MemberType -eq "Method"}

输出结果

Name                      MemberType
----                      ----------
Close                         Method
Continue                      Method
CreateObjRef                  Method
Dispose                       Method
Equals                        Method
ExecuteCommand                Method
GetHashCode                   Method
GetLifetimeService            Method
GetType                       Method
InitializeLifetimeService     Method
Pause                         Method
Refresh                       Method
Start                         Method
Stop                          Method
WaitForStatus                 Method