Windows Powershell provides easy to use commands or command-lets to manage Windows systems. Powershell can be used to manage Windows Services and provides builtin command for related operations.
Windows Powershell提供了易于使用的命令或命令集来管理Windows系统。 Powershell可用于管理Windows服务,并提供用于相关操作的内置命令。
帮帮我 (Help)
Help information about Powershell Services command can be get wit the following command.
可以通过以下命令获取有关Powershell Services命令的帮助信息。
$ Get-Help Get-Service
使用Get-Service列出服务(List Services with Get-Service)
Existing Windows services can be listed with the Powershell command Get-Service
. This will list all services currently running, stopped, suspended.
可以使用Powershell命令Get-Service
列出现有的Windows Get-Service
。 这将列出当前正在运行,停止,暂停的所有服务。
$ Get-Service
While listing services information about the service state, service name and service display name will be provided in separate columns.
在列出有关服务状态的服务信息时,将在单独的列中提供服务名称和服务显示名称。
仅列出正在运行的服务 (Only List Running Services)
In previous example we simply listed services but we may need to list only running services. In order to filter running services we will use Linux grep equivalent Where-Object
like below.
在前面的示例中,我们仅列出了服务,但是我们可能仅需要列出正在运行的服务。 为了过滤正在运行的服务,我们将使用Linux grep等效的Where-Object
如下所示。
$ Get-Service | Where-Object {$_.status -eq "running"}
启动服务(Start Service)
One of the most used practice of service command is starting the service. Syntax of starting service command is the same we just change the Verb part of the service command and use Start-Service by providing the service name. In this example we will start the service named RasAuto
.
服务命令最常用的一种做法是启动服务。 启动服务命令的语法相同,我们只是更改了服务命令的动词部分,并通过提供服务名称来使用Start-Service。 在此示例中,我们将启动名为RasAuto
的服务。
$ Start-Service RasAuto
停止服务 (Stop Service)
Stopping a service is similar to starting we just change start to stop like below. In the example we will stop service named RasAuto
停止服务类似于启动,我们只是将启动更改为停止,如下所示。 在示例中,我们将停止名为RasAuto
服务
$ Stop-Service RasAuto
重新启动服务 (Restart Service)
Restarting service is easy as starting and stopping service we will just use Restart-Service
command with the service name. Restart will stop and start service if it is working. If it is not working service will just started.
重新启动服务很容易,因为启动和停止服务我们将只使用带有服务名称的Restart-Service
命令。 如果重新启动,它将停止并启动服务。 如果它不工作,则服务将刚刚启动。
$ Restart-Service RasAuto
暂停服务 (Suspend Service)
In Windows services can be suspended. It is similar to stopping the service but there are some differences. Suspended service will hold current situations and related data and variables. Also the service stop triggers will not run. In this example we will suspend service named MyService
.
在Windows中,可以暂停服务。 它类似于停止服务,但有一些区别。 暂停服务将保存当前情况以及相关数据和变量。 而且,服务停止触发器将不会运行。 在此示例中,我们将暂停名为MyService的服务
。
$ Suspend-Service MyService
In order to suspend a service the service should support suspension. If the service do not support suspension and we try to suspend the service we will get an error like below.
为了暂停服务,服务应支持暂停。 如果该服务不支持暂停,而我们尝试暂停该服务,则会收到如下错误。
简历服务(Resume Service)
Suspended service can be resumed with the Resume-Service
command like below.
可以使用如下的Resume-Service
命令来恢复暂停的服务。
$ Resume-Service MyService
翻译自: https://www.poftut.com/get-service-service-management-with-windows-powershell-from-command-line/