[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 563
  • Last Modified:

VBS Format multiple USB drives

Hi, I found this code on EE the lists the USB drives on a computer:
bolDeviceFound = False 
strComputer = "."

'query the WMI service for the computer name
 
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
 
Set colItems = objWMIService.ExecQuery ("SELECT * from Win32_LogicalDisk WHERE Description = 'Removable Disk'")
 
For Each objItem in colItems
	WScript.Echo "The computer has USB drive letter: " & objItem.Name
	bolDeviceFound = True
Next
 
If bolDeviceFound = False Then
	WScript.Echo "NO USB devices found"
End If

Open in new window

And i have this that will format and name a USB  (E: in this case):
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
   
Set colVolumes = objWMIService.ExecQuery _
("Select * from Win32_Volume Where Name = 'E:\\'")

For Each objVolume in colVolumes
  errResult = objVolume.Format("NTFS", true,,"ATT")
Next

Open in new window

I get errors when i try to combine them - what i would like to do is combine them so the script loops through the USB drives and formats and names them all.

I may be coming at this the wrong way so any suggestions please?

PS I compile the script and 'run it as admin'.
0
tomdenton
Asked:
tomdenton
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
try this:
bolDeviceFound = False 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
   
Set colItems = objWMIService.ExecQuery ("SELECT * from Win32_LogicalDisk WHERE Description = 'Removable Disk'")
 
For Each objItem in colItems
	bolDeviceFound = True
	
	dim driveLetter = objItem.Name
	WScript.Echo "The computer has USB drive letter: " & driveLetter
	
	Set colVolumes = objWMIService.ExecQuery ("Select * from Win32_Volume Where Name = '" & driveLetter & ":\\'")

	For Each objVolume in colVolumes
	  errResult = objVolume.Format("NTFS", true,,"ATT")
	Next
Next
 
If bolDeviceFound = False Then
	WScript.Echo "NO USB devices found"
End If

Open in new window

0
 
tomdentonAuthor Commented:
Great job, thanks :)

I had to add 'dim driveLetter' separately and remove dim from line 11 at the start and also we had an extra ':' on line 14 but when I deleted that it works great

thanks again
Tom

Final version in case it helps others:

dim driveLetter
bolDeviceFound = False
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("SELECT * from Win32_LogicalDisk WHERE Description = 'Removable Disk'")
For Each objItem in colItems
      bolDeviceFound = True
      driveLetter = objItem.Name
      WScript.Echo "The computer has USB drive letter: " & driveLetter
      Set colVolumes = objWMIService.ExecQuery ("Select * from Win32_Volume Where Name = '" & driveLetter & "\\'")
      For Each objVolume in colVolumes
        errResult = objVolume.Format("NTFS", true,,"ATT")
      Next
Next
If bolDeviceFound = False Then
      WScript.Echo "NO USB devices found"
End If
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now