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.

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

6 Comments

Leave a Reply to David Jeffrey Cancel reply

Your email address will not be published.