Here's a bit of some background. I'm trying to determine the reboot status of a windows server. I'm using the following script:

https://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542

I've included that in the Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE folder.

I then wrote a new script. See attached below. I'm dot sourcing the original script to use that as a function.

Param (
    [String]$Computer
)
."C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE\Get-PendingRebootFunction.ps1"

# Begin PRTG section
$Results = Get-PendingReboot -ComputerName $Computer
foreach ($Result in $Results){
	if ($Result.CBServicing -eq $true){
		Write-Output "4:Reboot Pending"
        Exit 4;
	}
	elseif($Results.WindowsUpdate -eq $true){
		Write-Output "4:Reboot Pending"
        Exit 4;
	}
	elseif ($Results.CCMClientSDK){
		if ($Results.CCMClientSDK -eq $true){
			Write-Output "4:Reboot Pending"
            Exit 4;
   		}
	}
	elseif ($Results.PendFileRename -eq $true){
		Write-Output "4:Reboot Pending"
        Exit 4;
	}
	elseif ($Results.PendFileRenVal){
		Write-Output "4:Reboot Pending"
        Exit 4;
	}
	elseif ($Results.RebootPending -eq $true){
		Write-Output "4:Reboot Pending"
        Exit 4;
	}
	else{
		Write-Output "0:OK"
        Exit 0;
	}
}

If I run this on the PRTG server, against a machine that has a pending reboot, then I get a true value when I look at all of the values in $results.

If I try to use this with PRTG, the %host value from the device is being passed correctly, I've written the value $computer out to a text file. However, when used with the function, it's as if it's checking the PRTG server (which doesn't have a reboot pending).

So, when running from PowerShell on PRTG against a machine that has a pending reboot, I see the following values:

Computer: Computer1  
CBServicing    : True
WindowsUpdate  : True
CCMClientSDK   : False
PendFileRename : False
PendFileRenVal :
RebootPending  : True

When I run this as a custom sensor, I get the following output:

Computer: Computer1  
CBServicing    : False
WindowsUpdate  : False
CCMClientSDK   : False
PendFileRename : False
PendFileRenVal :
RebootPending  : False

I'm confused by the differences here. Why would the results be different? When running within PowerShell? I'm wondering if this is a context thing here.

The use of the function basically does a check against the machine from the PRTG server. Am I approaching this the wrong way?


Article Comments

Trying to add the { { { and } } } for the code here but every time I edit my post, I get the following error:

Sorry about that!
Something has gone wrong.

Maybe it was just a temporary problem.
If the problem persists contact our support.

But, it looks like it has finally worked.


Jan, 2015 - Permalink

Ted,
by default, scripts are executed with permissions of the local SYSTEM account. I had a look at the script you are using and it does not seem to accept credentials for explicit authentication. So it has to be done implicitly.
First, please try changing the security context (tab Settings of the sensor) of the script. Try running it again.
If this fails as well, try changing the account the PRTG Probe service is running under. Please use an account which would be able to access the target machines.
Best regards


Jan, 2015 - Permalink

Sorry, I left that detail out. I've already changed the security context of the sensor so it's using specified credentials and not the PRTG Probe service.


Jan, 2015 - Permalink

Ted,
could you try changing the user account the probe is running under?


Jan, 2015 - Permalink

I just tried that with the same results. It must be the way the original script is written. I am going to run this from the context of SCCM as a configuration item and baseline and see how that works.


Jan, 2015 - Permalink

Ted,
please keep me posted on this.


Jan, 2015 - Permalink

Awesome script...

I have always had better results using write-host over write-output, even though write-output makes more sense..

Andrew Huddleston ajhstn.github.com


Jul, 2016 - Permalink

The problem occurs because PRTG executes the script with the 32 bit powershell. When you access the registry with the 32 bit powershell it uses the 32 bit hive per default. I came up with this:

param([string]$computername = "")
try{
$baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine","$computername",[Microsoft.Win32.RegistryView]::Registry64 ) 
$key = $baseKey.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Component Based Servicing") 
$subkeys = $key.GetSubKeyNames() 
$key.Close() 
$baseKey.Close()
}
catch{
Write-Host "Reboot Pending:2"; exit 2 
}

if($subkeys.Contains("RebootPending")) { 
Write-Host "Reboot Pending:1"; exit 1 } 
else { 
Write-Host "Reboot Pending:0"; exit 0 }

Oct, 2019 - Permalink