Solved

VBS Format multiple USB drives

Posted on 2013-06-24
2
523 Views
Last Modified: 2013-06-24
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
Comment
Question by:tomdenton
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39270974
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
 

Author Closing Comment

by:tomdenton
ID: 39271078
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now