Blog

Archive for April, 2013

PatchMyPC Script

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'------------------------------------------------------------------------------ 
' PathMyPC.vbs 
'------------------------------------------------------------------------------ 
' Script that will update software. 
'------------------------------------------------------------------------------
'Usage: PathMyPC.vbs /EnableWU:<True/False> /AllCommon:<True/False>
' 
'   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:<True/False> /AllCommon:<True/False>"
 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

Posted in: Scripts

Leave a Comment (3) →