[vbs] Installing patches remotely-VBForums
Results 1 to 7 of 7

Thread: [vbs] Installing patches remotely

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Okinawa, Japan
    Posts
    271

    [vbs] Installing patches remotely

    This code uses WMI to install exe files (patches) from a folder on local computer to other computers on a network.
    This demonstrates how to:
    Create Explorer window with a TextArea in vbs and access the TextArea.
    Use WMI to create a process on target computer and receive notification when it terminates.

    Enjoy.

    VB Code:
    1. Dim TApp
    2. Dim IEObj
    3. Dim tArea
    4. Dim tButton
    5.  
    6. Const HIDDEN_WINDOW = 12
    7. Const SHOW_WINDOW=1
    8.  
    9. 'Array of Patch files to install.
    10. Dim InstallFiles()
    11.  
    12. 'maximum of 100 workstations to install patches to.
    13. Dim wsNames(100)
    14. Dim numComputers
    15. Dim retVal
    16. Dim PatchFolder
    17.     'Create explorer window
    18.     Set IEObj=CreateObject("InternetExplorer.Application")
    19.     IEObj.Navigate "about:blank"
    20.     IEObj.Height=400
    21.     IEObj.Width=500
    22.     IEObj.MenuBar=False
    23.     IEObj.StatusBar=False
    24.     IEObj.ToolBar=0
    25.     set outputWin=IEObj.Document
    26.     outputWin.Writeln "<title>RemotePatchInstall version 1.0</title>"
    27.     outputWin.writeln "<HTA:APPLICATION ID='objPatchomatic' APPLICATIONNAME='Patchomatic' SCROLL='no' SINGLEINSTANCE='yes' WINDOWSTATE='normal'>"
    28.  
    29.     outputWin.writeln "<BODY  bgcolor=ButtonFace ScrollBar='No'>"
    30.     outputWin.writeln "<TABLE cellSpacing=1 cellPadding=1 width='75pt' border=1>"
    31.     outputWin.writeln "<TBODY>"
    32.    
    33.     outputWin.writeln "<TR>"
    34.     outputWin.writeln "<TD>"
    35.     outputWin.writeln "<P align=center><TEXTAREA name=Information rows=6 cols=57 style='WIDTH: 412px; HEIGHT: 284px'></TEXTAREA></P></TD></TR>"
    36.     outputWin.writeln "<TR>"
    37. '   outputWin.writeln "<TD><P align=center><INPUT id=button1 style='WIDTH: 112px; HEIGHT: 24px' type=button size=38 value='Install Patches' name=button1></P></TD>"
    38.     outputWin.writeln "</TR>"
    39.     outputWin.writeln "<TR>"
    40.     outputWin.writeln "<TD></TD></TR></TBODY></TABLE>"
    41.     outputWin.writeln "</BODY>"
    42.  
    43.     IEObj.Visible=True
    44.     'Get the Information textarea object from the window
    45.     set tempObj=outputWin.getElementsByName("Information")
    46.     objFound=false
    47.     'loop through its object to find what we need
    48.     For each objN in tempObj
    49.         if objN.name="Information" then
    50.             objFound=true
    51.             set tArea=objN
    52.         end if
    53.     next
    54.     'if we didnt find the object theres a problem
    55.     if ObjFound=False then
    56.         'so show an error and bail
    57.         MsgBox "Unable to access the TextBox on IE Window",32,"Error"
    58.         WScript.Quit
    59.     end if
    60.  
    61.  
    62.  
    63. '*************************
    64. 'ADMINS: The below is all you should really have to change.
    65. '*************************
    66.  
    67. 'Change this to the location of the patches that will be installed.
    68.         'they should be limited to the amout you try to install at one time.
    69.         'ALSO the order they are installed is how explorer would list them by alphabetically.
    70.     'So given file names:
    71.     'patch1.exe
    72.     'patch2.exe
    73.     'patch11.exe
    74.     'installation order would be patch1.exe,patch11.exe, patch2.exe
    75.  
    76. PatchFolder="C:\Patches\Install"
    77.  
    78. 'Change this to location where the patches will be copied to on remote cp. This directory must exist on remote computer.
    79. 'I have it hidden on all workstations.
    80. RemotePatchFolder="C:\Install\"
    81.  
    82. 'Workstation names to refer to as array
    83. wsNames(1)="192.168.0.10"
    84. wsNames(2)="192.168.0.11"
    85.  
    86. 'number of remote computers
    87. numComputers=2
    88. '**********************
    89. 'ADMINS: The above is all you should really have to change.
    90. '**********************
    91.  
    92. 'Copy files to remote computers.
    93.  
    94. 'Get a list of the executable file in the folder and put them into the InstallFiles array
    95. 'on return, retVal will be number of files found.
    96. retVal=GetPatchFileList (PatchFolder,InstallFiles)
    97.  
    98. 'for each file copy to remote computers
    99. For cc=1 to numComputers                        'for each computer
    100.     For i = 1 to retVal                             'for each file
    101.         Dim copySuccess
    102.         Dim SharedDriveFolder
    103.             'do a replacement on the : to $, this means you must have admin priv
    104.             'this is because i want to copy to  "\\remotecpname\c$\PathName"
    105.             SharedDriveFolder=replace(RemotePatchFolder,":","$")
    106.             'copy it from the PatchFolder to the path on destination computer
    107.             'USE: RemoteCopyFile (SourceFilePath,DestinationFilePath, RemoteComputerName)
    108.             CurrentCP=cc
    109.             copySuccess=RemoteCopyFile(PatchFolder & "\" & InstallFiles(i),SharedDriveFolder,wsNames(CurrentCP))
    110.         if copySuccess=true then
    111.                 tArea.Value=tArea.Value &  PatchFolder & "\" & InstallFiles(i) & " copy - OK" & vbcrlf
    112.         else
    113.             tArea.Value=tArea.Value &  PatchFolder & "\" & InstallFiles(i) & " copy - FAILED" & vbcrlf
    114.         end if
    115.     Next
    116. Next
    117.  
    118.  
    119. 'Install the files on remote computer
    120.  
    121. 'go through each filename and start that process on remote PC.
    122. 'for each file install them on the computers.
    123. For cc=1 to numComputers
    124.  
    125.     'if theres more than one patch
    126.     if retVal>1 then
    127.     For i=1 to retVal-1
    128.                 CurrentCp=cc
    129.        
    130.                 'Now create a process on remote computer
    131.                 'USE: CreateProcessandwait( ComputerName, ExecutablePathonRemoteComputer
    132.                 'Create a process on the remote computer and waits. Now this can return a program terminated which is ok,
    133.                 'if it returns cancelled it means the process was stopped, this could happen if the update required a
    134.                 'computer restart.
    135.        
    136.                 CreateProcessandWait wsNames(CurrentCP), RemotePatchFolder & InstallFiles(i) & " /quiet /norestart", tArea
    137.  
    138.     next
    139.     end if
    140.  
    141.     'do the last patch with a forcereboot
    142.     CreateProcessandWait wsNames(CurrentCP), RemotePatchFolder & InstallFiles(retVal) & " /quiet"  & " /forcereboot" , tArea
    143.  
    144. next
    145.  
    146. tArea.value=tArea.Value & "Script Complete!" & vbcrlf
    147.  
    148.  
    149. '****************************       FUNCTIONS
    150.  
    151. 'Get list of files in Folder.
    152.  
    153. Function GetPatchFileList(FileFolder, FileStringArray())
    154.  
    155.         'create file system object
    156.         Set objFS=CreateObject("Scripting.FileSystemObject")
    157.         'set the a variable to point to our folder with the patches in it.
    158.         Set objFolder=objFS.GetFolder(FileFolder)
    159.         'set the initial file count to 0
    160.         numPatches=0
    161.  
    162.         for each objFile in objFolder.Files
    163.         if UCase(Right(objFile.Name,4))=".EXE" then
    164.                 numPatches=numPatches+1
    165.  
    166.                 redim preserve FileStringArray(numPatches)
    167.                 FileStringArray(numPatches)=objFile.Name
    168.         end if
    169.  
    170.         next
    171.         GetPatchFileList=numPatches    
    172. End Function
    173.  
    174. 'Copy files to remote computer.
    175.  
    176. Function RemoteCopyFile(SrcFileName,DstFileName,DestinationComputer)
    177. Dim lRetVal
    178.         'create file system object
    179.         Set objFS=CreateObject("Scripting.FileSystemObject")
    180.  
    181.         lRetVal=objFS.CopyFile (SrcFileName, "\\" & DestinationComputer & "\" & DstFileName)
    182.         if lRetVal=0 then
    183.                 RemoteCopyFile=True
    184.         else
    185.                 RemoteCopyFile=False
    186.         end if
    187. End Function
    188.  
    189.  
    190. 'Create process on remote computer and wait for it to complete.
    191.  
    192. Function CreateProcessAndWait(DestinationComputer,ExecutableFullPath,OutPutText)
    193.         Dim lretVal        
    194.  
    195.         strComputer= DestinationComputer
    196.         Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_Process")
    197.         Set objWMIServiceStart= GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_ProcessStartup")
    198.  
    199.         Set objConfig = objWMIServiceStart.SpawnInstance_
    200.        
    201.         objConfig.ShowWindow = 1        'show window or use HIDDEN_WINDOW
    202.  
    203.  
    204.  
    205.         lretVal= objWMIService.Create(ExecutableFullPath, null, objConfig, intProcessID)        
    206.         if lretVal=0 then
    207.                 OutPutText.Value = OutPutText.Value & "Process created with ID of " & intProcessID & " on " & DestinationComputer & vbcrlf
    208.                 OutPutText.Value = OutPutText.Value & "     Waiting for process " & intProcessID & " to complete." & vbcrlf
    209.  
    210.                 WaitForPID strComputer, intProcessID,OutPutText
    211.         OutPutText.Value = OutPutText.Value & "Process complete." & vbcrlf
    212.         else
    213.                 OutPutText.Value = OutPutText.Value & "Unable to start process " & ExecutableFullPath & " on " & DestinationComputer & vbcrlf
    214.         end if
    215. End Function
    216.  
    217.  
    218. 'Wait for PRocess to complete
    219.  
    220. Function WaitForPID(ComputerName,PIDNUMBER,OutPutText)
    221. Dim ProcessNumber
    222.  
    223. Set objWMIServiceQ = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & ComputerName & "\root\cimv2")
    224. Set colItems = objWMIServiceQ.ExecQuery("Select * from Win32_Process",,48)
    225. For Each objItem in colItems
    226.         'check if this process is the one we are waiting for
    227.         if objItem.ProcessID=PIDNUMBER then
    228.         OutPutText.Value = OutPutText.Value & "Process Info:" & vbcrlf
    229.         OutPutText.Value = OutPutText.Value & " Description: " & objItem.Description & vbcrlf
    230.         OutPutText.Value = OutPutText.Value & " ExecutablePath: " & objItem.ExecutablePath  & vbcrlf
    231.         OutPutText.Value = OutPutText.Value & " Name: " & objItem.Name  & vbcrlf
    232.         OutPutText.Value = OutPutText.Value & " Status: " & objItem.Status  & vbcrlf
    233.         OutPutText.Value = OutPutText.Value & " ThreadCount: " & objItem.ThreadCount  & vbcrlf
    234.         ProcessNumber=objItem.ProcessID
    235.         end if
    236.  
    237. Next
    238.  
    239.         PidWaitSQL="SELECT TargetInstance.ProcessID " & " FROM __InstanceDeletionEvent WITHIN 4 " _
    240.         & "WHERE TargetInstance ISA 'Win32_Process' AND " _
    241.         & "TargetInstance.ProcessID= '" & ProcessNumber & "'"
    242.  
    243.         Set Events = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & ComputerName & "\root\cimv2").ExecNotificationQuery (PidWaitSQL)
    244.  
    245.  
    246.  
    247.         Set TerminationEvent = Events.nextevent
    248.         OutPutText.Value = OutPutText.Value & "Program " & TerminationEvent.TargetInstance.ProcessID & _
    249.         " terminated. " & vbcrlf
    250.         set TerminationEvent=Nothing
    251.         exit function
    252.        
    253. End Function


    packetvb

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Okinawa, Japan
    Posts
    271

    Re: [vbs] Installing patches remotely

    Over 600 views and no comments?
    Anyway theres a logic problem in the script source.

    packetvb

  3. #3
    New Member
    Join Date
    Feb 2005
    Posts
    1

    Thumbs up Re: [vbs] Installing patches remotely

    Hi, Just wanted to extend my gratitude for the patch installation script. I have been wrestling with this all day and getting nowhere. this will make life a lot easier in the future. All I need to do is understand why yours works and mine doesn't...

  4. #4
    New Member
    Join Date
    May 2005
    Posts
    1

    Thumbs up Re: [vbs] Installing patches remotely

    PacketVB this is a good script -- I have successfully used it for patching two test servers. Is there a way to read the server list out of a file instead of having to add all the servers in an array. I have quite a lot of servers to update.

    Thank you in advance for your help on this.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Okinawa, Japan
    Posts
    271

    Re: [vbs] Installing patches remotely

    jrex7,
    Thanks.
    Hmm havent tried that from script. Ive done it from vb using API's.
    I wonder if WMI has something that would return a list from active directory, would need 2k or above though.

    When I get time ill have to check into that.
    But you may be able to get a com component that will allow you to call API from script. Cant remeber the name of the API.

    Packetvb

  6. #6
    New Member
    Join Date
    Mar 2009
    Posts
    1

    Re: [vbs] Installing patches remotely

    I see its been awhile since any replied to this, but is there a way to make the script to continue if it doesnt find one of the computers? Everytime that I run this it all goes well until it finds a computer that is not connectable (Turned off or in sleep mode). When it does find this it tells me that there is error. Please help. thanks.

  7. #7
    New Member
    Join Date
    Apr 2010
    Posts
    1

    Post Re: [vbs] Installing patches remotely

    Hi,

    I am new to this patching activities and vb scripting. Actually in our environment we are using a centralized location over a network to store patches.we are installing patches from that location for ever servers.I wanna automate this process, i need a vb-script to do the following task,

    1.First to check the operating system and copy the relevant patches from the shared location.
    2.Then install the patches and finally reboot the servers.
    3.The script can able to get list of patches as an input from a file and also it should create a log file for these activities.


    kindly guide me on this. Thanks in advance.


    regards,

    Jack

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.