Skip to content

Instantly share code, notes, and snippets.

@perzizzle
Created June 15, 2016 19:20
Show Gist options
  • Select an option

  • Save perzizzle/72ea85c51ac0512d1fdb1a22009157a0 to your computer and use it in GitHub Desktop.

Select an option

Save perzizzle/72ea85c51ac0512d1fdb1a22009157a0 to your computer and use it in GitHub Desktop.
service control
Param(
[Parameter(Mandatory=$True,Position=1)] [string] $cmd,
[Parameter(Mandatory=$True,Position=2)] [string] $serviceName
)
$Services = Get-Service | Where-Object {$_.name -like $serviceName}
foreach ($Service in $Services) {
$name = $Service.name
$status = $Service.status
IF ($cmd -like "*AUTO*" ) {
Start-Job -ScriptBlock {param($name) Set-Service $name -startuptype AUTOMATIC} -ArgumentList $name -Name $name
} ELSEIF ($cmd -like "*MANUAL*") {
Start-Job -ScriptBlock {param($name) Set-Service $name -startuptype MANUAL} -ArgumentList $name -Name $name
} ELSEIF ($cmd -like "*DISABLE*" ) {
#This will allow you to disable a running service
Start-Job -ScriptBlock {param($name) Set-Service $name -startuptype DISABLED} -ArgumentList $name -Name $name
}
}
While ($(Get-Job -State Running) -ne $null){
Write-Output "Waiting for jobs to finish"
Start-Sleep -Milliseconds 5000
}
ForEach($Job in Get-Job){
Write-Output "===Job Complete: $($Job.Name)"
Receive-Job $Job
}
Get-Job | Remove-Job
foreach ($Service in $Services) {
$name = $Service.name
$status = $Service.status
IF ($cmd -like "*RESTART*") {
Start-Job -ScriptBlock {param($name) Restart-Service $name} -ArgumentList $name -Name $name
} ELSEIF ($cmd -like "*START*") {
IF ($status -eq "STOPPED" ) {
Start-Job -ScriptBlock {param($name) Start-Service $name} -ArgumentList $name -Name $name
}
ELSE {
Write-Output "Cannot start Service $name is $status"
}
} ELSEIF ($cmd -like "*STOP*") {
IF ($status -eq "RUNNING" ) {
Start-Job -ScriptBlock {param($name) Stop-Service $name} -ArgumentList $name -Name $name
}
ELSE {
Write-Output "Cannot stop Service $name is $status"
}
} ELSEIF ($cmd -like "*STATUS*") {
Write-Output "Service $name is $status"
}
}
While ($(Get-Job -State Running) -ne $null){
Write-Output "Waiting for jobs to finish"
Start-Sleep -Milliseconds 5000
}
ForEach($Job in Get-Job){
Write-Output "===Job Complete: $($Job.Name)"
Receive-Job $Job
}
Get-Job | Remove-Job
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment