Files
management-scripts/Remediation-RestartServices.ps1
T
2019-11-11 19:22:39 -05:00

36 lines
958 B
PowerShell

param(
[Int32]$Wait=0,
[Parameter(Mandatory=$true)]
[string]$Services
)
## Wait for the specified amount of time before proceeding
if ( $Wait -gt 0 ) { Start-Sleep -Seconds $Wait }
##Split the list of services so we can process each one
$Services.Split(",") | ForEach {
## Get the service's status
$service = $_
$status = (Get-Service -Name $service -ErrorAction SilentlyContinue).Status
## Make sure the service exists
if ($status) {
## Make sure the service isn't currently running
if ($status -ne "Running") {
## Try to restart the service
Write-Host "Restarting ${service}"
try { Restart-Service -Name $service -Force -ErrorAction Stop }
## Write any errors to the host
catch { Write-Host $_ }
} else { Write-Host "${service} is already running" }
} else { Write-Host "${service} does not exist" }
}