Last active
November 24, 2021 00:58
-
-
Save RandomNoun7/03dfb910e5d93fefaae6e6c2da625c44 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"puppet_task_version": 1, | |
"supports_noop": false, | |
"description": "Stop or restart a service or list of services on a node.", | |
"parameters": { | |
"service": { | |
"description": "The name of the service, or a list of service names to stop.", | |
"type": "Variant[Array[String],String]" | |
}, | |
"norestart": { | |
"description": "Immediately restart the service(s) after start.", | |
"type": "Optional[Boolean]" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
param ( | |
# Name or list of service names to stop | |
[Parameter(Mandatory=$true)] | |
[string[]] | |
$service, | |
# Restart the service immediately | |
[Parameter(Mandatory=$false)] | |
[switch] | |
$norestart | |
) | |
foreach ($name in $service) { | |
try{ | |
$serviceObject = Get-service -Name $name | |
Write-output "Found Service: $name" | |
if(-not $serviceObject){ | |
Write-Output "Service not found: $name" | |
continue | |
} | |
Stop-Service -InputObject $serviceObject -ErrorAction Stop | |
Write-Output "Stopped service: $name" | |
if($norestart){continue} | |
Start-Service -InputObject $serviceObject | |
Write-Output "Started service: $name" | |
} catch { | |
Write-Output "Cannot stop service: $name" | |
Write-Output "Dependent services: $($serviceObject.dependentservices)" | |
exit 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment