Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB Script help!!

Posted on 2013-11-25
7
Medium Priority
?
458 Views
Last Modified: 2014-01-02
I'm trying to utilize a script I found on a forum located here on this page: Forum Link

Long story short, the script is ran on a computer running a XP Mode VM. I need my USB devices to attach to the VM via this script.

I have hopefully a pretty easy question, I don't understand where to input my customized variables. The script asks me to put in my computer name, and my USB name as it is shown on the VM. I have that information here below.


Here are my variables:

Computer Name:
VirtualXP-76893

USB Name:
Medical Graphics CPFS USB



Where do I input this information into the script?

'USB devices to a Virtual Machine.
'Usage: cscript ScriptName <vmname> <Device String>
'Validate the Parameters
If WScript.Arguments.Count <> 2 Then
Wscript.Echo "Usage: cscript ScriptName <vmname> <Device Name>"
WScript.Quit 1
End If
'Get the Virtual PC Object
Set objVPC = CreateObject("VirtualPC.Application")
'Find the Virtual Machine
Set objVM = objVPC.FindVirtualMachine(WScript.Arguments(0))
If objVM is Nothing Then
Wscript.Echo "Unable to Find the Virtual Machine: " & WScript.Arguments(0)
WScript.Quit 2
End If
'Get the USB devices collection
set usbDevicesColl = objVPC.USBDeviceCollection
If usbDevicesColl.Count = 0 Then
Wscript.Echo "There are no USB Devices Connected to the machine."
WScript.Quit 2
End If
'Display the Currently Connected USB Devices
'Wscript.Echo "USB Devices Connected to the machine"
For Each usbDevice in usbDevicesColl
'Wscript.Echo "USB Device Name: " & usbDevice.DeviceString
Next
on error resume next
'Variable to find if Device is Found
isFound = 0
For Each usbDevice in usbDevicesColl
if LCase(usbDevice.DeviceString) = LCase(WScript.Arguments(1)) Then
'Attach the USB device to VM
set objAssign = objVM.AttachUSBDevice(usbDevice) 
isFound = 1
break
End If
Next

If isFound = 0 Then
Wscript.Echo "Unable to find the USB device. Please check the device string."
Else
'Wscript.Echo "The Device is assigned to the Virtual Machine."
End If

Open in new window



The only explaination by the author is this:

Feel free to customize the code, I actually borrowed this and tweaked it a little from another forum; unfortunately I can not find that forum again so... cant post a link to it to provide that user with credit, however... for the record, it is not my source code, I have just modified it by commenting out some MSG boxes to provide a more seamless attachment of the usb devices.
To make this code work, you are running the script with two parameters, 1: The name of the Virtual Machine, 2: The name of the USB device to attach as it shows within the VM session. Both of these parameters should be passed as delimited strings. Example: "Windows XP Mode" "Cardjet Printer"
Example of the entire command: C:\path\script.vbs "Windows XP Mode" "Cardjet Printer"
I would recommend setting this up in a similar way that I do right now. I have the script located on a completely open share for the enterprise, and then have a created task on the machine hosting the VM. This task is set to run whenever event 213 from Virtual PC is triggered, it then runs the script with the specified arguments/parameters. I have a separate action created for each usb device that needs to be attached. Remember that these device names are the exact names specified within the session, so... if you havent installed the drivers yet, dont setup the script until after that has been done, since the proper name of the device will only show once the drivers have been installed (until then, it should be listed as unspecified, or unknown device, which is useless).
Until Microsoft incorporates this as a standard integrated feature, this I believe is the best method for attaching USB devices to Windows XP Mode sessions.
Thanks, and I hope this helps.
Scott

Thanks in advance
0
Comment
Question by:Pancake_Effect
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 800 total points
ID: 39675174
You don't need to put those variables in the script you call the script with parameters like :

C:\path\script.vbs "VirtualXP-76893" "Medical Graphics CPFS USB"

obviously the path of the location of the script will change depending on where you run it from.
0
 
LVL 13

Assisted Solution

by:Surone1
Surone1 earned 1200 total points
ID: 39675198
if you do want to modify the script, replace WScript.Arguments(0) with "VirtualXP-76893" and WScript.Arguments(0) with "Medical Graphics CPFS USB" also remove the first bit where it quits, or you will have to still run it with 2 (unused) arguments
0
 
LVL 4

Author Comment

by:Pancake_Effect
ID: 39675450
@jacko72. I think I'm understanding what you are saying. I imagine I just type that into CMD. Do I run the script within the VM or the Physical computer?


@ Surone1
I tried that, but it just errors out. Maybe I'm just taking out the wrong parts and inserting the wrong information. Could you possible show me a demonstration?

I'm sorry I'm actually more of a networker, I'm terrible at coding. haha.

Thanks!
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 13

Assisted Solution

by:Surone1
Surone1 earned 1200 total points
ID: 39675462
'Get the Virtual PC Object
Set objVPC = CreateObject("VirtualPC.Application")
'Find the Virtual Machine
Set objVM = objVPC.FindVirtualMachine("VirtualXP-76893")
If objVM is Nothing Then
Wscript.Echo "Unable to Find the Virtual Machine: " & "VirtualXP-76893"
WScript.Quit 2
End If
'Get the USB devices collection
set usbDevicesColl = objVPC.USBDeviceCollection
If usbDevicesColl.Count = 0 Then
Wscript.Echo "There are no USB Devices Connected to the machine."
WScript.Quit 2
End If
'Display the Currently Connected USB Devices
'Wscript.Echo "USB Devices Connected to the machine"
For Each usbDevice in usbDevicesColl
'Wscript.Echo "USB Device Name: " & usbDevice.DeviceString
Next
on error resume next
'Variable to find if Device is Found
isFound = 0
For Each usbDevice in usbDevicesColl
if LCase(usbDevice.DeviceString) = LCase("Medical Graphics CPFS USB") Then
'Attach the USB device to VM
set objAssign = objVM.AttachUSBDevice(usbDevice)
isFound = 1
break
End If
Next

If isFound = 0 Then
Wscript.Echo "Unable to find the USB device. Please check the device string."
Else
'Wscript.Echo "The Device is assigned to the Virtual Machine."
End If
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 800 total points
ID: 39675564
On the host i.e. the physical computer
0
 
LVL 4

Author Comment

by:Pancake_Effect
ID: 39675744
Awesome! Got it going, both worked, however I stuck with Surone1's code just to make it work out easier.

I have one last question, I want to add a part in the script that launches my shortcut to the VM. Can someone add this part of the code for me?

After googling it, it looks like this is the code:


Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""c:\Program Files\Mozilla Firefox\firefox.exe""")
Set objShell = Nothing


I however instead of firefox, the target actually points to my application.

I imagine if this is put on top of the code it would launch the program, maybe make it wait 15 seconds, and it would run the USB code.
0
 
LVL 13

Assisted Solution

by:Surone1
Surone1 earned 1200 total points
ID: 39675799
code to wait for 15 seconds coul be something like this:

endtime = DateAdd("s", 15, Now) 'set endtime
Do While endtime > Now 'start 15 second loop
DoEvents 'prevent my form  from freezing *note* not sure if it exists in vbscript
Loop
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Originally published Entrepreneur.com Booming numbers of freelancing professionals are changing the face of work. In the United States alone last year, the number of workers freelancing grew from 700,000 to 54 million, according to a Freelancers’…
This article is written by John Gates, CISSP. Gates, the SNUG President-Elect, currently holds the position of Manager of Information Systems at Lake Park High School in Roselle, Illinois.
The Bounty Board allows you to request an article or video on any technical topic, or fulfill a bounty request to earn points. Watch this video to learn how to use the Bounty Board to get the content you want, earn points, and browse submitted bount…
Where to go on the main page to find the job listings. How to apply to a job that you are interested in from the list that is featured on our Careers page.

660 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