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.
Article Comments
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
May, 2022 - Permalink
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
May, 2022 - Permalink
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
Aug, 2022 - Permalink
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.
Aug, 2022 - Permalink
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.
Aug, 2022 - Permalink
Hello Stefan,
As you can confirm that the output of the script is okay if you manually execute it, you can try to:
Does that resolve the isse?
Kind regards,
Felix Saure, Tech Support Team
May, 2022 - Permalink