Identifying Drives and Drive Types

Microsoft® Windows® 2000 Scripting Guide

Enterprise scripting often involves configuring hardware and software on remote computers; in turn, this requires you to know, in advance, the type of disk drives installed on a computer. For example, a script that installs an application on drive E works only if drive E is a hard disk. If drive E happens to represent a floppy disk or a CD-ROM drive, the script fails.

Fortunately, you can use the Win32_LogicalDisk class to verify the drive type for each disk drive installed on a computer.

Scripting Steps

Listing 10.4 contains a script that identifies the drives and drive types installed on a computer. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  3. Use the ExecQuery method to query the Win32_LogicalDisk class.

    This query returns a collection consisting of all the logical disk drives (including hard disks, floppy disks, and compact discs) installed on the computer.

  4. For each logical disk drive in the collection, echo the DeviceID (the drive letter) and the drive type.

    The Win32_LogicalDisk class returns the value of the DriveType property as an integer. Therefore, a series of Select Case statements is used to convert the value to a text string. For example, if the DriveType is equal to 3, the string "Local hard disk" is echoed to the screen.

Listing 10.4 Identifying Drives and Drive Types

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_LogicalDisk")
For Each objDisk in colDisks
 Wscript.Echo "DeviceID: "& objDisk.DeviceID 
 Select Case objDisk.DriveType
 Case 1
 Wscript.Echo "No root directory."
 Case 2
 Wscript.Echo "DriveType: Removable drive."
 Case 3
 Wscript.Echo "DriveType: Local hard disk."
 Case 4
 Wscript.Echo "DriveType: Network disk." 
 Case 5
 Wscript.Echo "DriveType: Compact disk." 
 Case 6
 Wscript.Echo "DriveType: RAM disk." 
 Case Else
 Wscript.Echo "Drive type could not be determined."
 End Select
Next