'USB Bootable Script
'Created by - Roshan Karkera (K.roshan)
'Techie Inspire - www.techieinspire.com
set newshell = createobject("wscript.shell")

If WScript.Arguments.length =0 Then 
	Set objShell = CreateObject("Shell.Application")
	objShell.ShellExecute "wscript.exe", Chr(34) & _
	WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else
	On Error Resume Next
	'Detect USB Drives
	ComputerName = "."
	Set wmiServices  = GetObject ( _
    "winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName)
	' Get physical disk drive
	Set wmiDiskDrives =  wmiServices.ExecQuery ( "SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType = 'USB'")
	Set obj = CreateObject("Scripting.Dictionary")
	set fso = createobject("scripting.fileSystemObject")
	file = replace(Wscript.ScriptFullName,Wscript.ScriptName,"diskpart.txt")   				
	EnumDrives
	temp= "Disk ID" & space(5) & "Drive" & space(19) & "Desc" & vbcrlf & "======" & space(4) & "=====" & space(7) & "================"
	For Each wmiDiskDrive In wmiDiskDrives
		idx=right(wmiDiskDrive.DeviceID,1)

		'Use the disk drive device id to find associated partition
		query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"  
		Set wmiDiskPartitions = wmiServices.ExecQuery(query)
			For Each wmiDiskPartition In wmiDiskPartitions       'Use partition device id to find logical disk
			Set wmiLogicalDisks = wmiServices.ExecQuery ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
             & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") 
				For Each wmiLogicalDisk In wmiLogicalDisks
				temp = temp & vbcrlf & space(4) & idx & space(13) & wmiLogicalDisk.DeviceID  & space(6) & left (wmiDiskDrive.Caption,28)		
				Next    
			Next
	Next
	start = "true"
	Do while start = "true"
	refDriveID = inputbox(temp & vbcrlf & vbcrlf & "Enter Disk ID to make it bootable, or click on Cancel to Quit", "Select Drive ID...") '"======SELECT A DEVICE ==========" & vbcrlf & "Enter the Disk ID to make it bootable, or click on Cancel to quit","Select Device To Format")
	
		if (refDriveID="") then
			msgbox "USB boot script program closing..." 
			wscript.quit
		End if

	if obj.exists(refDriveID) then
			result = MsgBox ("This will format your USB drive:" & vbCrLf & "Drive: " & obj.item(refDriveID).Letter &_
			vbTab & obj.item(refDriveID).Caption & vbCrLf & vbCrLf & "All data on this USB will be deleted." & vbCrLf & "Do you want to continue?", vbYesNo, "Confirm Drive Format")
			if result = vbYes then
			
				'Create Diskpart Script
				on error resume next
    				set ofile = fso.CreateTextFile(file,true)
					ofile.write "select disk " & refDriveID & vbCrLf 
					ofile.write  "clean" & vbCrLf 
					ofile.write  "create partition primary" & vbCrLf 
					ofile.write  "assign letter=" & obj.item(refDriveID).Letter & vbCrLf
					ofile.write  "active" & vbCrLf 
					ofile.write "format fs=fat32" & " label=BootableUSB"
    				ofile.close
	
				chkError = err.Number	
				DiskPart = newshell.run ("diskpart.exe /s " & chr(34) & file & chr(34),1,true)
				chkError = DiskPart
				if chkError<>0 then
					msgbox "Error occurred on DiskPart operation. Error number: " & chkError
					wscript.quit
				else
				Wscript.echo ("Operation Completed Successfully..." )
				start=false
				end if
			else
				if msgbox ("Do you want to quit?",vbYesNo,"Confirm") = vbYes then
					msgbox "USB boot script program closing..." 
					wscript.quit
				end if
			end if

		else
			msgbox "Invalid Disk ID"
		end if
	loop
	

End If

Class myfClass 
	public DeviceLetter
	public DeviceIDx
	public DeviceID
	public Letter
	Public Caption
End Class
Sub EnumDrives
	for each mydrive in wmiDiskDrives
		set driveClass = new myfClass
		driveClass.DeviceLetter = Replace(mydrive.DeviceID, "\", "\\")
		driveClass.deviceIDx = right(mydrive.DeviceID,1)
		driveClass.DeviceID = mydrive.DeviceID
		driveClass.Caption = mydrive.Caption

			    query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & mydrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"  
		Set wmiDiskPartitions = wmiServices.ExecQuery(query)
		For Each wmiDiskPartition In wmiDiskPartitions       'Use partition device id to find logical disk
        Set wmiLogicalDisks = wmiServices.ExecQuery ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
             & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") 

        For Each wmiLogicalDisk In wmiLogicalDisks
		driveClass.Letter =  left(wmiLogicalDisk.DeviceID,1)
		next
		next
		obj.Add driveClass.DeviceIDx, driveClass
		

	next
End Sub
	
	
