?
Solved

VBS Format multiple USB drives

Posted on 2013-06-24
2
Medium Priority
?
552 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

800 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