Solved

How to detect or know if Windows server 2003/2008 need restart ?

Posted on 2010-09-21
8
4,328 Views
Last Modified: 2012-08-17
Hi All,

I've got several hundreds of Windows Server 2003 and 2008 virtual machine + several physical machine which is updated by WSUS automatically but it needs to be restarted manually.

Is there any way to know which server needs restart and which one is not ?( eg using powershell)

Thanks
0
Comment
Question by:jjoz
8 Comments
 
LVL 12

Accepted Solution

by:
larsrohr earned 125 total points
ID: 33732040
This may be of use to you:

Checking for pending reboots with PowerShell
http://www.techmumbojumblog.com/?p=375

Be sure to check the comments by Aaron Dodd at the end.
0
 
LVL 5

Assisted Solution

by:sunilswain
sunilswain earned 125 total points
ID: 33732153
For Windows 2003 server you'll need to check "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
> Manager\PendingFileRenameOperations".

One sample script which wa used for windows 2003 server:
<script>
On Error Resume Next

Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_MULTI_SZ = 7

strComputer = "."

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

strKeyPath = "SYSTEM\CurrentControlSet\Control\Session Manager"
strValueName = "PendingFileRenameOperations"

oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,arrValues

If Not IsNull(arrValues) Then
For Each strValue In arrValues
Wscript.Echo strValue
Next
Else
Wscript.Echo "No Pending Reboot"
End If

</script>

Also for Server patch reboots please check the following page: http://blogs.msdn.com/b/hansr/archive/2006/02/17/patchreboot.aspx
0
 
LVL 1

Author Comment

by:jjoz
ID: 33732189
ah yes, this is the code that i test and it works for me, now it's my turn to modify this script so that it can work for multiple server entry.
function Get-PendingReboot($computer = '.') {
	$hkey		= 'LocalMachine';
	$path_server	= 'SOFTWARE\Microsoft\ServerManager';
	$path_control	= 'SYSTEM\CurrentControlSet\Control';
	$path_session	= join-path $path_control 'Session Manager';
	$path_name	= join-path $path_control 'ComputerName';
	$path_name_old	= join-path $path_name 'ActiveComputerName';
	$path_name_new	= join-path $path_name 'ComputerName';
	$path_wsus = ‘SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired’;

	$pending_rename	= 'PendingFileRenameOperations';
	$pending_rename_2	= 'PendingFileRenameOperations2';
	$attempts	= 'CurrentRebootAttempts';
	$computer_name	= 'ComputerName';

	$num_attempts	= 0;
	$name_old	= $null;
	$name_new	= $null;

	$reg= [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($hkey, $computer);
	
	$key_wsus = $reg.OpenSubKey($path_wsus);
	if ($key_wsus -ne $null) {
		$wsus_values	= @($key_wsus.GetValueNames());
		if ($wsus_values) {
			$wsus_rbpending = $true
		} else {
			$wsus_rbpending = $false
		}
			$key_wsus.Close() | out-null;
	}
	
	if ((($session_values -contains $pending_rename) -or ($session_values -contains $pending_rename_2)) `
	-or (($num_attempts -gt 0) -or ($name_old -ne $name_new)) `
	-or ($wsus_rbpending)) {
		return $true;
	}
	else {
		return $false;
	}

	$key_session	= $reg.OpenSubKey($path_session);
	if ($key_session -ne $null) {
		$session_values	= @($key_session.GetValueNames());
		$key_session.Close() | out-null;
	}

	$key_server	= $reg.OpenSubKey($path_server);
	if ($key_server -ne $null) {
		$num_attempts = $key_server.GetValue($attempts);
		$key_server.Close() | out-null;
	}

	$key_name_old	= $reg.OpenSubKey($path_name_old);
	if ($key_name_old -ne $null) {
		$name_old = $key_name_old.GetValue($computer_name);
		$key_name_old.Close() | out-null;

		$key_name_new	= $reg.OpenSubKey($path_name_new);
		if ($key_name_new -ne $null) {
			$name_new = $key_name_new.GetValue($computer_name);
			$key_name_new.Close() | out-null;
		}
	}

	$reg.Close() | out-null;

	if ((($session_values -contains $pending_rename) `
	-or ($session_values -contains $pending_rename_2)) `
	-or (($num_attempts -gt 0) -or ($name_old -ne $name_new))) {
		return $true;
	}
	else {
		return $false;
	}
}

Get-PendingReboot "mossdev01"

Open in new window

0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 1

Author Comment

by:jjoz
ID: 33732323
but if you specify OFFLINE server, it gets ERROR message like:
Exception calling "OpenRemoteBaseKey" with "2" argument(s): "Attempted to perform an unauthorized operation."
At :line:22 char:55
+ 	$reg= [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey <<<< ($hkey, $computer);

Open in new window

0
 
LVL 7

Assisted Solution

by:mchkorg
mchkorg earned 250 total points
ID: 33732375
Hi, I'm using Nagios to monitor tons of things on my linux+windows servers, including if WSUS patches are waiting to be applied *AND* when the computer needs to reboot.
Here it is:

create a script.wsf with this content (see attached)

It'll return a window with a message or a return code a and message you can get: "Reboot required"



I hope it'll help

<job>
<runtime>
<description>
Name:
  check_windows_updates (nrpe_nt-plugin) 1.5 based on check_msupdates (nrpe_nt-plugin) 1.0

License:
  The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute
  copies of the plugins under the terms of the GNU General Public License.
  For more information about these matters, see the file named COPYING.

Changelog / Contributors:
  2009 May - Albrecht Dress (albrecht.dress@arcor.de)
  2007 June - Micha Jankowski (fooky@pjwstk.edu.pl)
 
</description>
  <named
    name="h"
    helpstring="Help"
    type="simple"
    required="false"
  />
  <named
    name="w"
    helpstring="number of updates before warning status"
    type="string"
    required="false"
  />
  <named
    name="c"
    helpstring="number of updates before critical status "
    type="string"
    required="false"
  />  
</runtime>
<script language="VBScript">
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Const's and Var's
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Cons for return val's
Const intOK = 0
Const intWarning = 1
Const intCritical = 2
Const intUnknown = 3

' Cons for FSO
Const ForReading = 1
Const ForWriting = 2

Dim updatesNames

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Params
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Wscript.Arguments.Named.Exists("h") Then
    Wscript.Echo "Usage: check_windows_updates.wsf /w:1 /c:2"
    Wscript.Echo "/w: - number of updates before warning status "
    Wscript.Echo "/c: - number of updates before critical status "
End If

If Wscript.Arguments.Named.Exists("w") Then
    intWarningLvl = Cint(Wscript.Arguments.Named("w"))
Else
    intWarningLvl = 0
End If

If Wscript.Arguments.Named.Exists("c") Then
    intCriticLvl = Cint(Wscript.Arguments.Named("c"))
Else
    intCriticLvl = 0
End If

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Main
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set objAutoUpdate = CreateObject("Microsoft.Update.AutoUpdate")

intResultDetect = objAutoUpdate.DetectNow
If intResultDetect = 0 Then
Else
  WScript.Echo "WARNING: Unable to detect Automatic Updates."
  Wscript.Quit(intUnknown)
End If

Set objSession = CreateObject("Microsoft.Update.Session")
Set objSearcher = objSession.CreateUpdateSearcher


intImportant = 0
intOptional = 0

Set objSysInfo = CreateObject("Microsoft.Update.SystemInfo")
If objSysInfo.RebootRequired Then
  Wscript.Echo "Reboot required."
  Wscript.Quit(intWarning)
End If

Set result = objSearcher.Search("IsInstalled = 0 and IsHidden = 0")
Set colDownloads = result.Updates

For Each objEntry in colDownloads
  if objEntry.AutoSelectOnWebSites Then
    if intImportant = 0 Then
      importantNames = objEntry.Title
    else
      importantNames = importantNames & "; " & objEntry.Title
    End If
    intImportant = intImportant + 1
  Else
    If intOptional = 0 Then
      optionalNames = objEntry.Title
    Else
      optionalNames = optionalNames & "; " & objEntry.Title
    End If
    intOptional = intOptional + 1
  End If
Next

If intImportant + intOptional > 0 Then
   echoStr = "Updates: " & intImportant & " important, " & intOptional & " optional|"
   If intImportant > 0 Then
      echoStr = echoStr & "Important: " & importantNames
      If intOptional > 0 Then
        echoStr = echoStr & " (note: optional updates not listed)"
      End If
   Else
      echoStr = echoStr & "Optional: " & optionalNames
   End If
   WScript.Echo echoStr

    If intImportant > intCriticLvl Then
        Wscript.Quit(intCritical)
    End If

    If intImportant > intWarningLvl Then
        Wscript.Quit(intWarning)
    End If
    Wscript.Quit(intOK)  
Else
   WScript.Echo "No updates waiting or installing."
   Wscript.Quit(intOK)
End If

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' End
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
</script>
</job>

Open in new window

0
 
LVL 1

Author Comment

by:jjoz
ID: 33732382
wow, thanks for you reply mchkorg
do i have to manually run that script on my Windows Serve rand then install NAGIOS agent one by one ?
0
 
LVL 7

Assisted Solution

by:mchkorg
mchkorg earned 250 total points
ID: 33732578
Let say, if you already have nagios in place or plan to have it, you'll easily use this script to detect your WSUS status in your monitoring.
But if you don't have it, you could for example run it with psexec (look for it at MS's site, formerly sysinternals) to script something on all your servers

1) Create a check_all_servers.bat with this in it:
@echo off
set ComputerList=myserver1 myserver2
for %%a in (%ComputerList%) do @call check_one.bat %%a
pause

2) In check_one.bat, you'll put:
@echo off
echo Checking server %1
psexec \\%1 -u admin_login -p his_password c:\windows\system32\cscript.exe /NoLogo /T:120 c:\check_windows_updates.wsf /w:0 /c:1
REM provided the .wsf script is named and placed here: c:\check_windows_updates.wsf

3) Run the check_all_servers.bat

I tested it all, some of my servers don't want to start the .wsf, I don't know why. I did not investigate further as it works for most of them.
0
 
LVL 1

Author Closing Comment

by:jjoz
ID: 33839610
thanks ma n!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question