Scripting School: Taking inventory of drives

Save your script output to a file, and then use it to take inventory of the drives in use or to help administrators keep track of a heavily-used volume. For more details, read Christa's latest column on scripting.

Editor's note: This is the 10th column in a continuing interactive series on scripting that appears monthly on SearchWinSystems.com. You can send any scripting questions to the author, Christa Anderson, at scripting@searchwinsystems.com.


A suggested use for saving script output to a file is to use it to take inventory -- for instance, of drives in use. Possible applications of this script could be to help users inventory their PC drives for total size and available size or to help administrators keep track of a heavily used volume such as the one that stores e-mail or system images.

To do this, we'll employ the File System Object (FSO) mentioned in my December column, Writing output to a text file. However, this time we will use another aspect of it: the Drive Object. The Drive Object has the following properties:

  • Identifying information: the drive letter, serial number, local volume name and share name (if applicable);
  • Drive type (i.e., fixed, removable, network);
  • The file system;
  • Total and available space on the drive (with or without quotas);
  • Total space and free space remaining (or available space, if quotas are in effect on that drive);
  • Whether the drive is ready (for removable drives); and
  • The drive's path and root folder.

In this example, we will get the drive's volume name and serial number so we can identify it as well as its total and available space.

Creating an inventory of the drive
You can't just connect to the Drive Object. First, you must connect to the FSO (which controls all parts of the file system from drives and folders to single files). Once you've done that, then you need to connect to the drive itself. Assuming that sDriveletter has been assigned the value of the drive letter supplied by the person running the script, the code for accomplishing this is:

 Set fso = CreateObject("Scripting.FileSystemObject") Set drive = fso.GetDrive(fso.GetDriveName(sDriveletter))

In this example, I shortened my code by incorporating two steps into the second step: I'm getting the drive name and then passing that information to the GetDrive function so I can connect to the drive. I could have performed these two steps individually. If you prefer to do that, just perform the method in the parentheses first. I've stored this procedure in a user-defined function called GetDrive that looks like this:

 Function GetDrive(sdriveletter) Set odrive = oFSO.GetDrive(oFSO.GetDriveName(sDriveletter)) End Function

This function will not execute until I call it using Call GetDrive(sDriveletter), stating that that function should execute for the value stored in the sDriveletter variable. Once you identify the object representing the drive, the rest is easy: You're making the script spit out the contents of that drive's properties. In this example, we want to know the drive's total size and available space (since the AvailableSize property will provide accurate information regarding whether quotas on that volume apply) and its volume name and serial number. This information is stored in the TotalSize, AvailableSize, VolumeName and SerialNumber properties of the Drive Object.

Saving inventory to a text file
Echoing those drive properties means that they vanish as soon as you close the command window in which you ran the script. This is the kind of inventory information that you will probably want to save.

As discussed in my previous column, writing inventory to a file is a matter of creating an instance of the FSO, then creating a text file object from that FSO at the path you provide in the script. (I'd recommend hard-coding that path into the script so that you always know where to find that inventory, rather than letting users provide the path as an argument to the script. Note: Be careful if you save all inventories to the same network path for ease of backup. You'll want to refer to the previous column to learn how to prevent them from overwriting other files with the same name.)

Since we're working with the FSO to get the drive inventory in the first place, you don't need to repeat that step. Once you've created an FSO, it's with you for the duration of the script. In this column's sample script, I let the core script creating the text file connect to the FSO. I also let the function that gets the drive information use that FSO. Be sure to use the same variables for the FSO in all functions calling it, as well as in the main body of the script. Also, define the variables for FSO globally (that is, in the main body of the script) rather than within the body of a function. Just connect to the existing FSO you made to prepare the inventory, and then complete the rest of the steps to create the text file and specify its absolute path name and file name.

Once that's accomplished, you can write to the file using the WriteLine methods described in my previous column. Remember, you can intermix variables and text, like this:

 oFiletxt.WriteLine("The total size of the disk is"&drive.TotalSize

The entire script we've just created looks like this:

 Option Explicit 'Define the variables to be used in this script Dim oFSO, oFiletxt, sFilename, sPath, odrive, odrivepath, sdriveletter 'Define the drive letter according to user input sDriveletter = Wscript.Arguments(0) 'Create the file system object and the file to store inventory in Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFiletxt = oFSO.CreateTextFile("c:driveinventory.txt", True) sPath = oFSO.GetAbsolutePathName("c:driveinventory.txt") sFilename = oFSO.GetFileName(sPath) 'Call the function connecting to the drive Call GetDrive(sDriveletter) 'Write the information in the file oFiletxt.WriteLine("Current Drive Inventory for "&odrive.VolumeName) oFiletxt.WriteLine("Total Drive Volume (in gigabytes):"&odrive.TotalSize/1073741824) oFiletxt.WriteLine("Available Space (in gigabytes):"&odrive.AvailableSpace/1073741824) oFiletxt.WriteLine("Drive Serial Number:"&odrive.SerialNumber) oFiletxt.Close If oFSO.FileExists(sPath) Then Wscript.Echo "The drive inventory is stored in",sFilename&"." This function gets the drive identified by the user Function GetDrive(sdriveletter) Set odrive = oFSO.GetDrive(oFSO.GetDriveName(sDriveletter)) End Function

To summarize, you can't actually do anything to a drive using the Drive Object; this object has no methods that would allow you to create drive volumes, mount drives or format them. This object is for exposing information about the specified drive. That said, in combination with recording the results in a text file, the Drive Object gives you an easy way to inventory drives so your company knows what its client and server computers look like over time.

Read all of Christa's scripting columns:
April 2005: Beginner's guide to scripting
May 2005: It's time to increase your scripting expertise
June 2005: Connect users to network resources
July 2005: More on connecting to network resources
August 2005: Find objects with Windows Scripting Host
September 2005: Windows Script Host arguments
October 2005: Scripting School: Turning the environment with WshShell
November 2005: Scripting School: Connect scripts to remote computers
December 2005: Scripting School: Writing output to a text file

ABOUT THE AUTHOR:
When Christa Anderson began working with Windows Server operating systems in 1992, she became increasingly interested in finding more efficient and flexible ways of performing routine tasks. Christa has written extensively about administrative scripting and taught technical sessions on the subject at conferences such as Comdex and CeBIT, helping people who had never done any scripting to write their own scripts in half a day. In addition to her interest in scripting Windows management, Christa is an authority on server-based computing and the program manager for Terminal Services licensing in Longhorn. If you have a scripting question for Christa, please e-mail her at scripting@SearchWinSystems.com.

Dig deeper on Windows File Management

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close