简体   繁体   English

使用Powershell停止服务,重新启动并启动服务

[英]Stop the Service, Restart and Start the service using Powershell

I need to Stop the service, and once the service is stopped i need to reboot the system. 我需要停止服务,并且一旦服务停止,我就需要重新启动系统。 After the reboot i need to start the service if it is in stopped state using powershell script. 重新启动后,如果使用Powershell脚本处于停止状态,我需要启动该服务。

$stage= get-service -name "chrome"

if ( $stage.Status -eq "Running" )
{

Write-Host "The service " $stage.Name "is running"

Stop-Service -Name 'chrome'



}


if ( $stage.Status -eq "Stopped")

{
Restart-Computer -Wait

Start-Service 'chrome'


}

You may need to run the code from different server to achieve your goal. 您可能需要从其他服务器运行代码以实现您的目标。 as suggested above, once server rebooted, you can't resume back with the same script. 如上所述,服务器重新启动后,您将无法使用相同的脚本恢复原状。

Other than that, there is small change needs to be done you script. 除此之外,您还需要对脚本进行一些小的更改。 which is, you need to call again below code between your if conditions. 也就是说,您需要在if条件之间再次调用以下代码。 So that you will get a expected output 这样您将获得预期的输出

$stage= get-service -name "chrome" $ stage = get-service -name“ chrome”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM