Blog

Deploy and run DeFraggler

This script will run Defraggler with the parameters of your choice.
It will also deploy it if its not already installed.

A full list of parameters can be found here – http://www.piriform.com/docs/defraggler/advanced-usage/command-line-parameters

**Update** – 24/10/2012
Hey guys,

Sorry but I forgot GFI ads a -logfile parameter at the end of scripts so I have updated it to prevent it passing to Defraggler.

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
Dim strFile
 
'Defraggler URL
strURL = "http://download.piriform.com/dfsetup211.exe"
strExecute = "dfsetup211.exe /S"
strOutput = ""
strParameters = ""
 
'Detect Arch
Set WshShell = CreateObject("WScript.Shell")
osType = WshShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")
 
If osType = "x86" Then
	strDF = "C:\Program Files\Defraggler\df.exe"
ElseIf osType = "AMD64" Then
	strDF = "C:\Program Files\Defraggler\df64.exe"
Else
	WScript.Echo "ERROR: Unknown Architechture " & osType
	WScript.Quit(2)
End If
 
'Get Run Parameters
Set objArgs = Wscript.Arguments
 
For Each strArg in objArgs
	If (strParameters = "") Then
		strParameters = strArg
	Else
		If InStr(strArg, "log") = 0 Then
			strParameters = strParameters & " " & strArg
		End if
	End If
Next
 
' Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
If objFSO.Fileexists(strDF) Then
	RunDefragler
Else
	'Get Temp Folder
	strSaveTo = WScript.CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2)
 
	' Check if the specified target file or folder exists,
	' and build the fully qualified path of the target file
	If objFSO.FolderExists(strSaveTo) Then
		strFile = objFSO.BuildPath(strSaveTo, Mid(strURL, InStrRev(strURL, "/" ) + 1 ) )
	ElseIf objFSO.FolderExists(Left(strSaveTo, InStrRev(strSaveTo, "\" ) - 1 ) ) Then
		strFile = strSaveTo
	Else
		WScript.Echo "ERROR: Target folder not found."
		WScript.Quit(2)
	End If
 
	'Download Program
	WScript.Echo "Defraggler NOT FOUND"
	WScript.Echo "Downloading " & strURL & " to " & strSaveTo
	HTTPDownload strURL, strFile
 
	'Install program
	Set oCmd = CreateObject("Wscript.Shell") 
 
	commandLine = "%comspec% /c " & strSaveTo & "\" & strExecute 
	WScript.Echo "Running " & commandLine
	oCmd.Run commandLine, 0, True
 
	WScript.Echo "Defraggler Installed!"
	RunDefragler
 
	WScript.Quit(0)
 
End If
 
Sub RunDefragler()
	WScript.Echo "Running " & strDF & " " & strParameters
 
	Set oExec = WshShell.Exec(strDF & " " & strParameters)
 
	Do While oExec.Status <> 1
		WScript.Sleep 100
	Loop
 
	Do While oExec.StdOut.AtEndOfStream <> True 
		strOutput = strOutput & oExec.StdOut.ReadLine & vbcrlf
	Loop
 
	WScript.Echo strOutput
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: Blog, Scripts

Leave a Comment (6) ↓

6 Comments

  1. Paulu013 December 21, 2012

    Would it be possible to check if the harddrive is a SSD first and if so skip the deployment alltogether, as it is not recommended to defrag a SSD.

    reply
    • wpadmin February 19, 2013

      If you can find me a way to determine if a drive is an SSD then I would be happy to incorporate it.

      reply
  2. Perry March 19, 2013

    Hi,
    We would like to use a simlar script to deploy and run MalwareBytes, have you ever completed anything similar that you would share? we are looking touse the licensed version.

    thanks!

    reply
    • wpadmin March 19, 2013

      I haven’t for malwarebytes no, but its easy enough to do.

      reply
  3. Burak April 10, 2013

    How about something for Ccleaner with custom options? (my clients hate it when they lose the passwords after a ccleaner)

    reply
    • wpadmin April 10, 2013

      Pretty sure CCleaner options are only for the paid version.
      Otherwise the default settings are used.

      reply

Leave a Comment