FrenchITGuy
FrenchITGuy

Reputation: 11

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
} 

Upvotes: 1

Views: 1560

Answers (2)

FrenchITGuy
FrenchITGuy

Reputation: 11

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

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200213

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.

Upvotes: 1

Related Questions