To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
The ASP.NET Chart Control
Article :: Introduction to SharePoint Development in Visual Studio 2010
Part 10 of the Visual Basic .NET 2010 Express Tutorial Complete!
How to Use the Visual Studio Code Analysis Tool FxCop
Article :: Interview with Andrei Alexandrescu (Part 3 of 3)



Go Back   VBForums > VBForums CodeBank > CodeBank - Other

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


packetvb
packetVB is offline   Reply With Quote
Old Jan 6th, 2005, 10:18 PM   #2
packetVB
Hyperactive Member
 
Join Date: Sep 02
Location: Okinawa, Japan
Posts: 271
packetVB is on a distinguished road (20+)
Re: [vbs] Installing patches remotely

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

packetvb
packetVB is offline   Reply With Quote
Old Feb 10th, 2005, 10:10 PM   #3
AndyTyler
New Member
 
Join Date: Feb 05
Posts: 1
AndyTyler is an unknown quantity at this point (<10)
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...
AndyTyler is offline   Reply With Quote
Old May 10th, 2005, 12:57 PM   #4
jrex7
New Member
 
Join Date: May 05
Posts: 1
jrex7 is an unknown quantity at this point (<10)
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.
jrex7 is offline   Reply With Quote
Old May 11th, 2005, 05:28 PM   #5
packetVB
Hyperactive Member
 
Join Date: Sep 02
Location: Okinawa, Japan
Posts: 271
packetVB is on a distinguished road (20+)
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
packetVB is offline   Reply With Quote
Old Mar 30th, 2009, 01:47 PM   #6
maddvldg
New Member
 
Join Date: Mar 09
Posts: 1
maddvldg is an unknown quantity at this point (<10)
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.
maddvldg is offline   Reply With Quote
Old Apr 8th, 2010, 02:18 AM   #7
jack here
New Member
 
Join Date: Apr 10
Posts: 1
jack here is an unknown quantity at this point (<10)
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
jack here is offline   Reply With Quote
Reply

Go Back   VBForums > VBForums CodeBank > CodeBank - Other


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:42 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.