October 27th, 2005

How Can I Determine Which Drives are Mapped to Network Shares?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine which drives on a computer are mapped to network shares?

— CP

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CP. OK, you better sit down, because what we’re about to say will shock you: if you want to determine which drives on a computer are mapped to network shares, don’t use the Win32_MappedLogicalDisk class.

Oh, look: you spilled coffee on everything. Didn’t we tell you to sit down? Here, here’s some paper towels.

While you clean up – don’t forget that little puddle by the phone – we’ll explain why. The truth is, you can use the WMI class Win32_MappedLogicalDisk to determine which drives are mapped to network shares. The problem, however, is that while this class will return the drive letters for these drives, it won’t tell you which shares the drives are mapped to. Weird, but true.

And so it’s Win32_LogicalDisk to the rescue. With this class not only can we determine which drives are mapped to network shares, but we can also determine which network shares they map to. And to do that requires nothing more than a script like this:

strComputer = “.”

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

Set colDrives = objWMIService.ExecQuery _ (“Select * From Win32_LogicalDisk Where DriveType = 4”)

For Each objDrive in colDrives Wscript.Echo “Drive letter: ” & objDrive.DeviceID Wscript.Echo “Network path: ” & objDrive.ProviderName Next

Stop us if you’ve heard this one before, but the script starts out by connecting to the WMI service on the local computer. (As usual, you can modify this script to run against a remote computer simply by assigning the name of that machine to the variable strComputer.) We then use this line of code to return a collection of all the mapped network drives:

Set colDrives = objWMIService.ExecQuery _
    (“Select * From Win32_LogicalDisk Where DriveType = 4”)

The key here – as you might have guessed – lies in our Where clause. We’re asking for all instances of the class where the DriveType is equal to 4; needless to say, a DriveType equal to 4 represents a mapped network drive. (For other DriveType values, see the WMI SDK on MSDN.) The query returns a collection of all the mapped drives; we then set up a For Each loop to walk through that collection. For each mapped drive we echo the value of two properties: DeviceID, which returns the drive letter for the drive; and ProviderName, which returns the network share the drive is mapped to.

In other words, we get back information similar to this:

Drive letter: E:
Network path: \\atl-fs-01\public
Drive letter: F:
Network path: \\atl-fs-01\finance
Drive letter: G:
Network path: \\atl-fs-01\users\kenmyer

That’s OK, we understand: as useful as this script is, it still comes as a bit of a shock that we didn’t use the Win32_MappedLogicalDisk class. Listen, this has been kind if rough on you: why don’t you take the rest of the day off? Just tell your boss the Scripting Guys said it was OK.

Legal disclaimer. On the advice of our attorneys (and, trust us, the Scripting Guys need their own attorneys), we did not say it was OK. (But if you want to try it anyway, well, good luck.)

Author

0 comments

Discussion are closed.