当前位置:   article > 正文

如何在Powershell中使用Get-Service命令检查Windows服务状态

get-service

Service are one of the most important part of the operating systems. There are different tools and commands to list these services. Powershell provides Get-Service commandlet in order to list these services and filter them acording to the provided filter.

服务是操作系统中最重要的部分之一。 有不同的工具和命令来列出这些服务。 Powershell提供了Get-Service命令集,以便列出这些服务并根据提供的过滤器对其进行过滤。

列出所有服务 (List All Services)

We will start with a simple use case where we will list all services without providing an options to the Get-Service command. This will list currently existing service with running or stopped status.

我们将从一个简单的用例开始,在该用例中,我们将列出所有服务,而不为Get-Service命令提供选项。 这将列出处于运行或停止状态的当前现有服务。

PS> Get-Service
List All Services 
List All Services 
列出所有服务

As we can see from screenshot services are ordered by name by default. Following information is provided by default about listed services.

从屏幕截图中可以看出,默认情况下,服务按名称排序。 默认情况下,提供以下有关所列服务的信息。

  • Status will display if the service is Running or Stopped

    如果服务正在Running或已Stopped将显示Status

  • Name will display the real and short name of the service which is used by commands.

    Name将显示命令使用的服务的真实名称和简称。

  • Display Name will show extended and informative name of the service which is more human friendly.

    Display Name将显示服务的扩展Display Name和信息性名称,更人性化。

列出远程计算机或系统上的所有服务 (List All Service On Remote Computer or System)

As Microsoft provided remote access and management of the remote systems with powreshell we can use Get-Service command in order to list services on the remote systems. We will provide the -computername of the remote system. This can a DNS name which can be resolved or a IP address we can access.

由于Microsoft通过powreshell提供了对远程系统的远程访问和管理,因此我们可以使用Get-Service命令来列出远程系统上的服务。 我们将提供远程系统的-computername 。 这可以是可以解析的DNS名称,也可以是我们可以访问的IP地址。

PS > Get-Service -computername DESKTOP-HQVAMA3
List All Service On Remote Computer or System
List All Service On Remote Computer or System
列出远程计算机或系统上的所有服务

列出具有指定名称的服务(List Services with Specified Name)

Windows operating systems has a lot of services which will fill the terminal easily. Or we may need to list specific service name by providing its name of some part of its name. We will just provide the name after Get-Service command. We can also use * glob where only some part of the service name will be specified. In this example we will try to list WinDefend service by providing WinDef* as service name.

Windows操作系统有很多服务,可以很容易地填充终端。 或者,我们可能需要通过提供其名称中某些部分的名称来列出特定的服务名称。 我们将仅在Get-Service命令后提供名称。 我们也可以使用* glob,其中仅指定服务名称的一部分。 在此示例中,我们将尝试通过提供WinDef*作为服务名称来列出WinDefend服务。

PS > Get-Service WinDef*
List Services with Specified Name
List Services with Specified Name
列出具有指定名称的服务

仅列出当前正在运行的服务(List Only Currently Running Services)

There is two main service status in Windows. Running or Stopped . We may need to list services according to their status. We can use object parameter Status like below which will list only running services.

Windows中有两种主要服务状态。 Running或已Stopped 。 我们可能需要根据服务状态列出服务。 我们可以使用如下所示的对象参数Status ,它将仅列出正在运行的服务。

PS> Get-Service | Where-Object {$_.Status -eq "Running"}
List Only Currently Running Services
List Only Currently Running Services
仅列出当前正在运行的服务

列出服务和相关服务(List Service with Dependent Service)

Services are important and may be linked and dependent to each other. Before stopping them we may need to list dependent services of the given service. We can use -RequiredServices options in order to list service dependencies.

服务很重要,可以相互链接并相互依赖。 在停止它们之前,我们可能需要列出给定服务的依赖服务。 我们可以使用-RequiredServices选项以列出服务依赖性。

PS> Get-Service "WinDefend" -RequiredServices
List Service with Dependent Service
List Service with Dependent Service
列出服务和相关服务

列表按状态排序(已停止/正在运行)(List Sorted By Status (Stopped/Running))

We may need to check service list according to their Running and Stopped status. We can sort them by usingSort-Object like below.

我们可能需要根据服务的“正在运行”和“已停止”状态检查服务列表。 我们可以使用如下所示的Sort-Object对它们进行Sort-Object

PS> Get-Service | Sort-Object status
List Sorted By Status (Stopped/Running)
List Sorted By Status (Stopped/Running)
列表按状态排序(已停止/正在运行)

将服务列表写入文件 (Write Service List Into A File)

We may need to investigate service list later or we just want to save them in to a file. We can use Out-File command which will write the service list the file we want. In this example we will write into a file named Services.txt

我们可能稍后需要调查服务列表,或者我们只想将它们保存到文件中。 我们可以使用Out-File命令将服务列表写入所需的文件。 在此示例中,我们将写入名为Services.txt的文件

PS> Get-Service | Out-File "C:\Users\İsmail Baydan\Desktop\Services.txt"
LEARN MORE  Windows Update Service Not Running Error for Windows 7, Windows 8, Windows 10, Windows Server 2008, 2012, 2016
了解更多信息Windows 7,Windows 8,Windows 10,Windows Server 2008、2012、2016的Windows Update服务未运行错误

翻译自: https://www.poftut.com/how-to-check-windows-service-status-with-get-service-command-in-powershell/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/392263
推荐阅读
相关标签
  

闽ICP备14008679号