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

Custom Sensor - vbs does not update value

Votes:

0

Hi There

I just wrote a script to check if a machine has a pending reboot. https://github.com/TS-Steff/PRTG-GetComputerRebootStatus

'https://www.winvistatips.com/threads/how-can-i-write-a-script-to-read-a-remote-registry-using-different-credentials.760483/
'#####
' 0 - OK, no reboot pending
' 1 - Reboot pending
' 2 - Wrong argument count
' 3 - Connection Error

On Error Resume Next
Err.Clear

Const HKCR = &H80000000 'HKEY_CLASSES_ROOT
Const HKCU = &H80000001 'HKEY_CURRENT_USER
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
Const HKUS = &H80000003 'HKEY_USERS
Const HKCC = &H80000005 'HKEY_CURRENT_CONFIG


Dim strComputer, strUser, strPassword
Dim objSWbemLocator, objSWbemServices, objReg
Dim strKeyPath, strEntryName, strValue
Dim rebootReuired

Set objArguments = WScript.Arguments
'arguments
'https://www.clearbyte.ch/vb-script-parameter-uebergabe/

rebootRequired = 0
strComputer = ""
strUser = ""
strDomain = ""
strPassword = ""
strDomainUser = ""
strKeyPathWUPD = "SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired"
strKeyPathCBS = "SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending"


'check argument count
if objArguments.Count <> 4 Then
	WScript.echo "2:Wrong argument Count"
	WScript.Quit
else
	strComputer = objArguments(0)
	strUser = objArguments(1)
	strDomain = objArguments(2)
	strPassword = objArguments(3)
	strDomainUser = strDomain & "\" & strUser
end if


'connect to server
Set objSWbemLocator = CreateObject("wbemScripting.SwbemLocator")
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, "root\default", strDomainUser, strPassword)

if Err.Number <> 0 Then
	WScript.echo "3:" & Err.Description
	WScript.Quit
end if

Set objReg = objSWbemServices.Get("StdRegProv")
if Err.Number <> 0 Then
	WScript.echo "3:" & Err.Description
	WScript.Quit
end if

'check if WUPD or CBS reboot flagged
if objReg.EnumKey(HKLM, strKeyPathWUPD, arrSubKeys) = 0 OR objReg.EnumKey(HKLM, strKeyPathCBS, arrSubKeys) = 0  Then
  '*** Reboot Required ***
  WScript.echo "1:Reboot Pending"
else
  '*** NO REBOOT REQUIRED
  WScript.echo "0:OK"
end if

If i add the custom sensor the script sucessfully checks the machine and returns the correct value such as "0:OK"

However. If the system needs a reboot, registry value available, the sensor does not change it's state and stucks at 0:OK. The log-data from the specific sensor also returns 0:OK If I recreate the sensor the state is 1:reboot pending. After the reboot, and manually checking the reg-keys (which are not there anymore), the sensor keeps in state 1:reboot pending.

If I run the script directly from the probe, it always returns the correct value.

Why does the probe always return the last state?

Thanks for your help.

custom-sensor vbs vbscript

Created on May 19, 2022 8:40:17 AM



6 Replies

Votes:

0

Hello Stefan,

As you can confirm that the output of the script is okay if you manually execute it, you can try to:

  • Check the User Context in the settings of the script in PRTG
  • Change the Logon User of the PRTG Probe Service (in the properties of the service) to an admin account first

Does that resolve the isse?


Kind regards,
Felix Saure, Tech Support Team

Created on May 24, 2022 7:10:20 AM by  Felix Saure [Paessler Support]



Votes:

0

Dear Felix

Thank you. The User Context in the settings is "Use security context of probe service". The Service on the Probe runs as Domain-Admin.

If I change the User Context to "Use Windows credentials of parent device" I get error 0x522 client does not have required rights.

The credentials to access the remote server are passed with the parameters in the sensor settings: %device %windowsuser %windowsdomain "%windowspassword"

Initial lookup works. But it always returns the same status. Is if there is something in the cache.

Thanks for any idea

Created on May 24, 2022 8:41:14 AM



Votes:

0

Hello Stefan,

There is no cache for sensor results I'm afraid. What you could do is to append some debugging code to the script to create separate log files for each scan. This way you could see what the script returns with each run.


Kind regards,
Felix Saure, Tech Support Team

Created on May 25, 2022 9:43:20 AM by  Felix Saure [Paessler Support]



Votes:

0

Hi there

So we just finished another approach with the test-pendingreboot PS-Module from PS-Gallery https://www.powershellgallery.com/packages/PendingReboot/0.9.0.6

### Possible states 
# 0: No Reboot Pending
# 1: Reboot Pending
# 9: Error

[cmdletbinding()]
param(
    [Parameter(Mandatory=$true)]
        [string]$Server = '',

    [Parameter(Mandatory=$false)]
        [string]$username = '',

    [Parameter(Mandatory=$false)]
        [string]$password = '',

    [Parameter(Mandatory=$false)]
        [switch]$Info = $false
)

#Import Module PendingReboot
Import-Module -Name PendingReboot

# create credentials
if($password){
    [securestring]$password = ConvertTo-SecureString $password -AsPlainText -Force
    [pscredential]$credentials = New-Object System.Management.Automation.PSCredential ($username, $password)
}

# Run PendingReboot
if($credentials){
    $ServerResult = Test-PendingReboot -Detailed -ComputerName $Server -SkipPendingFileRenameOperationsCheck -SkipConfigurationManagerClientCheck -Credential $credentials
}else{
    $ServerResult = Test-PendingReboot -Detailed -ComputerName $Server -SkipPendingFileRenameOperationsCheck -SkipConfigurationManagerClientCheck
}

if($ServerResult){ Write-Verbose $ServerResult }

## Verhalten bei zugriff verweigert
if(!$ServerResult){
    if($info){ write-host "ERR  - Clould not get resutls from Server: "$Server -ForegroundColor Red }
    write-host "9:Error connecting to host" 
}elseif($ServerResult.IsRebootPending){
    if($info){ write-host "INFO - Server Needs a Reboot!" -ForegroundColor DarkYellow }
    write-host "1: $Server Reboot pending"
}else{
    if($info){ write-host "INFO - No Reboot Pending!" -ForegroundColor Green }
    write-host "0: $Server No reboot pending"
}

If we run the script locally on the Probe it only works if it is run as administrator. If we run the script as standard user, even with the credentials, we always return the initial state.

The script is run as EXE sensor with the parameters -Server %host -username %domain\%windowsuser -password %windowspassword

It does not make a difference if we run the script with the parent windows credentials or as service user.

Kind regards, Stefan

Created on Aug 23, 2022 10:06:17 AM

Last change on Aug 25, 2022 9:30:25 AM by  Florian Lesage [Paessler Support]



Votes:

0

SOLVED

After a lot of testing, googling and testing again we solved it by adding this piece of after the parameters definition

# Check if 64Bit Environment
if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64") {
    # Source: https://stackoverflow.com/a/38981021
    #write-warning "Y'arg Matey, we're off to 64-bit land....."
    if ($myInvocation.Line) {
        &"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line
    }else{
        &"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -file "$($myInvocation.InvocationName)" $args
    }
    exit $lastexitcode
}

Somehow querying the registry with 32-Bit Powershell does not work properly. This snippet does check if the script is running in 64-Bit Powershell. If Not, restarts its self and passes all parameters again.

Source: https://stackoverflow.com/a/38981021

Thanks again and have a nice day.

Created on Aug 25, 2022 12:31:38 PM

Last change on Aug 26, 2022 6:09:08 AM by  Florian Lesage [Paessler Support]



Votes:

0

Hi Stefan,

Glad to hear that you found a solution. At the moment, the fact that the probe runs in 32bit might make the custom script a little bit complex and lead to potentials issues (especially when using external modules).

This won't be the case with the new multi-platform probe coming which will run by default in 64bit. We will soon start working on it again to add the latest missing features and new sensors to it.

Have a nice day.

Created on Aug 26, 2022 6:15:42 AM by  Florian Lesage [Paessler Support]




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.