What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Remote Powershell Script Return Error code not changing sensor to red

Votes:

0

All,

I am working on a script that monitors the windows update service weather its running or not. We have some issues where we need to know if this service is running and flag it to alert. I have made some progress on the script to actually use the placeholder details of PRTG to auth correctly to the remote server but I just cannot get it to change the sensor red to alert with the exit codes. I can manually stop or start the service on the remote box and see it change in PRTG but it just will just not set it to Alert or red when the exit code is 2. It sees my messages in PRTG but just wont change the sensor.

Code

C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE\WUServiceStatus.ps1:

Param(
[string]$WHost,
[string]$Username,
[string]$Password
)

#create credentials 
$SecPassword = $Password | ConvertTo-SecureString -AsPlainText -force
$cred = new-object -typename System.Management.Automation.PSCredential ($Username, $SecPassword)
 
$ret = invoke-command -computer $WHost -Credential $cred -ScriptBlock {
# search for process
$pr = get-service wuauserv | Select -ExpandProperty "Status"

# If Update service running return error
if ($pr -match "Running") 
{
    Write-OutPut "2:Windows Update RUNNING";
    Exit 2
}
ELSE
{
    Write-OutPut "0:Windows Update STOPPED";
    Exit 0
}
}  # end script block
$ret

Custom EXE/Script sensor Parameters settings:

-UserName %windowsdomain\%windowsuser -Password "%windowspassword" -WHost %host

Can anyone advise how to get this to actually set PRTG to alert status when the Exit 2 code is found if the windows update service is running?

I feel like it could be an issue with the invoke command not returning the exit code correctly but I am not a PS GURU and not 100% sure what to look for to understand why.

exe-script invoke-command remote-powershell

Created on Feb 20, 2023 3:24:39 PM

Last change on Feb 20, 2023 4:10:07 PM by  Felix Wiesneth [Paessler Support]



1 Reply

Accepted Answer

Votes:

1

After thinking about this long and hard, I realized that the invoke command does not pass the exit code to the local PS script. What I had to do was add another if statement to read the output of the invoke and then do an exit code so PRTG could pick it up.

Code Location: C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE\AA-WUServiceStatus.ps1

Updated Code:

Param(
[string]$WHost,
[string]$Username,
[string]$Password
)

# Create credentials 
$SecPassword = $Password | ConvertTo-SecureString -AsPlainText -force
$cred = new-object -typename System.Management.Automation.PSCredential ($Username, $SecPassword)
 
$ret = invoke-command -computer $WHost -Credential $cred -ScriptBlock {

# Search for process. Can change wuauserv to any known windows service.
$pr = get-service wuauserv | Select -ExpandProperty "Status"

# If service running return value
if ($pr -match "Running") 
{
    Write-OutPut "RUNNING";
    Exit
}
ELSE
{
    Write-OutPut "STOPPED";
    Exit
}
}  # end script block
$ret

# Take output of the remote invoke and exit script locally so PRTG reads sensor status correctly
if ($ret -match "RUNNING") 
{
    Write-OutPut "2:Windows Update RUNNING";
    Exit 2
}
ELSE
{
    Write-OutPut "0:Windows Update STOPPED";
    Exit 0
}

Created a Custom EXE/Script Sensor.

Under the Parameters line add this:

-UserName %windowsdomain\%windowsuser -Password "%windowspassword" -WHost %host

In order for this to work on Workgroup servers NOT on a Domain, I had to run the following on the REMOTE Server first:

Adjust x.x.x.x for the IP address of PRTG. Change prtgDOMAIN.com to your domain or remove it keeping just the IP address.

Enable-PSRemoting –force
winrm set winrm/config/client '@{TrustedHosts="x.x.x.x,prtgDOMAIN.com"}'

Then you need to add the REMOTE Servers IP address to your PRTG servers TrustedHosts:

Change x.x.x.x to the IP address of the REMOTE Server.

Set-Item WSMan:\localhost\Client\TrustedHosts -Concatenate -Value x.x.x.x

I am not a PS GURU by any means and this is just what has worked for us. There is probably a better way to do this than what I have posted. Worked out a lot of this from the information provided by this URL and a Big Thank You for getting me in the right direction: https://thedomainiown.wordpress.com/prtg-related/general-custom-exe-script/

I will note that I had to set the Domain or Computer Name under PRTG Settings of the server to the IP address. Without this I could not get it to work. So you have to set something either, domain or the IP address in that box when adding it to PRTG.

Hope this helps others as I was hitting dead ends on the forums with similar unresolved examples. I will try to help on this thread if anyone has questions.

Created on Feb 22, 2023 3:13:01 PM




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.