function Test-ServiceRunning {
param(
[Parameter(Mandatory=$true)]
[String]$ServiceName
)
$ValidServiceNames = Get-Service | Select-Object -Property "Name"
try {
# Test if valid service name
if ($ValidServiceNames.Name -contains $ServiceName) {
#Write-Host "Service name exists and can be used!"
$ServiceToCheck = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($ServiceToCheck | Where-Object {$_.Status -eq "Running"}) {
#Write-Host "Running"
return $true
} else {
#Write-Host "Not Running"
return $false
}
} else {
Write-Host "Not a valid service name!"
exit
}
} catch {
Write-Error "Error in processing request"
}
}
Leave a Reply