我应该使用什么命令来获取PowerShell上的全面服务信息?

au9on6nz  于 2022-11-10  发布在  Shell
关注(0)|答案(2)|浏览(254)

我需要创建一个RPC来检索和修改服务状态,基于在服务UI中可见的“Log on As”字段。Get-Service命令只为我提供状态、名称和DisplayName,但我需要更多信息。我应该对Get-Service使用修饰符,还是有其他方法来解决这个问题?
log on field in Services UI

9jyewag0

9jyewag01#

默认情况下,“Get-Service”可以为您提供更多功能。许多Cmdlet都有格式定义,默认情况下仅显示特定属性。
您可以使用“Format-List*-Force”在顶层(=直接成员)上看到更多内容

PS> Get-Service -Name 'BITS' | fl * -Force

UserName            : LocalSystem
Description         : Transfers files in the background using idle network bandwidth. If the service is disabled, then any applications that depend on BITS, such as Windows Update or MSN
                      Explorer, will be unable to automatically download programs and other information.
DelayedAutoStart    : True
BinaryPathName      : C:\WINDOWS\System32\svchost.exe -k netsvcs -p
StartupType         : Manual
Name                : BITS
RequiredServices    : {RpcSs}
CanPauseAndContinue : False
CanShutdown         : False
CanStop             : False
DisplayName         : Background Intelligent Transfer Service
DependentServices   : {}
MachineName         : .
ServiceName         : BITS
ServicesDependedOn  : {RpcSs}
StartType           : Manual
ServiceHandle       :
Status              : Stopped
ServiceType         : Win32ShareProcess
Site                :
Container           :

但是有很多间接的(嵌套的)成员。您可以通过使用“Get-Members”或使用它们(例如$service.RequiredServices)来发现它们。

PS> $service | Get-Member -MemberType Property

   TypeName: System.Service.ServiceController#StartupType

Name                MemberType Definition
----                ---------- ----------
BinaryPathName      Property   System.String {get;set;}
CanPauseAndContinue Property   bool CanPauseAndContinue {get;}
CanShutdown         Property   bool CanShutdown {get;}
CanStop             Property   bool CanStop {get;}
Container           Property   System.ComponentModel.IContainer Container {get;}
DelayedAutoStart    Property   System.Boolean {get;set;}
DependentServices   Property   System.ServiceProcess.ServiceController[] DependentServices {get;}
Description         Property   System.String {get;set;}
DisplayName         Property   string DisplayName {get;set;}
MachineName         Property   string MachineName {get;set;}
ServiceHandle       Property   System.Runtime.InteropServices.SafeHandle ServiceHandle {get;}
ServiceName         Property   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;}
StartType           Property   System.ServiceProcess.ServiceStartMode StartType {get;}
StartupType         Property   Microsoft.PowerShell.Commands.ServiceStartupType {get;set;}
Status              Property   System.ServiceProcess.ServiceControllerStatus Status {get;}
UserName            Property   System.String {get;set;}

这里是扩展视图(最高可达3层;还有更多)

[ServiceController] BITS
>   UserName                           : LocalSystem
>   Description                        : Transfers files in the background using idle network bandwidth. If the service is disabled, then any applications that depend on BITS, such as Windows Update or MSN Explorer, will be unable to automatically download programs and other information.
>   DelayedAutoStart                   : True
>   BinaryPathName                     : C:\WINDOWS\System32\svchost.exe -k netsvcs -p
>   StartupType                        : [ServiceStartupType] Manual
>   >   value__                            : 3
>   Name                               : BITS
>   RequiredServices                   : @[ServiceController[]]
>   >   [0] <RequiredServices>             : [ServiceController] RpcSs
>   >   >   Name                               : RpcSs
>   >   >   RequiredServices                   : @{[ServiceController[]]} ~[0..2] RpcEptMapper , DcomLaunch
>   >   >   CanPauseAndContinue                : False
>   >   >   CanShutdown                        : False
>   >   >   CanStop                            : False
>   >   >   DisplayName                        : Remote Procedure Call (RPC)
>   >   >   DependentServices                  : 
>   >   >   MachineName                        : .
>   >   >   ServiceName                        : RpcSs
>   >   >   ServicesDependedOn                 : @{[ServiceController[]]} ~[0..2] RpcEptMapper , DcomLaunch
>   >   >   StartType                          : {[ServiceStartMode]} Automatic
>   >   >   ServiceHandle                      : 
>   >   >   Status                             : {[ServiceControllerStatus]} Running
>   >   >   ServiceType                        : {[ServiceType]} Win32ShareProcess
>   >   >   Site                               : 
>   >   >   Container                          : 
>   CanPauseAndContinue                : False
>   CanShutdown                        : False
>   CanStop                            : False
>   DisplayName                        : Background Intelligent Transfer Service
>   DependentServices                  : @[ServiceController[]]
>   MachineName                        : .
>   ServiceName                        : BITS
>   ServicesDependedOn                 : @[ServiceController[]]
>   >   [0] <ServicesDependedOn>           : [ServiceController] RpcSs
>   >   >   Name                               : RpcSs
>   >   >   RequiredServices                   : @{[ServiceController[]]} ~[0..2] RpcEptMapper , DcomLaunch
>   >   >   CanPauseAndContinue                : False
>   >   >   CanShutdown                        : False
>   >   >   CanStop                            : False
>   >   >   DisplayName                        : Remote Procedure Call (RPC)
>   >   >   DependentServices                  : 
>   >   >   MachineName                        : .
>   >   >   ServiceName                        : RpcSs
>   >   >   ServicesDependedOn                 : @{[ServiceController[]]} ~[0..2] RpcEptMapper , DcomLaunch
>   >   >   StartType                          : {[ServiceStartMode]} Automatic
>   >   >   ServiceHandle                      : 
>   >   >   Status                             : {[ServiceControllerStatus]} Running
>   >   >   ServiceType                        : {[ServiceType]} Win32ShareProcess
>   >   >   Site                               : 
>   >   >   Container                          : 
>   StartType                          : [ServiceStartMode] Manual
>   >   value__                            : 3
>   ServiceHandle                      : 
>   Status                             : [ServiceControllerStatus] Stopped
>   >   value__                            : 1
>   ServiceType                        : [ServiceType] Win32ShareProcess
>   >   value__                            : 32
>   Site                               : 
>   Container                          : 
---------------------------------------------------------------------------
enyaitl3

enyaitl32#

另一种选择是使用WMIC,它提供了更多信息:

Get-CimInstance Win32_Service -Filter 'name = "wuauserv"' | fl *

相关问题