如何在 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

更新於:2020 年 1 月 22 日

3K+ 瀏覽量

啟動你的 職業生涯

完成該課程獲得認證

開始
廣告