'------------------
' AUBehave.vbs   ver 2.8  17-Nov-2003
' Check Automatic Update settings
' http://www.susserver.com/
'------------------
On Error Resume Next

Set objWshNet = CreateObject("Wscript.Network")

const HKCU = &H80000001
const HKLM = &H80000002

strComputer = lcase(objWshNet.ComputerName)
if left(strComputer,2)="\\" then
 strComputer=right(strComputer,(len(strComputer)-2))
end if

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

If Err.Number <> 0 Then
 msgbox "Connection Error to computer:" & VBCRLF & VBCRLF & "     " & strComputer & VBCRLF, vbCritical, "Connection Error"
 WScript.Quit
End If

strMsg = "No Auto Update:  "
strKeyPath = "Software\Policies\Microsoft\Windows\WindowsUpdate\AU"
strValueName = "NoAutoUpdate"
If RegValueExists(strKeyPath, strValueName) Then
 oReg.GetDWORDValue HKLM,strKeyPath,strValueName,dwValue
 Resultmsg = Resultmsg & GetNoAutoUpdate(dwValue) & VBCRLF & VBCRLF
Else
 Resultmsg = Resultmsg & strMsg & "Setting Not Configured in Policy" & VBCRLF & VBCRLF
End If

strMsg = "Use WSUS Server:  "
strKeyPath = "Software\Policies\Microsoft\Windows\WindowsUpdate\AU"
strValueName = "UseWUServer"
If RegValueExists(strKeyPath, strValueName) Then
 oReg.GetDWORDValue HKLM,strKeyPath,strValueName,dwValue
 Resultmsg = Resultmsg & GetUseWUServer(dwValue) & VBCRLF

 If dwValue = "1" Then
  strMsg = "The WSUS Server is:  "
  strKeyPath = "Software\Policies\Microsoft\Windows\WindowsUpdate"
  strValueName = "WUServer"
  If RegValueExists(strKeyPath, strValueName) Then
   oReg.GetStringValue HKLM,strKeyPath,strValueName,strValue
   Resultmsg = Resultmsg & strMsg & strValue & VBCRLF
  Else
   Resultmsg = Resultmsg & strMsg & "Missing." & VBCRLF & "You should reconfigure SUS." &VBCRLF
  End If
 End If
End If

strMsg = VBCRLF & "Automatic Update Options:  "
strKeyPath = "Software\Policies\Microsoft\Windows\WindowsUpdate\AU"
strValueName = "AUOptions"
If RegValueExists(strKeyPath, strValueName) Then
 oReg.GetDWORDValue HKLM,strKeyPath,strValueName,dwValue
 GetAUOptions(dwValue)
 Resultmsg = Resultmsg & strMsg & VBCRLF

 If dwValue = "4" Then
  strMsg = "  - Scheduled Install Day:  "
  strKeyPath = "Software\Policies\Microsoft\Windows\WindowsUpdate\AU"
  strValueName = "ScheduledInstallDay"
  If RegValueExists(strKeyPath, strValueName) Then
   oReg.GetDWORDValue HKLM,strKeyPath,strValueName,dwValue
   Resultmsg = Resultmsg & strMsg & getday(dwValue) & VBCRLF
  Else
   Resultmsg = Resultmsg & strMsg & "Setting Not Configured in Policy" & VBCRLF
  End If
 
  strMsg = "  - Scheduled Install Time:  "
  strKeyPath = "Software\Policies\Microsoft\Windows\WindowsUpdate\AU"
  strValueName = "ScheduledInstallTime"
  If RegValueExists(strKeyPath, strValueName) Then
   oReg.GetDWORDValue HKLM,strKeyPath,strValueName,dwValue
   Resultmsg = Resultmsg & strMsg & dwValue &":00" & VBCRLF & VBCRLF
  Else
   Resultmsg = Resultmsg & strMsg & "Setting Not Configured in Policy" & VBCRLF
  End If
 Else
   Resultmsg = Resultmsg & VBCRLF
 End If

Else
 Resultmsg = Resultmsg & strMsg & "Setting Not Configured in Policy" & VBCRLF
 strMsg = "  - User controlled setting:  "
 strKeyPath = "Software\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
 strValueName = "AUOptions"
 If RegValueExists(strKeyPath, strValueName) Then
  oReg.GetDWORDValue HKLM,strKeyPath,strValueName,dwValue
  Resultmsg = Resultmsg & strMsg & GetAUOptions(dwValue) & VBCRLF

  If dwValue = "4" Then
   strMsg = "    - Scheduled Install Day:  "
   strKeyPath = "Software\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
   strValueName = "ScheduledInstallDay"
   If RegValueExists(strKeyPath, strValueName) Then
    oReg.GetDWORDValue HKLM,strKeyPath,strValueName,dwValue
    Resultmsg = Resultmsg & strMsg & getday(dwValue) & VBCRLF
   Else
    Resultmsg = Resultmsg & strMsg & "Setting Not Configured" & VBCRLF
   End If
 
   strMsg = "    - Scheduled Install Time:  "
   strKeyPath = "Software\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
   strValueName = "ScheduledInstallTime"
   If RegValueExists(strKeyPath, strValueName) Then
    oReg.GetDWORDValue HKLM,strKeyPath,strValueName,dwValue
    Resultmsg = Resultmsg & strMsg & dwValue &":00" & VBCRLF
   Else
    Resultmsg = Resultmsg & strMsg & "Setting Not Configured" & VBCRLF
   End If
  Else
    Resultmsg = Resultmsg & VBCRLF
  End If

 Else
  Resultmsg = Resultmsg & strMsg & "Setting Not present" & VBCRLF
 End If
End If

strMsg = "  - After downloading updates:  "
strKeyPath = "Software\Policies\Microsoft\Windows\WindowsUpdate\AU"
strValueName = "NoAutoRebootWithLoggedOnUsers"
If RegValueExists(strKeyPath, strValueName) Then
 oReg.GetDWORDValue HKLM,strKeyPath,strValueName,dwValue
 Resultmsg = Resultmsg & strMsg & GetNoAutoReboot(dwValue) & VBCRLF & VBCRLF
Else
 Resultmsg = Resultmsg & strMsg & "Your system will rstart itself in five minutes.           " & VBCRLF & VBCRLF
End If

MsgBox Resultmsg,,"Windows Update Configuration"

set oReg = nothing


Function GetNoAutoUpdate(Index)
 Select Case Index
  Case 0 GetNoAutoUpdate = "Automatic Updates are Enabled"
  Case 1 GetNoAutoUpdate = "Automatic Updates are Disabled"
  Case Else GetNoAutoUpdate = "Can not read Automatic Update Policy"
 End select
End Function

Function GetUseWUServer(Index)
 Select Case Index
  Case 0 GetUseWUServer = "You are not using WSUS"
  Case 1 GetUseWUServer = "You are using WSUS"
  Case Else GetUseWUServer = "Can not determine whether you are using WSUS"
 End select
End Function

Function GetDay(Index)
 Select Case Index
  Case "0" GetDay = "Every Day"
  Case "1" GetDay = "Every Sunday"
  Case "2" GetDay = "Every Monday"
  Case "3" GetDay = "Every Tuesday"
  Case "4" GetDay = "Every Wednesday"
  Case "5" GetDay = "Every Thursday"
  Case "6" GetDay = "Every Friday"
  Case "7" GetDay = "Every Saturday"
  Case Else GetDay = "Error in this value"
 End select
End Function

Function GetAUOptions(Index)
 Select Case Index
  Case "0" GetAUOptions = "0"
  Case "1" GetAUOptions = "1 - Disabled in User Settings"
  Case "2" GetAUOptions = "2 - Notify Admin-priv user of a pending update waiting to be downloaded. User will initate the download."
  Case "3" GetAUOptions = "3 - Automatically downloads updates and notify Admin-priv user of pending installation."
  Case "4" GetAUOptions = "4 - Automatically downloads updates. Installation will occur on the scheduled day & time."
  case Else GetAUOptions = "Error in this value"
 End select
End Function

Function GetNoAutoReboot(Index)
 Select Case Index
  Case "0" GetNoAutoReboot = "Your system will rstart itself in five minutes.           "
  Case "1" GetNoAutoReboot = "You will be asked to restart your computer to install them."
  case Else GetNoAutoReboot = "Error in this value"
 End select
End Function

Function GetAUState(Index)
 Select Case Index
  Case "0" GetAUState = "0 - Initial 24 hour timeout (AU Wizard will wait)"
  Case "1" GetAUState = "1 - Waiting for user to run AU wizard from System Tray"
  Case "2" GetAUState = "2 - Detect pending"
  Case "3" GetAUState = "3 - Download pending (waiting for user to accept pre-download prompt)"
  Case "4" GetAUState = "4 - Download in progress"
  Case "5" GetAUState = "5 - Install pending"
  Case "6" GetAUState = "6 - Install complete"
  Case "7" GetAUState = "7 - Disabled"
  Case "8" GetAUState = "8 - Reboot pending (Reboot was declined)"
  Case Else GetAUState = "Error in this value"
 End select
End Function

Function RegValueExists(sRegKey, sRegValue)
  sRegKey = Trim(sRegKey)
  sRegValue = LCase(Trim(sRegValue))
  ' init value
  RegValueExists = False
  If oReg.EnumValues(HKLM, sRegKey, aValueNames, aValueTypes) = 0 Then
    If Not IsNull(aValueNames) Then
      For i = 0 To UBound(aValueNames)
        If LCase(aValueNames(i)) = sRegValue Then
          RegValueExists = True
        End If
      Next
    End If
  End If
End Function

Function RegKeyExists(sRegKey)
  sRegKey = Trim(sRegKey)
  If oReg.EnumValues(HKLM, sRegKey, aValueNames, aValueTypes) = 0 Then
    RegKeyExists = True
  Else
    RegKeyExists = False
  End If
End Function
