如何通过powershell脚本显示正在运行的进程数

时间:2018-04-03 04:14:23

标签: powershell windows-server-2012

试图找出如何通过PowerShell脚本获取运行服务的计数。我知道Get-Service将为我们提供系统上所有进程的列表和(Get-Service)。伯爵会给予计数。但问题是我们如何才能在系统中找到Running Services的计数?我简化了过滤条件,以获取仅Get-Service | Where-Object {$_.Status -eq "Running"}运行服务的列表,但这些服务的数量没有任何问题。

1 个答案:

答案 0 :(得分:2)

<强> TL; DR

(Get-Service | Where-Object Status -eq "Running").Count

您是否听说过Get-Member CmdLet?

如果没有;在这里阅读:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-member?view=powershell-6

  

获取对象的属性和方法。

在类似的情况下,我发现这是最有用的命令之一。习惯使用它,因为它会非常方便!

这对你有什么帮助?嗯......

Get-Service | Get-Member

返回:

   TypeName: System.ServiceProcess.ServiceController

Name                      MemberType    Definition
----                      ----------    ----------
Name                      AliasProperty Name = ServiceName
RequiredServices          AliasProperty RequiredServices = ServicesDependedOn
Disposed                  Event         System.EventHandler Disposed(System.Object, System.EventArgs)
Close                     Method        void Close()
Continue                  Method        void Continue()
CreateObjRef              Method        System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
Dispose                   Method        void Dispose(), void IDisposable.Dispose()
Equals                    Method        bool Equals(System.Object obj)
ExecuteCommand            Method        void ExecuteCommand(int command)
GetHashCode               Method        int GetHashCode()
GetLifetimeService        Method        System.Object GetLifetimeService()
GetType                   Method        type GetType()
InitializeLifetimeService Method        System.Object InitializeLifetimeService()
Pause                     Method        void Pause()
Refresh                   Method        void Refresh()
Start                     Method        void Start(), void Start(string[] args)
Stop                      Method        void Stop()
WaitForStatus             Method        void WaitForStatus(System.ServiceProcess.ServiceControllerStatus desiredStat...
CanPauseAndContinue       Property      bool CanPauseAndContinue {get;}
CanShutdown               Property      bool CanShutdown {get;}
CanStop                   Property      bool CanStop {get;}
Container                 Property      System.ComponentModel.IContainer Container {get;}
DependentServices         Property      System.ServiceProcess.ServiceController[] DependentServices {get;}
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;}
Status                    Property      System.ServiceProcess.ServiceControllerStatus Status {get;}
ToString                  ScriptMethod  System.Object ToString();

不是很酷吗?!

快速查看可用的属性(Get-Service | Get-Member -MemberType Property) - 发现有用的东西吗?

(Get-Service | Where-Object Status -eq "Running").Count