Hello! I need to find out what servers in our environment are requiring a reboot.
This code works interactively:
PS> $remoteMachine = "DC1"
$updatehive = reg query "\\$remoteMachine\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
if ($updateHive -match "RebootRequired") {write-host "$remoteMachine needs a reboot."} else {write-host "$remoteMachine is OK."}
$remoteMachine = "DC2"
$updatehive = reg query "\\$remoteMachine\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
if ($updateHive -match "RebootRequired") {write-host "$remoteMachine needs a reboot."} else {write-host "$remoteMachine is OK."}
DC1 needs a reboot.
DC2 is OK.
The same code does not work in a foreach loop:
PS> $machineList = "\\fp1\install\scripts\servers.txt"
Foreach ($remoteMachine in $machineList)
{ $updatehive = reg query "\\$remoteMachine\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto
Update"
if ($updateHive -match "RebootRequired") {write-output "$remoteMachine needs a reboot."}
}
reg : ERROR: Invalid key name.
At line:3 char:19
+ { $updatehive = reg query "\\$remoteMachine\HKEY_LOCAL_MACHINE\SOFTWARE\Micros ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (ERROR: Invalid key name.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
OK, maybe I need to build the string first:
PS> $machineList = "\\fp1\install\scripts\servers.txt"
Foreach ($remoteMachine in $machineList)
{ $hivePath = "\\$remoteMachine\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
$updatehive = reg query "$hivePath"
if ($updateHive -match "RebootRequired") {write-output "$remoteMachine needs a reboot."}
}
reg : ERROR: Invalid key name.
At line:4 char:19
+ $updatehive = reg query "$hivePath"
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (ERROR: Invalid key name.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
OK, maybe I need to use native Powershell:
PS> $remoteMachine = "DC1"
PS> $remoteHKLM = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine",$remoteMachine)
PS> $updateteHive = $remoteHKLM.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update")
PS> if ($updateHive -contains "RebootRequired") {write-output "$remoteMachine needs a reboot."}
nothing. I also tried the following code variants, with the same response - nothing:
PS> $remoteHKLM = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$remoteMachine)
PS> $updateteHive = $remoteHKLM.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\")
PS> if ($updateHive -like "RebootRequired") {write-output "$remoteMachine needs a reboot."}
PS> if ($updateHive -match "RebootRequired") {write-output "$remoteMachine needs a reboot."}
I am stumped. Hopefully there is some substitution rule I don't know about, being a relative newbie.
What I would really like is for the list of servers needing a reboot to be emailed or texted to me if any were found.
Any advice or guidance would be greatly appreciated!
Rob