This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

PowerShell to Monitor for presence of registry key, test-path issue when run remotely

Checking for this on agents seems ok, we are testing for 2x parts that occur when a reboot is required. I took the Windows Update Monitor and copied bits to meet our needs.

2 x SAM checks:

$reg = test-path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending';

if ($reg) {
   write-host "Message: Reboot Pending."
   write-host "Statistic: 1"
   exit 0
}

else {
   write-host "Message: Reboot not required."
   write-host "Statistic: 0"
   exit 0
}

And for the other, similar but with: $reg = test-path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired';

The problem is, this is executed on the remote host and is working on agents only, not WMI'd servers.

We have WinRM access to our servers on the domain that are not agented, yet, 1100 of them and WinRM tests ok.

The problem being, when I try to get something to run via WinRM it is not responding as expected.

I found support articles stating test-path works for folders, but not registries when run remotely which explains why we are seeing inconsistent responses.

I tried with this method, the query runs fine and responds as expected when tested via PowerShell, but I can not get it to run correctly via SAM monitor, testing returns a consistent statistic 0 not relevant to the device being tested on, or some that work if i test via PowerShell direct from our poller, just not via the SAM GUI


Running as local host, tried running as both agent and agentless, set to x64 as that is what our servers being polled are.

$hklm = [uint32]'0x80000002'
$reg = gwmi -list stdregprov -ns root/default -computer '${IP}'
$r = $reg.CheckAccess($hklm,'SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending',1)
if ($r.bgranted -like 'True') {
   write-host "Message: Reboot Pending."
   write-host "Statistic: 1"
   exit 0
}
else {
   write-host "Message: Reboot not required."
   write-host "Statistic: 0"
   exit 0
}


Any successful way to solve this for 

Parents Reply Children
No Data