0

Hi Guys,

I’m currently working on a PS Script that will monitor Windows service(s) that has a startup type of automatic but is not running.

The script below  is working fine if you apply it to individual computers or just a group. However, if you apply it to another group/multiple groups(group of windows servers) the monitor will stop working and will only work on the original group.

I’m kinda stuck here and I’m not really sure if the problem is with actual PS script or if it’s on how i set up the properties of the script.

 

# Step 1. Query WMI for a list of services that are set to start automatically but are not running = where {($_.StartType -eq ‘Automatic’ ) -and ($_.Status -eq “Stopped”)}
# Add to dictionary
# Step 2. Remove any excluded services from the list of services(set via param)
# Step 3. If any failed services were found return unhealthy othewise return healthy
# Step 4. Add the number of failed services to the property bag

# Output: Display the list of failed Services
# Display total number of failed services detected.

 

param(
$Arguments, # Mandatory
[Parameter(HelpMessage=”Services that will be excluded from monitoring”)][string]$ExcludedServices
)

. ([Scriptblock]::Create($Arguments))

$ScomAPI = New-Object -comObject “MOM.ScriptAPI”
$PropertyBag = $ScomAPI.CreatePropertyBag()

$arrExclusions = $ExcludedServices.Split(“,”).Trim()

$objStoppedAutoServices = Get-Service | where {($_.StartType -eq ‘Automatic’ ) -and ($_.Status -eq “Stopped”)}

$strFailedServices = “”

#Setup hash table & add all services that are set to automatic but has status of ‘Stopped’/or is not running
$ServicesList = @{}

ForEach($Service in $objStoppedAutoServices)
{
$ServicesList.add($Service.Name, $service.Name + “(” + $Service.DisplayName + “)”)

}

#write-host “Added” $ServicesList.Count “items to our hash table”

 

#Removed excluded services from our table
ForEach($Service in $objStoppedAutoServices) {

foreach($Exclusion in $arrExclusions)
{
if ($Service -like $Exclusion) {

$ServicesList.Remove($service.Name)
#Write-Host “Removing ” $Service.Name “in the list”
}
}

}

 

#this will be use to display all services that failed
ForEach($objService in $ServicesList.Keys)
{
$strFailedServices += $ServicesList.Item($objService) + “rn”
}

 

If($ServicesList.Count -gt 0 ) {

#Found some unhealthy Service(s)
$PropertyBag.AddValue(“State”, “Unhealthy”)

} else {
#write-host “Health state = healthy”
$PropertyBag.AddValue(“State”, “Healthy”)
}

 

$PropertyBag.AddValue(“NumberOfFailedServices”, $ServicesList.Count)
$PropertyBag.AddValue(“FailedServices”, $strFailedServices)

 

# Send output to SCOM
$PropertyBag

Graham Davies commented