How to find Windows servers that need a reboot
You might need to restart Windows servers prior to deploying patches or other software applications. This section describes a special script that helps you query servers in their Windows environments to determine which servers need to be restarted.
As its output, the script creates a Smart Server Group of Windows servers that need to be restarted.
Once servers have been found that need to be rebooted, the Rebooting Servers in a Controlled Manner (user contribution) script might come in handy.
To create and run a script for finding Windows servers that need to be restarted
- Through the Property Dictionary, create a boolean property called NEEDS_REBOOT in the Server Property Class.
- Within the Servers workspace, create a Smart Server Group that has the conditions NEEDS_REBOOT equals True.
- In the Depot, create a new NSH Script using the following code. 
 Ensure you set the script type to Execute separately against each host.
 ###################################################################
 #
 # SetRebootProperty.nsh
 # by Greg Kullberg
 # April 26, 2006
 #
 # REVISION HISTORY
 # Modified May 9, 2006 for compatibility with version 7.x
 # Modified May 25, 2006 - using blquery instead of reg to work w/ Windows 2000 systems
 #also checks to see if it is running against a windows system
 # Modified Dec 14, 2006 (GK) - added better checking for DEBUG
 # Modified Jan 15, 2007 (GK) - now using the NSH_RUNCMD_HOST variable to get target host
 # Modified Mar 13, 2007 (GK) - Added additional registry check for UpdateExeVolatile
 #key, which can also be used to determine a server needs to be rebooted
 # Modified Apr 28, 2011 (LB Jourdain) - added check for RebootRequired Key to detect
 # w2k8 servers that need reboot after having been patched.
 # DESCRIPTION
 # Determines whether or not a Windows server needs to be rebooted
 #
 # This script will execute a 'reg query' and will check the registry
 # for the PendingFileRenameOperations key. Depending on the result,
 # the script will set the NEEDS_REBOOT server property.
 #
 # Required Properties:NEEDS_REBOOT
 # Script type:Runscript
 #
 ###################################################################
 
 # Declare variables
 NEEDS_REBOOT="FALSE"
 DEBUG=$1
 HOST=$NSH_RUNCMD_HOST
 OS=`uname -s`
 
 echo "Host is $HOST, OS is $OS"
 
 
 # Set the DEBUG variable to 1 to print out all debug statements
 sub print_debug()
 {
 if [ "$DEBUG" = "TRUE" -o "$DEBUG" = "True" -o "$DEBUG" = "true" ]
 then
 echo $@
 fi
 }
 
 
 # Subroutine takes two arguments: The property name and the value to which
 # the property should be set. It is assumed that the global server variable
 # defines the server for which the property should be set.
 sub set_server_property()
 {
 PROP_NAME=$1
 PROP_VALUE=$2
 
 print_debug "Executing command: blcli Server setPropertyValue $SERVER_ID $PROP_NAME $PROP_VALUE"
 RESULT=`blcli Server setPropertyValue $SERVER_ID $PROP_NAME "$PROP_VALUE"`
 if test "$?" = "0"
 then
 print_debug "$PROP_NAME property set to $PROP_VALUE on $HOST."
 else
 echo "ERROR: Failed to set $PROP_NAME property for $HOST: $RESULT"
 fi
 }
 
 # If the host isn't windows then we don't need to do anything
 if [ "$OS" = "WindowsNT" ]
 then
 # Get the server's ID
 print_debug "Executing command blcli Server getServerDBKeyByName $HOST"
 SERVER_ID=`blcli Server getServerDBKeyByName $HOST`
 print_debug echo "Result is $?"
 print_debug "Server ID is $SERVER_ID"
 
 ##############################################
 # Check the Session Manager Registry for PendingFileRenameOperations key,
 # RebootRequired Key and check for the UpdateExeVolatile Key.
 # If the PendingFileRenameOperations or RebootRequired
 # Keys exist, the server needs to be rebooted. If the UpdateExeVolatile
 # Key exists, check the value. If the value is anything other than 0, the
 # server needs to be rebooted.
 #
 # This data is based on information found on the following website:
 # http://www.microsoft.com/technet/prodtechnol/windowsserver2003/deployment/winupdte.mspx
 # and
 # http://blogs.msdn.com/b/hansr/archive/2006/02/17/patchreboot.aspx
 # (WUA is used under the hood to deploy patches on W2K8, patches do create these keys.)
 ###
 
 PENDING_FILE_RENAME="`blquery -h $HOST -e 'reg_value_exists ("HKEY_LOCAL_MACHINE\\\\SYSTEM\\\\CurrentControlSet\\\\Control\\\\Session Manager\\\\PendingFileRenameOperations")' | tr -d '[:cntrl:]'`"
 REBOOTREQUIREDKEY="`blquery -h $HOST -e 'reg_key_exists ("HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\WindowsUpdate\\\\Auto Update\\\\RebootRequired")' | tr -d '[:cntrl:]'`"
 RESTART_STATUS="`blquery -h $HOST -e 'reg_value_exists ("HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Microsoft\\\\Updates\\\\UpdateExeVolatile")' | tr -d '[:cntrl:]'`"
 
 ##############################################
 # Set the RESTART_STATUS_VAL to zero by default. If the UpdateExeVolatile Key
 # exists, query the value and set the RESTART_VAL variable to its actual value.
 #
 ###
 
 RESTART_STATUS_VAL="0"
 if [ "$RESTART_STATUS" = "1" ]
 then
 RESTART_STATUS_VAL="`blquery -h $HOST -e 'reg_value ("HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Microsoft\\\\Updates\\\\UpdateExeVolatile")' | tr -d '[:cntrl:]'`"
 fi
 
 if [ "$PENDING_FILE_RENAME" = "1" -o "$RESTART_STATUS_VAL" != "0" -o "$REBOOTREQUIREDKEY" = "1" ]
 then
 echo "Server needs to be rebooted."
 print_debug "Result from registry is: $RESULT"
 NEEDS_REBOOT="TRUE"
 else
 echo "Server does not need to be rebooted."
 NEEDS_REBOOT="FALSE"
 fi
 
 
 # Set the NEEDS_REBOOT property
 echo "Setting NEEDS_REBOOT to $NEEDS_REBOOT"
 set_server_property "NEEDS_REBOOT" "$NEEDS_REBOOT"
 else
 echo "$HOST is not Windows, skipping"
 fi
- Create an NSH Script Job based on the script that you created in the previous step.
- Execute the NSH Script Job. 
- When the job completes, take a look at the log results of the script to see if it found any servers that appear to need restarting.
- Browse to the Smart Server Group that you created earlier. The servers that need a reboot are included in the Smart Group.
Where to go from here
Decide whether to restart the detected servers now, or to schedule a job to restart them later (such as during a change window).
