?
Solved

VBScript: How do I run a command that contains a object or variable?

Posted on 2009-04-07
10
Medium Priority
?
349 Views
Last Modified: 2012-05-06
I am trying to write a script that will help me to copy a large number of USB Disks.  I have most of the code written but having trouble w/ a small section.  I need to run a command that uses one of output from a WMI query.  I have option explicit turned on and have "Dim'd" all necessary variables.

      formatUSB = "cmd /c format " & objItem.DeviceID, "/FS:NTFS /Q"
      WScript.Echo "Formatting USB Drive..."
      Set objShell = CreateObject ("WScript.Shell")
      Set objExec2 = objShell.Exec (formatUSB)

When I run this script, I get Expected ')" or Expected end of statement.  Is this possible to do?
0
Comment
Question by:jleleux
[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
  • 6
  • 3
10 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 24089524
remove the comma (,) after DeviceID
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24089527

formatUSB = "cmd /c format " & objItem.DeviceID " /FS:NTFS /Q"
      WScript.Echo "Formatting USB Drive..."
      Set objShell = CreateObject ("WScript.Shell")
      Set objExec2 = objShell.Exec (formatUSB)

Open in new window

0
 
LVL 38

Expert Comment

by:Shift-3
ID: 24089569
You'll need another ampersand in there too.
 

formatUSB = "cmd /c format " & objItem.DeviceID & " /FS:NTFS /Q"

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:jleleux
ID: 24090523
Now I get the following error:

Microsoft VBScript runtime error: Object required: ''
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24090536
try this - if the error persists, please indicate which line throws the error.
formatUSB = "cmd /c format " & objItem.DeviceID & " /FS:NTFS /Q"
WScript.Echo "Formatting USB Drive..."
Set objShell = CreateObject ("WScript.Shell")
Set objExec2 = objShell.Exec (formatUSB)

Open in new window

0
 
LVL 1

Author Comment

by:jleleux
ID: 24090584
Here is the whole program, this might be more helpful.  The error is happening on line 38, col 2.

Option Explicit
'On Error Resume Next
Dim strComputer
Dim objWMIService
Dim colItems
Dim objItem
Dim copyDev
Dim formatUSB
Dim choice
Dim objShell
Dim objExec1
Dim objExec2

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const DriveType = 2

strComputer = "."
   WScript.Echo
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk where DriveType =" & DriveType, _
                                         "WQL",wbemFlagReturnImmediately + wbemFlagForwardOnly)
     
Do
      For Each objItem In colItems
          WScript.Echo "DeviceID: " & objItem.DeviceID
          WScript.Echo "VolumeName: " & objItem.VolumeName
      'If objItem.VolumeName Or objItem.DeviceID    
      Next
      
      ' Copy Microsoft DevCon utility
      copyDev = "cmd /c xcopy P:\Software\Miscellaneous\devcon.exe c:\windows\system32 /y"
      WScript.Echo "Copying needed utility..."
      Set objShell = CreateObject ("WScript.Shell")
      Set objExec1 = objShell.Exec (copyDev)
      
      ' Format Drive
      formatUSB = "cmd /c format " & objItem.DeviceID & " /FS:NTFS /Q"
      WScript.Echo "Formatting USB Drive..."
      Set objShell = CreateObject ("WScript.Shell")
      Set objExec2 = objShell.Exec (formatUSB)
      
      
      ' Asks the user if they want to copy another drive or exit the script.
      WScript.StdOut.Write "Do you want to copy another drive? (y/n)"
      choice = WScript.StdIn.ReadLine
Loop While choice = "y" Or choice = "Y"
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24090652
yep, it's your objitem - I'll clean up the script for you...
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 total points
ID: 24090690
Try this - unless there's more to it, there was some code that was either redundant or unnecessary...
Option Explicit
'On Error Resume Next
 
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const DriveType = 2
Dim objExec, objDrive, strCommand, strDrive, choice
 
Dim objShell : Set objShell = CreateObject ("WScript.Shell")
Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Dim colItems : Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk where DriveType =" & DriveType, _
                                         "WQL",wbemFlagReturnImmediately + wbemFlagForwardOnly)
 
DevConCopy 'no need to repeat per loop?
 
Dim objItem      
Do 
  For Each objItem In colItems
    WScript.Echo "DeviceID: " & objItem.DeviceID
    WScript.Echo "VolumeName: " & objItem.VolumeName
    FormatDrive (objItem.DeviceID)
  Next
 
  ' Asks the user if they want to copy another drive or exit the script.
  WScript.StdOut.Write "Do you want to copy another drive? (y/n)"
  choice = WScript.StdIn.ReadLine
Loop While choice = "y" Or choice = "Y"
 
Sub FormatDrive (strDrive)  ' Format Drive
  strCommand = "cmd /c format " & strDrive & " /FS:NTFS /Q"
  WScript.Echo "Formatting USB Drive..."
  objShell.Run strCommand
End Sub
 
Sub DevConCopy()  ' Copy Microsoft DevCon utility
  strCommand = "cmd /c xcopy P:\Software\Miscellaneous\devcon.exe c:\windows\system32 /y"
  WScript.Echo "Copying needed utility..."
  objShell.Run strCommand
End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:jleleux
ID: 31567615
Thanks all for helping a noob scripter!!!!  I really appreciate!!!!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24090945
Glad I could lend a hand - thanx for the grade! :^)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

719 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