Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I tell whether or not a computer has a CD named MyApps in it?

-- CB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CB. You know, this is one of those questions that the Scripting Guys almost feel bad about answering. Why? Well, at first glance this sounds like it might be a really complicated problem to solve; as it turns out, however, it’s pretty easy, so easy that we almost feel that it’s unfair for us to write a column on a topic that elementary. After all, we’re the Scripting Guys: we should be taking on nothing but the toughest and most-demanding of challenges. The Scripting Guys take the easy way out? Never!

Like we said, though, we almost feel bad about answering it:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_CDROMDrive Where VolumeName = 'MyApps'")

Wscript.Echo colItems.Count

The reason this question is so easy to answer is because the WMI class Win32_CDROMDrive includes a property named VolumeName. If a CD happens to be in a CD drive then the VolumeName property will be equal to the name of that CD (or at least the name of the CD as it appears in Windows Explorer). If we want to know if a CD named MyApps happens to be loitering in a computer all we have to do is count up the number of CDs on that computer that have a VolumeName equal to MyApps. If the number is 0, then the CD is not inserted on any of the drives on that computer. If the number is greater than 0 then a CD by that name is inserted in the computer.

That pretty much sums up everything this script does. We start out by binding to the WMI service on the local computer. It goes without saying that you could perform this same task on a remote machine; in fact, that’s where the script really comes in handy. (If you want to know if a CD is inserted in your local machine it’s probably easier to just open the drive and take a peek.) Even better, suppose this CD might have been left in any of one of a hundred different computers. In that case you might want to paste this code into one of our multi-computer script templates and search each of those 100 or so machines. For more information on doing that see, well, our multi-computer script templates page.

OK. After connecting to the WMI service we then use this line of code to retrieve a collection of all the CD drives that have a VolumeName equal to MyApps (again, the VolumeName is the name of the CD as it appears in Windows Explorer):

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_CDROMDrive Where VolumeName = 'MyApps'")

At that point all we have to do is echo back the Count, which tells us the number of MyApps CDs found on the computer.

Boy, we did end a little bit early today, didn’t we? OK, how about this? Seeing as how we mentioned the multi-computer templates, here’s a revised script that checks for the MyApps CD on all the computers listed in a text file named Servers.txt:

On Error Resume Next

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\scripts\servers.txt", ForReading)

Do Until objTextFile.AtEndOfStream 
    strComputer = objTextFile.Readline

    ' =====================================================================
    ' Insert your code here
    ' =====================================================================

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colItems = objWMIService.ExecQuery _
        ("Select * From Win32_CDROMDrive Where VolumeName = 'MyApps'")

    Wscript.Echo strComputer, colItems.Count

    ' =====================================================================
    ' End
    ' =====================================================================

Loop

objTextFile.Close

As you can see, all we had to do was insert our basic code (slightly revised, in that we echo back the computer name as well as the number of CDs found) into the section of the template labeled Insert your code here. The script will then go out and search for the CD on every computer listed in the text file.

Could anything be easier? Probably not. Which means we almost feel like we should take on a harder question next time out. Almost.