PatchMyPC Script

I wrote this a little while ago, but for some reason I forgot to post it.
Here it is 🙂

'------------------------------------------------------------------------------ 
' PathMyPC.vbs 
'------------------------------------------------------------------------------ 
' Script that will update software. 
'------------------------------------------------------------------------------
'Usage: PathMyPC.vbs /EnableWU: /AllCommon:
' 
'   EnableWU: This switch will run Windows updates automatically after third party updates
'   AllCommon: This switch will install all the programs in the common list that aren't installed unless the program is marked as skipped.
' 
' ex: PathMyPC.vbs /EnableWU:True /AllCommon:False
'
'------------------------------------------------------------------------------ 
' Author: Jake Paternoster
' Created: 12/04/2013 (Info@Screwloose.com.au)
'------------------------------------------------------------------------------ 

Dim WshShell, oExec, OsType, FSO, strFolder, AgentPath, strURL, strKey
Set WshShell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
 
OsType = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
strURL = "http://patchmypc.net/PatchMyPC.exe"
strParameters = "/s"

EnableWU = WScript.Arguments.Named("EnableWU")

If lcase(EnableWU) = "true" Then 
	strParameters = strParameters & " /update"
End If 

AllCommon = WScript.Arguments.Named("AllCommon")

If lcase(AllCommon) = "true" Then 
	strParameters = strParameters & " /applyallcommon"
End If 
 
 If OsType = "x86" then
     AgentPath = "C:\Program Files\"
 ElseIf OsType = "AMD64" then
     AgentPath = "C:\Program Files (x86)\"
 End If
 
 If FSO.FolderExists(AgentPath & "Advanced Monitoring Agent") Then
     AgentPath = AgentPath & "Advanced Monitoring Agent"
 ElseIf FSO.FolderExists(AgentPath & "Advanced Monitoring Agent GP") Then
     AgentPath = AgentPath & "Advanced Monitoring Agent GP"
 Else
     wscript.Echo "Agent Folder Not Found."
	 wscript.exit(2000)
 End If

strFolder = AgentPath & "\PatchMyPC"
 
 If Not FSO.FolderExists(strFolder) Then
	WScript.Echo "Creating Folders..."
	FSO.CreateFolder strFolder
	FSO.CreateFolder strFolder & "\Logs"
End If

If Not FSO.FileExists(strFolder & "\PatchMyPC.exe") Then
	WScript.Echo "Downloading PatchMyPC..."
	HTTPDownload strURL, strFolder & "\PatchMyPC.exe"
End If

WScript.Echo "Setting Registry Values"

strKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Patch My PC"
WshShell.RegWrite strKey & "\PATH", strFolder, "REG_SZ"
WshShell.RegWrite strKey & "\Options\AutoUpdatePatchMyPC", "1", "REG_SZ"
WshShell.RegWrite strKey & "\Options\KillPrograms", "1", "REG_SZ"
WshShell.RegWrite strKey & "\Options\MinimizeToTrayUpdates", "1", "REG_SZ"
WshShell.RegWrite strKey & "\Options\MRemoveIcons", "1", "REG_SZ"
WshShell.RegWrite strKey & "\Options\RestorePoint", "1", "REG_SZ"
WshShell.RegWrite strKey & "\Options\LogSaveLocation", strFolder & "\Logs", "REG_SZ"

WScript.Echo "Running PatchMyPC..."

ret = WshShell.Run(chr(34) & strFolder & "\PatchMyPC.exe" & chr(34) & " " & strParameters, 1, true)

If ret = 0 then
	WScript.Echo "PatchMyPC completed Successfully"
Else
	WScript.Echo "PatchMyPC Encountered and error!"
End If

'Wait 2 seconds to make sure the log file is written.
WScript.Sleep 2000

WScript.Echo ""
WScript.Echo "PatchMyPC Results:"
WScript.Echo "=================================="
Set folder = FSO.GetFolder(strFolder & "\Logs")
Set files = folder.Files
  
For Each logs In files
	Set objFile = FSO.OpenTextFile(strFolder & "\Logs\" & logs.Name, 1)
	
	Do Until objFile.atEndOfStream 
        WScript.Echo objFile.readLine
	Loop   
	
	objFile.Close
	
	FSO.DeleteFile(strFolder & "\Logs\" & logs.Name)
Next
 
Sub usage 
 '------------------------------------------------------------------------------ 
 WScript.Echo "Usage: PathMyPC.vbs /EnableWU: /AllCommon:"
 WScript.Echo " "
 WScript.Echo "EnableWU: This switch will run Windows updates automatically after third party updates"
 WScript.Echo "AllCommon: This switch will install all the programs in the common list that aren't installed unless the program is marked as skipped."
 WScript.Echo ""
 WScript.Echo "ex: PathMyPC.vbs /EnableWU:True /AllCommon:False"
 
 WScript.Quit(1) 
End Sub 
 
Sub HTTPDownload(myURL, strFile)
 
 Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
 
 objXMLHTTP.open "GET", myURL, false
 objXMLHTTP.send()
 
 If objXMLHTTP.Status = 200 Then
 Set objADOStream = CreateObject("ADODB.Stream")
 objADOStream.Open
 objADOStream.Type = 1 'adTypeBinary

 objADOStream.Write objXMLHTTP.ResponseBody
 objADOStream.Position = 0 'Set the stream position to the start

 objADOStream.SaveToFile strFile
 objADOStream.Close
 Set objADOStream = Nothing
 End if
 
 Set objXMLHTTP = Nothing
End Sub

4 Comments

  • Great stuff!!

    My only question is I tried running this from command prompt before testing it through GFI. It worked great but prompted a lot of windows I had to hit “OK” to pass.
    Any way to avoid that?

  • Still not great with vbscripts. How do I add items to the skipped list?

  • I had to change line 122 to (“MSXML2.ServerXMLHTTP”) or I was getting an access denied error.

  • We absolutely love your blog and find nearly all of your post’s to be precisely what I’m looking
    for. Would you offer guest writers to write content to suit your needs?
    I wouldn’t mind composing a post or elaborating on a lot of the
    subjects you write about here. Again, awesome web site!

    My web-site … judi deposit pulsa tanpa potongan

Leave a Reply

Your email address will not be published.