【问题标题】:Stop services, run batch files, then start services停止服务,运行批处理文件,然后启动服务
【发布时间】:2016-10-18 09:16:51
【问题描述】:

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

#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
} 

【问题讨论】:

  • 真正的问题是什么?
  • 那么,您的问题是什么?什么没有按预期工作?你“不能走得更远”……怎么办?
  • 它没有按预期工作。服务停止,但批处理文件未启动。
  • 尝试使用调用操作符来运行批处理文件:& "C:\download\hello.bat"(就像那样,没有Start-Processcmd /c
  • 我试过了,但到目前为止没有任何乐趣。

标签: windows powershell batch-file


【解决方案1】:

以下应该有效:

$services = 'StiSvc', 'AdobeARMservice'

Stop-Service $services -ErrorAction Stop

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

Start-Service $services

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

【讨论】:

    【解决方案2】:

    谢谢大家, 我修改了脚本如下。该脚本停止 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"} 
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-27
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      相关资源
      最近更新 更多