繁体   English   中英

停止服务,运行批处理文件,然后启动服务

[英]Stop services, run batch files, then start services

我必须停止多个服务,检查服务是否正确停止,然后运行多个批处理文件。 批处理作业完成后,首先重新启动已停止的服务。 我开始了以下脚本,但无法更进一步。

#Define Services
$service1 = 'StiSvc'
$service2 = 'AdobeARMservice'
$services = @(
  $service1,
  $service2
)

#Stop Services
Get-Service | 
  Where { $services -contains $_.Name } |
  Foreach {
    $_ | Stop-Service
  }

#Verify Services
Get-Service | 
  Where { $services -contains $_.Name } |
  Foreach {
    if ((Get-Service $_.Name).Status -eq "stopped") {
      Write-Host 'Service Stop Pass (0)'
    } else {
      Write-Host 'Service Stop Failed (1000)';
      exit '1000'
    }
  }

#Start batch
if ($services.Status -eq "Stopped") {
  Start-Process "cmd.exe" "/c C:\download\hello.bat"
} else {
  Start-Sleep -s 10
} 

以下应该工作:

$services = 'StiSvc', 'AdobeARMservice'

Stop-Service $services -ErrorAction Stop

& 'C:\download\hello.bat'
& 'C:\path\to\other.cmd'
#...

Start-Service $services

如果没有,您需要提供有关究竟是什么失败以及如何失败的更多信息。 包括所有错误和状态消息。

谢谢大家,我修改了脚本如下。 该脚本停止 2 个服务,当 2 个服务停止时,它会启动批处理文件。

#Stop Services

Get-Service StiSvc, AdobeARMservice | Stop-Service

#Verify Services

if (((Get-Service StiSvc).Status -eq "stopped") -and 
((Get-Service AdobeARMservice).Status -eq "stopped"))

# Start batch

{Start-Process  "cmd.exe"  "/c C:\download\hello.bat"} 

我正在努力改进脚本。 我会尽快回到你身边。 问候

暂无
暂无

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

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