Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{
"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
You can’t perform that action at this time.