Dear support,

The script is not working correctly, he indicates a "Ok" but there are Automatic services stopped right now. What am I doing wrong.

#===========================
# ___ ___ _____ ___
#| _ \ _ \_  _/ __|
#|  _/   / | ||(_ |
#|_| |_|_\ |_|\___|
#    NETWORK MONITOR
#-------------------
# Description:     This script will iterate through the windows services that are set to automatic starting and alert
#                 if they don't.
# Parameters:
# -ComputerName - The name of the computer you want to check for its service (ip is okay too)
# -IgnoreList: The services that are ignored by the script (like google update services) 
# -Username: The username and the domain/computer name that applies to the target hosts
# -Password: The password for the given user.
# Example: 
# Get-Services.ps1 -ComputerName %host -Username "%windowsdomain\%windowsuser" -Password "%windowspassword" -IgnoreList "Service1,Service2"
# Values with % will be replaced by PRTG automatically.
# ------------------
# (c) 2014 Stephan Linke | Paessler AG
param(
    [string]$ComputerName = "localhost",
    [string]$IgnoreList = "Google Update,Software Protection,Remote Registry",
    [string]$UserName = "XXXXXX",
    [string]$Password = "XXXX"
)

# Error if there's anything going on
$ErrorActionPreference = "Stop"

# Generate Credentials Object
$SecPasswd  = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials= New-Object System.Management.Automation.PSCredential ($UserName, $secpasswd)

# hardcoded list that applies to all hosts
$IgnoreScript = 'Google Update Service (gupdate)'
$IgnoreCombined = @($IgnoreList) + @($IgnoreScript)

$Ignore = $IgnoreCombined -Split ","

# Get list of services that are not running, not in the ignore list and set to automatic. 
Try{ $Services = Get-WmiObject Win32_Service -ComputerName $ComputerName -Credential $Credentials | Where {$_.StartMode -eq 'Auto' -and $Ignore -notcontains $_.DisplayName -and $_.State -ne 'Running'}  }
# If the script runs for the PRTG server itself, we don't need credentials
Catch{ $Services = Get-WmiObject Win32_Service -ComputerName $ComputerName | Where {$_.StartMode -eq 'Auto' -and $Ignore -notcontains $_.DisplayName -and $_.State -ne 'Running'}  }

if($Services){
    $ServiceList = ($Services | Select -expand DisplayName) -join ", "
    if(!$Services.Count){
        Write-Host "1:Automatic service(s) not running:"$ServiceList
        exit 1
    }
    else{
    Write-Host $Services.Count":Automatic service(s) not running:"$ServiceList
    exit 1
    }
}
else{
    Write-Host "0:OK"
    exit 0
}
#===========================




---- logs(C:\ProgramData\Paessler\PRTG Network Monitor\Logs (Sensors) )----

Result of Sensor 5971.txt

0:OK

Result of Sensor 5971.Data.txt

Data['blockedsens'].asString := '';
Data['canlinux'].asString := '1';
Data['channel'].asString := 'Value';
Data['channelinfos'].asString := '{"1":{"Unit":"oukTimeResponse","CustomUnit":"","ValueLookupName":"","LimitMode":"0","LimitMinError":"","LimitMinWarning":"","LimitMaxError":"","LimitMaxWarning":""},"2":{"Unit":"oukCustom","CustomUnit":"#","ValueLookupName":"","LimitMode":"0","LimitMinError":"","LimitMinWarning":"","LimitMaxError":"","LimitMaxWarning":""}}';
Data['channelnames'].asString := 'Execution Time'#$D#$A + 
'1'#$D#$A + 
'Value'#$D#$A + 
'2'#$D#$A + 
'';
Data['environment'].asString := '';
Data['exefile'].asString := 'AutomaticServices.ps1';
Data['exeparams'].asString := '';
Data['fastcount'].asString := '0';
Data['host'].asString := 'XXXXXX';
Data['hostv6'].asString := '';
Data['inerror'].asString := '0';
Data['interfacenumber'].asString := '';
Data['inum'].asString := '';
Data['ipversion'].asString := '0';
Data['isexesensor'].asString := '1';
Data['lastmsg'].asString := 'OK';
Data['lastuptime'].asString := '0';
Data['lastvalue'].asString := '0';
Data['linuxlogindomain'].asString := '';
Data['linuxloginpassword'].asString := '***';
Data['monitorchange'].asString := '0';
Data['mutexname'].asString := '';
Data['reboot'].asString := '42576.9336798148';
Data['resultfile'].asString := 'Result of Sensor 5971.txt';
Data['sensorid'].asString := '5971';
Data['simulate'].asString := '0';
Data['timeout'].asString := '60';
Data['tlsexplicit_default'].asString := '';
Data['tlsexplicit_ftp'].asString := '';
Data['tlsexplicit_imap'].asString := '';
Data['tlsexplicit_pop3'].asString := '';
Data['tlsexplicit_port'].asString := '';
Data['tlsexplicit_smtp'].asString := '';
Data['unit'].asString := '#';
Data['uptimecount'].asString := '0';
Data['usednstime'].asString := '0';
Data['usewindowsauthentication'].asString := '0';
Data['valuetype'].asString := '0';
Data['windowslogindomain'].asString := 'BG';
Data['windowsloginpassword'].asString := '***';
Data['windowsloginusername'].asString := 'SERVICE_PRTG';
Data['writeresult'].asString := '1';

Source: https://helpdesk.paessler.com/en/support/solutions/articles/62319-how-do-i-monitor-all-services-on-a-server-set-to-automatic


Article Comments

It seems like you're not using any parameters. You'll have to use at least:

-ComputerName %host -Username "%windowsdomain\%windowsusername" -Password '%windowspassword'

Jul, 2016 - Permalink