Script Center > Scripting Forums > The Official Scripting Guys Forum! > Check OS, Check Hotfix, Install if not present
Ask a questionAsk a question
 

AnswerCheck OS, Check Hotfix, Install if not present

  • Tuesday, October 21, 2008 2:31 PMMark A. Wilson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi all,
    This may seem like a simple question, but Scripting is not my forte.

    I'm trying to figure out a way to check that the client OS is XPsp3 and then look for the existence of the $NTUninstallKB953760$ folder, and if it is not present, to install \\server\share\WindowsXP-kb953760-x86-ENU.exe.

    Any assistance or advice would be greatly appreciated.

    Thanks, Mark

Answers

  • Wednesday, October 22, 2008 6:24 AMSalvador Manaois IIIModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi Mark,

    I modified your script a bit to check for whether the patch is installed instead of checking for the uninstall folder.

    1 Set objShell = CreateObject("WScript.Shell")  
    2 Set objFSO = CreateObject("Scripting.FileSystemObject")  
    3  
    4 strComputer = "." 
    5 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")  
    6 Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem WHERE Name LIKE '%Microsoft Windows XP%'")  
    7 For Each objItem In colItems  
    8    If objItem.CSDVersion = "Service Pack 3" Then 
    9       If CheckPatch("KB953760")=True then  
    10          Wscript.Echo "Patch found." 
    11       Else 
    12           Wscript.Echo "Patch not found. Installing patch..." 
    13           objShell.Run "\\server\share\WindowsXP-kb953760-x86-ENU.exe /qn", ,TRUE  
    14       End if  
    15    End If 
    16 Next 
    17  
    18 Function CheckPatch(patch)  
    19   Set objSession = CreateObject("Microsoft.Update.Session")  
    20   Set objSearcher = objSession.CreateUpdateSearcher  
    21   Set objResults = objSearcher.Search("Type='Software'")  
    22   Set colUpdates = objResults.Updates  
    23   Found = False 
    24   Result = 0  
    25   For i = 0 to colUpdates.Count - 1  
    26       Result = Instr(colUpdates.Item(i).Title, patch)  
    27        If Result > 0 then  
    28          Found = True 
    29          Exit For 
    30        End if  
    31   Next 
    32   CheckPatch = Found  
    33 End Function 

    Regards,

    Salvador Manaois III
    MCSE MCSA CEH MCITP | Enterprise/Server Admin
    Bytes & Badz : http://badzmanaois.blogspot.com

All Replies

  • Tuesday, October 21, 2008 6:04 PMnckmrtn Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed AnswerHas Code
    Hey Mark,

    Give this a try:

    Set objShell = CreateObject("WScript.Shell"
    Set objFSO = CreateObject("Scripting.FileSystemObject"
     
    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2"
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem WHERE Name LIKE '%Microsoft Windows XP%'")
    For Each objItem In colItems 
       If objItem.CSDVersion = "Service Pack 3" Then 
          If objFSO.FolderExists("C:\WINDOWS\$NTUninstallKB953760$")=False Then 
             objShell.Run "\\server\share\WindowsXP-kb953760-x86-ENU.exe /qn", ,TRUE 
          End If 
       End If 
    Next 

    -N
    • Edited bynckmrtn Tuesday, October 21, 2008 6:12 PM
    • Proposed As Answer byaboren Wednesday, October 22, 2008 5:40 AM
    •  
  • Wednesday, October 22, 2008 5:39 AMaboren Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    nckmrtn said:

    Hey Mark,

    Give this a try:

    Set objShell = CreateObject("WScript.Shell"
    Set objFSO = CreateObject("Scripting.FileSystemObject"
     
    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2"
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem WHERE Name LIKE '%Microsoft Windows XP%'")
    For Each objItem In colItems 
       If objItem.CSDVersion = "Service Pack 3" Then' YES XP has SP3, then we check for patch
          If objFSO.FolderExists("C:\WINDOWS\$NTUninstallKB953760$")=False Then'Nope patch is not here, lets install on next line
             objShell.Run "\\server\share\WindowsXP-kb953760-x86-ENU.exe /qn", ,TRUE'Install patch from share
          End If 
       End If 
    Next 

    -N



    Put some small "helpers" in the script wich by itself is doing the job.
    Checks for SP3 & the actual hotfix.
  • Wednesday, October 22, 2008 6:24 AMSalvador Manaois IIIModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi Mark,

    I modified your script a bit to check for whether the patch is installed instead of checking for the uninstall folder.

    1 Set objShell = CreateObject("WScript.Shell")  
    2 Set objFSO = CreateObject("Scripting.FileSystemObject")  
    3  
    4 strComputer = "." 
    5 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")  
    6 Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem WHERE Name LIKE '%Microsoft Windows XP%'")  
    7 For Each objItem In colItems  
    8    If objItem.CSDVersion = "Service Pack 3" Then 
    9       If CheckPatch("KB953760")=True then  
    10          Wscript.Echo "Patch found." 
    11       Else 
    12           Wscript.Echo "Patch not found. Installing patch..." 
    13           objShell.Run "\\server\share\WindowsXP-kb953760-x86-ENU.exe /qn", ,TRUE  
    14       End if  
    15    End If 
    16 Next 
    17  
    18 Function CheckPatch(patch)  
    19   Set objSession = CreateObject("Microsoft.Update.Session")  
    20   Set objSearcher = objSession.CreateUpdateSearcher  
    21   Set objResults = objSearcher.Search("Type='Software'")  
    22   Set colUpdates = objResults.Updates  
    23   Found = False 
    24   Result = 0  
    25   For i = 0 to colUpdates.Count - 1  
    26       Result = Instr(colUpdates.Item(i).Title, patch)  
    27        If Result > 0 then  
    28          Found = True 
    29          Exit For 
    30        End if  
    31   Next 
    32   CheckPatch = Found  
    33 End Function 

    Regards,

    Salvador Manaois III
    MCSE MCSA CEH MCITP | Enterprise/Server Admin
    Bytes & Badz : http://badzmanaois.blogspot.com
  • Wednesday, October 22, 2008 3:55 PMMark A. Wilson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks so much for your assistance guys!
  • Friday, October 24, 2008 2:38 PMEdward Hyde Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Any way to run this against remote computers where the person logged on is only in the users group?
  • Friday, October 24, 2008 4:17 PMSalvador Manaois IIIModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     Not possible as the script is installing an OS patch; non-admins will not be able to accomplish this task. Use WSUS if you want to install patches on your clients without explicitly granting users admin rights on their machines.

    http://technet.microsoft.com/en-us/wsus/default.aspx

    Regards,

    Salvador Manaois III
    MCSE MCSA CEH MCITP | Enterprise/Server Admin
    Bytes & Badz : http://badzmanaois.blogspot.com