Hello,

i have created an custom powershell-script with xml-Output. The script checks some Windows-Services and show it in seperate channels in the Sensor. My Problem is, i don't know, how i must change my script, to execute the Script on the device, where i set-up the sensor. The script monitors actually the Services of the Probe-Server. Here is my script:

<#
===================================================================================
Paessler PRTG Sensor to check Windows Services
===================================================================================
Autor:        My
Script:       Services.ps1
Version:      1.0
Date:         10.08.2020
Scriptpath:   C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML
Scripttype:   EXE/Script Advanced
#>
param(
    [string]$ComputerName = "%host",
    [string]$Domain = "%windowsdomain",
    [string]$Username = "%windowsuser",
    [string]$Password = "%windowspassword",
    #Servicenames:
    [string]$ServiceName1 = "netlogon",
    [string]$ServiceName2 = "AppXSvc",
    [string]$ServiceName3 = "Dhcp"
)



$xmlOutput = '<?xml version="1.0" encoding="UTF-8" ?><prtg>'

if ((Get-Service -Name $ServiceName1).Status -eq 'Running'){$Count ="1"}
elseif

((Get-Service -Name $ServiceName1).Status -eq 'Stopped'){$Count ="2"}

elseif

((Get-Service -Name $ServiceName1).Status -eq 'Pause-Pending'){$Count ="3"}

elseif

((Get-Service -Name $ServiceName1).Status -eq 'Paused'){$Count ="4"}


 $xmlOutput = $xmlOutput + "<result>
        <channel>Anmeldedienst</channel>
        <value>$Count</value>
        <valuelookup>Windows_Services</valuelookup>
    </result>"

if ((Get-Service -Name $ServiceName2).Status -eq 'Running'){$Count ="1"}
elseif

((Get-Service -Name $ServiceName2).Status -eq 'Stopped'){$Count ="2"}

elseif

((Get-Service -Name $ServiceName2).Status -eq 'Pause-Pending'){$Count ="3"}

elseif

((Get-Service -Name $ServiceName2).Status -eq 'Paused'){$Count ="4"}

    $xmlOutput = $xmlOutput + "<result>
        <channel>AppX-Bereitstellungsdienst</channel>
        <value>$Count</value>
        <valuelookup>Windows_Services</valuelookup>
    </result>"
 if ((Get-Service -Name $ServiceName3).Status -eq 'Running'){$Count ="1"}
elseif

((Get-Service -Name $ServiceName3).Status -eq 'Stopped'){$Count ="2"}

elseif

((Get-Service -Name $ServiceName3).Status -eq 'Pause-Pending'){$Count ="3"}

elseif

((Get-Service -Name $ServiceName3).Status -eq 'Paused'){$Count ="4"}

    $xmlOutput = $xmlOutput + "<result>
        <channel>DHCP</channel>
        <value>$Count</value>
        <valuelookup>Windows_Services</valuelookup>
    </result>"  



$xmlOutput = $xmlOutput + "</prtg>"
$xmlOutput

Article Comments

Hello,

that is correct, the script itself runs on the probe computer. You can read into remote powershell if the script should operate on a different target.

If that turns out to be too complicated, you can do it the other way:

  • On the target, use the Windows Scheduler to run a script locally
  • In that script, use HTTP POST (like with invoke-webrequest) to push an XML, or JSON to an HTTP Push Data Advanced sensor.

An example to push a static JSON:

$postParams = '{"prtg": {"text": "This sensor is added to 127.0.0.1", "result": [{"LimitMode": 1, "Value": 87, "LimitMinError": 10, "LimitErrorMsg": "Percentage too high", "Channel": "Percentage", "LimitMaxError": 90, "Unit": "Percent"}, {"Channel": "Response Time", "Unit": "TimeResponse", "Value": 4711}]}}'
Invoke-WebRequest -Uri http://10.49.1.2:5050/MyToken -Method POST -ContentType application/json -Body $postParams 

For XML, please adjust the ContentType parameter.


Aug, 2020 - Permalink