Skip to content

Instantly share code, notes, and snippets.

@RandomNoun7
Last active November 24, 2021 00:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RandomNoun7/03dfb910e5d93fefaae6e6c2da625c44 to your computer and use it in GitHub Desktop.
Save RandomNoun7/03dfb910e5d93fefaae6e6c2da625c44 to your computer and use it in GitHub Desktop.
{
"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]"
}
}
}
[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