' --- KON-BOOT USB INSTALLER -------------------------------
' (c) kon-boot.com 
' ----------------------------------------------------------
Dim query 
Dim WMBIObj 
Dim AllDiskDrives 
Dim SingleDiskDrive 
Dim AllLogicalDisks 
Dim SingleLogicalDisk 
Dim AllPartitions 
Dim Partition 
Dim result
Dim textmsg
Dim wshShell
Dim FileObj
Dim Counter

set FileObj = CreateObject("Scripting.FileSystemObject")
set wshShell = wscript.createObject("wscript.shell")
Set WMBIObj = GetObject("winmgmts:\\.\root\cimv2")
Set AllDiskDrives = WMBIObj.ExecQuery("SELECT * FROM Win32_DiskDrive where InterfaceType='USB'") ' 
For Each SingleDiskDrive In AllDiskDrives 
	counter = counter + 1
    query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + SingleDiskDrive.DeviceID + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition" 
    Set AllPartitions = WMBIObj.ExecQuery(query) 
    For Each Partition In AllPartitions 
        query = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + Partition.DeviceID + "'} WHERE AssocClass = Win32_LogicalDiskToPartition"
        Set AllLogicalDisks = WMBIObj.ExecQuery (query) 
        For Each SingleLogicalDisk In AllLogicalDisks  
			
			textmsg = 	"============================================" & VbCr & _ 
						"DeviceID: " & SingleDiskDrive.DeviceID & VbCr & _ 
						"Logical Drive: " & SingleLogicalDisk.DeviceID  & VbCr & _ 
						"Model: " & SingleDiskDrive.Model & VbCr & _ 
						"Manufacturer: " & SingleDiskDrive.Manufacturer & VbCr & _
						"============================================" & VbCr & _ 
						"Would you like to use this drive as destination?" & VbCr & _ 
						"Warning, disk data may be overwritten"
			result = MsgBox(textmsg, vbQuestion + vbOKCancel, "Kon-Boot USB Installer")
			
			if result = vbOk Then
				wshShell.Run "grubinst.exe --skip-mbr-test " & SingleDiskDrive.DeviceID, 1, true

				FileObj.CopyFolder CDir & "\EFI", SingleLogicalDisk.DeviceID & "\", 1			

				FileObj.CopyFile "USBFILES\grldr", SingleLogicalDisk.DeviceID & "\", 1
				FileObj.CopyFile "USBFILES\konboot.img", SingleLogicalDisk.DeviceID & "\", 1
				FileObj.CopyFile "USBFILES\menu.lst", SingleLogicalDisk.DeviceID & "\", 1
				
				MsgBox "Your Kon-Boot on USB is ready!", vbInformation + vbOKOnly, "Kon-Boot USB Installer"
				WScript.quit 
			End If
		Next
    Next 
Next 
if counter = 0 Then
	MsgBox "No USB disks detected or unknown error!", vbCritical + vbOkOnly, "Kon-Boot USB Installer"
End If