简体   繁体   中英

Stop services, run batch files, then start services

I've to stop multiples services, check the services if they are correctly stopped THEN run multiple batch files. When the batch jobs are done, start again the stopped services at the first place. I started the following script but could not go further.

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

The following should work:

$services = 'StiSvc', 'AdobeARMservice'

Stop-Service $services -ErrorAction Stop

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

Start-Service $services

If it doesn't, you need to provide more information about what exactly fails, and how. Include all error and status messages.

Thank you all, I modified the script as follow. The script stops 2 services, when the 2 services are stopped it start the batch file.

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

I'm working on to improve the script. I'll come back to you asap. Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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