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

Powershell script - check reboot status - not sure why this doesn't work

Votes:

0

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?

powershell reboot-pending restart-pending windows-reboot

Created on Jan 15, 2015 3:21:24 PM

Last change on Jan 16, 2015 7:14:51 AM by  Konstantin Wolff [Paessler Support]



8 Replies

Votes:

0

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.

Created on Jan 15, 2015 4:27:07 PM

Last change on Jan 15, 2015 4:53:25 PM by  Torsten Lindner [Paessler Support]



Votes:

0

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

Created on Jan 16, 2015 7:19:59 AM by  Konstantin Wolff [Paessler Support]



Votes:

0

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.

Created on Jan 16, 2015 1:33:05 PM



Votes:

0

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

Created on Jan 16, 2015 1:54:59 PM by  Konstantin Wolff [Paessler Support]



Votes:

0

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.

Created on Jan 16, 2015 2:24:57 PM



Votes:

0

Ted,
please keep me posted on this.

Created on Jan 19, 2015 9:50:52 AM by  Konstantin Wolff [Paessler Support]



Votes:

0

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

Created on Jul 24, 2016 9:06:22 AM



Votes:

0

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 }

Created on Oct 11, 2019 6:33:21 AM




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.