Scripts to manage Disk Drives and Volumes
Adding a Volume Mount PointAnalyzing Volume Defragmentation
Associating Disk Partitions with Physical Disk Drives
Binding to a Specific Disk Drive
Changing the Drive Letter of a Volume
Changing Volume Names
Defragmenting a Volume
Dismounting a Volume
Ensuring that All Drives are Ready
Enumerating Available Disk Space
Enumerating CD-ROM Properties
Enumerating Disk Drive Properties Using FSO
Enumerating Disk Partition Properties
Enumerating Disk Space by User on the Local Computer
Enumerating Floppy Drives
Enumerating Logical Disk Drive Properties
Enumerating Physical Disk Properties
Enumerating Volume Mount Points
Enumerating Volume Properties
Formatting a Volume
Identifying Drive Types
Modifying the AutoChk Timeout Value
Monitoring Logical Disk Drive Performance
Monitoring Physical Disk Drive Performance
Monitoring Volume Change Events
Mounting a Volume
Preventing AutoChk From Running
Retrieving Floppy Controller Information
Retrieving IDE Controller Information
Retrieving Information About Mapped Network Drives
Retrieving SCSI Controller Information
Retrieving USB Hub Information
Returning a Disk Drive Collection
Retrieving USB Controller Information
Running ChkDsk
Scheduling AutoChk
Verifying ChkDsk Volume Status
Adding a Volume Mount Point
Adds a new volume mount point for the folder W:\Scripts. Note that in the WQL query, you must use two forward slashes rather than one, and you must also include two forward slashes at the end of the folder path. Thus the folder X:\Scripts\WMI would be listed as X:\\Scripts\\WMI\\.
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery _ ("Select * From Win32_Volume Where Name = 'D:\\'") For Each objItem in colItems objItem.AddMountPoint("W:\\Scripts\\") Next
Analyzing Volume Defragmentation
Analyzes the defragmentation status of all the volumes on a computer. This is equivalent to running defrag.exe with the command-line options -a (analyze) and -v (verbose).
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colVolumes = objWMIService.ExecQuery("Select * from Win32_Volume") For Each objVolume in colVolumes errResult = objVolume.DefragAnalysis(blnRecommended, objReport) If errResult = 0 then Wscript.Echo "Average file size: " & objReport.AverageFileSize Wscript.Echo "Average fragments per file: " & _ objReport.AverageFragmentsPerFile Wscript.Echo "Cluster size: " & objReport.ClusterSize Wscript.Echo "Excess folder fragments: " & _ objReport.ExcessFolderFragments Wscript.Echo "File percent fragmentation: " & _ objReport.FilePercentFragmentation Wscript.Echo "Fragmented folders: " & objReport.FragmentedFolders Wscript.Echo "Free soace: " & objReport.FreeSpace Wscript.Echo "Free space percent: " & objReport.FreeSpacePercent Wscript.Echo "Free space percent fragmentation: " & _ objReport.FreeSpacePercentFragmentation Wscript.Echo "MFT percent in use: " & objReport.MFTPercentInUse Wscript.Echo "MFT record count: " & objReport.MFTRecordCount Wscript.Echo "Page file size: " & objReport.PageFileSize Wscript.Echo "Total excess fragments: " & _ objReport.TotalExcessFragments Wscript.Echo "Total files: " & objReport.TotalFiles Wscript.Echo "Total folders: " & objReport.TotalFolders Wscript.Echo "Total fragmented files: " & _ objReport.TotalFragmentedFiles Wscript.Echo "Total MFT fragments: " & objReport.TotalMFTFragments Wscript.Echo "Total MFT size: " & objReport.TotalMFTSize Wscript.Echo "Total page file fragments: " & _ objReport.TotalPageFileFragments Wscript.Echo "Total percent fragmentation: " & _ objReport.TotalPercentFragmentation Wscript.Echo "Used space: " & objReport.UsedSpace Wscript.Echo "Volume name: " & objReport.VolumeName Wscript.Echo "Volume size: " & objReport.VolumeSize If blnRecommended = True Then Wscript.Echo "This volume should be defragged." Else Wscript.Echo "This volume does not need to be defragged." End If Wscript.Echo End If Next
Associating Disk Partitions with Physical Disk Drives
Returns the DeviceID of all the physical disk drives installed on a computer, and indicates the partitions that have been created on those disks.
ComputerName = "." Set wmiServices = GetObject _ ("winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName) Set wmiDiskDrives = wmiServices.ExecQuery _ ("SELECT Caption, DeviceID FROM Win32_DiskDrive") For Each wmiDiskDrive In wmiDiskDrives WScript.Echo wmiDiskDrive.Caption & " (" & wmiDiskDrive.DeviceID & ")" strEscapedDeviceID = _ Replace(wmiDiskDrive.DeviceID, "\", "\\", 1, -1, vbTextCompare) Set wmiDiskPartitions = wmiServices.ExecQuery _ ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & _ strEscapedDeviceID & """} WHERE " & _ "AssocClass = Win32_DiskDriveToDiskPartition") For Each wmiDiskPartition In wmiDiskPartitions WScript.Echo vbTab & wmiDiskPartition.DeviceID Set wmiLogicalDisks = wmiServices.ExecQuery _ ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _ wmiDiskPartition.DeviceID & """} WHERE " & _ "AssocClass = Win32_LogicalDiskToPartition") For Each wmiLogicalDisk In wmiLogicalDisks WScript.Echo vbTab & vbTab & wmiLogicalDisk.DeviceID Next Next Next
Binding to a Specific Disk Drive
Demonstration script that uses the FileSystemObject to return available disk space on a specific disk drive. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject") Set objDrive = objFSO.GetDrive("C:") Wscript.Echo "Available space: " & objDrive.AvailableSpace
Changing the Drive Letter of a Volume
Changes the drive letter of volume D to Q. If you modify this script to change the drive letter of a volume other than D, note that the volume name in the WQL query must include both the colon (:) and two forward slashes (\\). Thus drive C would look like this: C:\\. When specifying the new drive letter, however, you only have to include the colon (in the sample script, Q:).
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colVolumes = objWMIService.ExecQuery _ ("Select * from Win32_Volume Where Name = 'D:\\'") For Each objVolume in colVolumes objVolume.DriveLetter = "Q:" objVolume.Put_ Next
Changing Volume Names
Changes the volume label for drive C to "Finance Volume."
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colDrives = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk where DeviceID = 'C:'") For Each objDrive in colDrives objDrive.VolumeName = "Finance Volume" objDrive.Put_ Next
Defragmenting a Volume
Defragments volume D on a computer. If you modify this script to defragment a different volume (such as X), note that your WQL query must specify the drive letter followed by a colon and then followed by two forward slashes. Thus volume X would be listed as X:\\.
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colVolumes = objWMIService.ExecQuery _ ("Select * from Win32_Volume Where Name = 'D:\\'") For Each objVolume in colVolumes errResult = objVolume.Defrag() Next
Dismounting a Volume
Dismounts volume E from the file system. If you modify this script to dismount a different volume (such as X), note that your WQL query must specify the drive letter followed by a colon and then followed by two backslashes. Thus volume X would be listed as X:\\. The two parameters: 1) force the volume to be dismounted, even if users are currently connected to it; and, 2) place the volume in a no-automount, offline state. This script can be modified by setting either (or both) of these parameters to False.
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery _ ("Select * From Win32_Volume Where Name = 'E:\\'") For Each objItem in colItems objItem.Dismount(True, True) Next
Ensuring that All Drives are Ready
Demonstration script that uses the FileSystemObject to ensure that all drives are ready (i.e., media is inserted) before echoing the drive letter and available disk space. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject") Set colDrives = objFSO.Drives For Each objDrive in colDrives If objDrive.IsReady = True Then Wscript.Echo "Drive letter: " & objDrive.DriveLetter Wscript.Echo "Free space: " & objDrive.FreeSpace Else Wscript.Echo "Drive letter: " & objDrive.DriveLetter End If Next
Enumerating Available Disk Space
Returns the amount of free space for each hard disk installed on a computer.
Const HARD_DISK = 3 strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colDisks = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "") For Each objDisk in colDisks Wscript.Echo "DeviceID: "& vbTab & objDisk.DeviceID Wscript.Echo "Free Disk Space: "& vbTab & objDisk.FreeSpace Next
Enumerating CD-ROM Properties
Returns information about all the CD-ROM drives installed on a computer.
On Error Resume Next strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_CDROMDrive") For Each objItem in colItems Wscript.Echo "Availability: " & objItem.Availability For Each strCapability in objItem.Capabilities Wscript.Echo "Capability: "& strCapability Next Wscript.Echo "Caption: " & objItem.Caption Wscript.Echo "Description: " & objItem.Description Wscript.Echo "Device ID: " & objItem.DeviceID Wscript.Echo "Drive: " & objItem.Drive Wscript.Echo "Manufacturer: " & objItem.Manufacturer Wscript.Echo "Media Loaded: " & objItem.MediaLoaded Wscript.Echo "Media Type: " & objItem.MediaType Wscript.Echo "Name: " & objItem.Name Wscript.Echo "PNP Device ID: " & objItem.PNPDeviceID Wscript.Echo "SCSI Bus: " & objItem.SCSIBus Wscript.Echo "SCSI Logical Unit: " & objItem.SCSILogicalUnit Wscript.Echo "SCSI Port: " & objItem.SCSIPort Wscript.Echo "SCSI Target ID: " & objItem.SCSITargetId Next
Enumerating Disk Drive Properties Using FSO
Demonstration script that uses the FileSystemObject to return the properties of all the disk drives installed on a computer. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject") Set colDrives = objFSO.Drives For Each objDrive in colDrives Wscript.Echo "Available space: " & objDrive.AvailableSpace Wscript.Echo "Drive letter: " & objDrive.DriveLetter Wscript.Echo "Drive type: " & objDrive.DriveType Wscript.Echo "File system: " & objDrive.FileSystem Wscript.Echo "Free space: " & objDrive.FreeSpace Wscript.Echo "Is ready: " & objDrive.IsReady Wscript.Echo "Path: " & objDrive.Path Wscript.Echo "Root folder: " & objDrive.RootFolder Wscript.Echo "Serial number: " & objDrive.SerialNumber Wscript.Echo "Share name: " & objDrive.ShareName Wscript.Echo "Total size: " & objDrive.TotalSize Wscript.Echo "Volume name: " & objDrive.VolumeName Next
Enumerating Disk Partition Properties
Lists the properties for all the disk partitions on a computer.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colDiskPartitions = objWMIService.ExecQuery _ ("Select * from Win32_DiskPartition") For each objPartition in colDiskPartitions Wscript.Echo "Block Size: " & vbTab & objPartition.BlockSize Wscript.Echo "Bootable: " & vbTab & objPartition.Bootable Wscript.Echo "Boot Partition: " & vbTab & objPartition.BootPartition Wscript.Echo "Description: " & vbTab & objPartition.Description Wscript.Echo "Device ID: " & vbTab & objPartition.DeviceID Wscript.Echo "Disk Index: " & vbTab & objPartition.DiskIndex Wscript.Echo "Index: " & vbTab & objPartition.Index Wscript.Echo "Name: " & vbTab & objPartition.Name Wscript.Echo "Number Of Blocks: " & vbTab & _ objPartition.NumberOfBlocks Wscript.Echo "Primary Partition: " & vbTab & _ objPartition.PrimaryPartition Wscript.Echo "Size: " & vbTab & objPartition.Size Wscript.Echo "Starting Offset: " & vbTab & _ objPartition.StartingOffset Wscript.Echo "Type: " & vbTab & objPartition.Type Next
Enumerating Disk Space by User on the Local Computer
Uses disk quotas to enumerate the amount of disk space currently used by individual users for drive C on the local computer. Requires Windows 2000 or later, and cannot be run against a remote computer (unless you use the WSHController object). To enumerate disk space usage for a different drive, change C:\ in line two to the appropriate drive letter (for example, D:\). Disk quotas must be enabled for this script to work.
Set colDiskQuotas = CreateObject("Microsoft.DiskQuota.1") colDiskQuotas.Initialize "C:\", True For Each objUser in colDiskQuotas Wscript.Echo "Logon name: " & objUser.LogonName Wscript.Echo "Quota used: " & objUser.QuotaUsed Next
Enumerating Floppy Drives
Returns information about all the floppy disk drives installed on a computer.
On Error Resume Next strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_FloppyDrive") For Each objItem in colItems Wscript.Echo "Availability: " & objItem.Availability Wscript.Echo "Configuration Manager Error Code: " & _ objItem.ConfigManagerErrorCode Wscript.Echo "Configuration Manager User Configuration: " & _ objItem.ConfigManagerUserConfig Wscript.Echo "Device ID: " & objItem.DeviceID Wscript.Echo "Manufacturer: " & objItem.Manufacturer Wscript.Echo "Name: " & objItem.Name Wscript.Echo "PNP Device ID: " & objItem.PNPDeviceID Wscript.Echo Next
Enumerating Logical Disk Drive Properties
Lists the properties for all the logical disk drives on a computer. Includes mapped network drives.
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 "Compressed: " & vbTab & objDisk.Compressed Wscript.Echo "Description: " & vbTab & objDisk.Description Wscript.Echo "DeviceID: " & vbTab & objDisk.DeviceID Wscript.Echo "DriveType: " & vbTab & objDisk.DriveType Wscript.Echo "FileSystem: " & vbTab & objDisk.FileSystem Wscript.Echo "FreeSpace: " & vbTab & objDisk.FreeSpace Wscript.Echo "MediaType: " & vbTab & objDisk.MediaType Wscript.Echo "Name: " & vbTab & objDisk.Name Wscript.Echo "QuotasDisabled: " & vbTab & objDisk.QuotasDisabled Wscript.Echo "QuotasIncomplete: " & vbTab & objDisk.QuotasIncomplete Wscript.Echo "QuotasRebuilding: " & vbTab & objDisk.QuotasRebuilding Wscript.Echo "Size: " & vbTab & objDisk.Size Wscript.Echo "SupportsDiskQuotas: " & vbTab & _ objDisk.SupportsDiskQuotas Wscript.Echo "SupportsFileBasedCompression: " & vbTab & _ objDisk.SupportsFileBasedCompression Wscript.Echo "SystemName: " & vbTab & objDisk.SystemName Wscript.Echo "VolumeDirty: " & vbTab & objDisk.VolumeDirty Wscript.Echo "VolumeName: " & vbTab & objDisk.VolumeName Wscript.Echo "VolumeSerialNumber: " & vbTab & _ objDisk.VolumeSerialNumber Next
Enumerating Physical Disk Properties
Retrieves the properties for all the physical disk drives installed on a computer.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer _ & "\root\cimv2") Set colDiskDrives = objWMIService.ExecQuery _ ("Select * from Win32_DiskDrive") For each objDiskDrive in colDiskDrives Wscript.Echo "Bytes Per Sector: " & vbTab & _ objDiskDrive.BytesPerSector For i = Lbound(objDiskDrive.Capabilities) to _ Ubound(objDiskDrive.Capabilities) Wscript.Echo "Capabilities: " & vbTab & _ objDiskDrive.Capabilities(i) Next Wscript.Echo "Caption: " & vbTab & objDiskDrive.Caption Wscript.Echo "Device ID: " & vbTab & objDiskDrive.DeviceID Wscript.Echo "Index: " & vbTab & objDiskDrive.Index Wscript.Echo "Interface Type: " & vbTab & objDiskDrive.InterfaceType Wscript.Echo "Manufacturer: " & vbTab & objDiskDrive.Manufacturer Wscript.Echo "Media Loaded: " & vbTab & objDiskDrive.MediaLoaded Wscript.Echo "Media Type: " & vbTab & objDiskDrive.MediaType Wscript.Echo "Model: " & vbTab & objDiskDrive.Model Wscript.Echo "Name: " & vbTab & objDiskDrive.Name Wscript.Echo "Partitions: " & vbTab & objDiskDrive.Partitions Wscript.Echo "PNP DeviceID: " & vbTab & objDiskDrive.PNPDeviceID Wscript.Echo "SCSI Bus: " & vbTab & objDiskDrive.SCSIBus Wscript.Echo "SCSI Logical Unit: " & vbTab & _ objDiskDrive.SCSILogicalUnit Wscript.Echo "SCSI Port: " & vbTab & objDiskDrive.SCSIPort Wscript.Echo "SCSI TargetId: " & vbTab & objDiskDrive.SCSITargetId Wscript.Echo "Sectors Per Track: " & vbTab & _ objDiskDrive.SectorsPerTrack Wscript.Echo "Signature: " & vbTab & objDiskDrive.Signature Wscript.Echo "Size: " & vbTab & objDiskDrive.Size Wscript.Echo "Status: " & vbTab & objDiskDrive.Status Wscript.Echo "Total Cylinders: " & vbTab & _ objDiskDrive.TotalCylinders Wscript.Echo "Total Heads: " & vbTab & objDiskDrive.TotalHeads Wscript.Echo "Total Sectors: " & vbTab & objDiskDrive.TotalSectors Wscript.Echo "Total Tracks: " & vbTab & objDiskDrive.TotalTracks Wscript.Echo "Tracks Per Cylinder: " & vbTab & _ objDiskDrive.TracksPerCylinder Next
Enumerating Volume Mount Points
Enumerates all the volume mount points on a computer.
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery _ ("SELECT * FROM Win32_MountPoint") For Each objItem In colItems WScript.Echo "Directory: " & objItem.Directory WScript.Echo "Volume: " & objItem.Volume WScript.Echo Next
Enumerating Volume Properties
Retrieves the properties of all the volumes installed on a computer.
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_Volume") For Each objItem In colItems WScript.Echo "Automount: " & objItem.Automount WScript.Echo "Block Size: " & objItem.BlockSize WScript.Echo "Capacity: " & objItem.Capacity WScript.Echo "Caption: " & objItem.Caption WScript.Echo "Compressed: " & objItem.Compressed WScript.Echo "Device ID: " & objItem.DeviceID WScript.Echo "Dirty Bit Set: " & objItem.DirtyBitSet WScript.Echo "Drive Letter: " & objItem.DriveLetter WScript.Echo "Drive Type: " & objItem.DriveType WScript.Echo "File System: " & objItem.FileSystem WScript.Echo "Free Space: " & objItem.FreeSpace WScript.Echo "Indexing Enabled: " & objItem.IndexingEnabled WScript.Echo "Label: " & objItem.Label WScript.Echo "Maximum File Name Length: " & objItem.MaximumFileNameLength WScript.Echo "Name: " & objItem.Name WScript.Echo "Quotas Enabled: " & objItem.QuotasEnabled WScript.Echo "Quotas Incomplete: " & objItem.QuotasIncomplete WScript.Echo "Quotas Rebuilding: " & objItem.QuotasRebuilding WScript.Echo "Serial Number: " & objItem.SerialNumber WScript.Echo "Supports Disk Quotas: " & objItem.SupportsDiskQuotas WScript.Echo "Supports File-Based Compression: " & objItem.SupportsFileBasedCompression WScript.Echo Next
Formatting a Volume
Formats drive D using the NTFS file system. Drives could also be formatted using the FAT or FAT32 file systems. Note that this method cannot be used to format floppy drives. If you modify this script to format a different volume (such as drive X), also note that your WQL query must specify the drive letter followed by a colon and then followed by two forward slashes. Thus drive X would be listed as X:\\.
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colVolumes = objWMIService.ExecQuery _ ("Select * from Win32_Volume Where Name = 'D:\\'") For Each objVolume in colVolumes errResult = objVolume.Format("NTFS") Next
Identifying Drive Types
Identifies the drive type (floppy drive, hard drive, CD-ROM, etc.) for each physical drive installed on a computer.
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: "& vbTab & objDisk.DeviceID Select Case objDisk.DriveType Case 1 Wscript.Echo "No root directory. Drive type could not be " _ & "determined." Case 2 Wscript.Echo "DriveType: "& vbTab & "Removable drive." Case 3 Wscript.Echo "DriveType: "& vbTab & "Local hard disk." Case 4 Wscript.Echo "DriveType: "& vbTab & "Network disk." Case 5 Wscript.Echo "DriveType: "& vbTab & "Compact disk." Case 6 Wscript.Echo "DriveType: "& vbTab & "RAM disk." Case Else Wscript.Echo "Drive type could not be determined." End Select Next
Modifying the AutoChk Timeout Value
Sets the auto-delay time for Autochk.exe to 30 seconds.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colAutoChkSettings = objWMIService.ExecQuery _ ("Select * from Win32_AutochkSetting") For Each objAutoChkSetting in colAutoChkSettings objAutoChkSetting.UserInputDelay = 30 objAutoChkSetting.Put_ Next
Monitoring Logical Disk Drive Performance
Uses cooked performance counters to monitor performance of the logical disk drives installed on a computer.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") set objRefresher = CreateObject("WbemScripting.SWbemRefresher") Set colDisks = objRefresher.AddEnum _ (objWMIService, "win32_perfformatteddata_perfdisk_logicaldisk"). _ objectSet objRefresher.Refresh For i = 1 to 100 For Each objDisk in colDisks Wscript.Echo "Average Disk Bytes Per Read: " & vbTab & _ objDisk.AvgDiskBytesPerRead Wscript.Echo "Average Disk Bytes Per Transfer: " & vbTab & _ objDisk.AvgDiskBytesPerTransfer Wscript.Echo "Average Disk Bytes Per Write: " & vbTab & _ objDisk.AvgDiskBytesPerWrite Wscript.Echo "Average Disk Queue Length: " & vbTab & _ objDisk.AvgDiskQueueLength Wscript.Echo "Average Disk Read Queue Length: " & vbTab & _ objDisk.AvgDiskReadQueueLength Wscript.Echo "Average Disk Seconds Per Read: " & vbTab & _ objDisk.AvgDiskSecPerRead Wscript.Echo "Average Disk Seconds Per Transfer: " & vbTab & _ objDisk.AvgDiskSecPerTransfer Wscript.Echo "Average Disk Seconds Per Write: " & vbTab & _ objDisk.AvgDiskSecPerWrite Wscript.Echo "Average Disk Write Queue Length: " & vbTab & _ objDisk.AvgDiskWriteQueueLength Wscript.Echo "Current Disk Queue Length: " & vbTab & _ objDisk.CurrentDiskQueueLength Wscript.Echo "Disk Bytes Per Second: " & vbTab & _ objDisk.DiskBytesPerSec Wscript.Echo "Disk Read Bytes Per Second: " & vbTab & _ objDisk.DiskReadBytesPerSec Wscript.Echo "Disk Reads Per Second: " & vbTab & _ objDisk.DiskReadsPerSec Wscript.Echo "Disk Transfers Per Second: " & vbTab & _ objDisk.DiskTransfersPerSec Wscript.Echo "Disk Write Bytes Per Second: " & vbTab & _ objDisk.DiskWriteBytesPerSec Wscript.Echo "Disk Writes Per Second: " & vbTab & _ objDisk.DiskWritesPerSec Wscript.Echo "Free Megabytes: " & vbTab & objDisk.FreeMegabytes Wscript.Echo "Name: " & vbTab & objDisk.Name Wscript.Echo "Percent Disk Read Time: " & vbTab & _ objDisk.PercentDiskReadTime Wscript.Echo "Percent Disk Time: " & vbTab & _ objDisk.PercentDiskTime Wscript.Echo "Percent Disk Write Time: " & vbTab & _ objDisk.PercentDiskWriteTime Wscript.Echo "Percent Free Space: " & vbTab & _ objDisk.PercentFreeSpace Wscript.Echo "Percent Idle Time: " & vbTab & _ objDisk.PercentIdleTime Wscript.Echo "Split IO Per Second: " & vbTab & _ objDisk.SplitIOPerSec Wscript.Sleep 2000 objRefresher.Refresh Next Next
Monitoring Physical Disk Drive Performance
Uses cooked performance counters to monitor performance of the physical disk drives installed on a computer.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") set objRefresher = CreateObject("WbemScripting.SWbemRefresher") Set colDisks = objRefresher.AddEnum _ (objWMIService, "win32_perfformatteddata_perfdisk_physicaldisk"). _ objectSet objRefresher.Refresh For i = 1 to 100 For Each objDisk in colDisks Wscript.Echo "Average Disk Bytes Per Read: " & vbTab & _ objDisk.AvgDiskBytesPerRead Wscript.Echo "Average Disk Bytes Per Transfer: " & vbTab & _ objDisk.AvgDiskBytesPerTransfer Wscript.Echo "Average Disk Bytes Per Write: " & vbTab & _ objDisk.AvgDiskBytesPerWrite Wscript.Echo "Average Disk Queue Length: " & vbTab & _ objDisk.AvgDiskQueueLength Wscript.Echo "Average Disk Read Queue Length: " & vbTab & _ objDisk.AvgDiskReadQueueLength Wscript.Echo "Average Disk Seconds Per Read: " & vbTab & _ objDisk.AvgDiskSecPerRead Wscript.Echo "Average Disk Seconds Per Transfer: " & vbTab & _ objDisk.AvgDiskSecPerTransfer Wscript.Echo "Average Disk Seconds Per Write: " & vbTab & _ objDisk.AvgDiskSecPerWrite Wscript.Echo "Average Disk Write Queue Length: " & vbTab & _ objDisk.AvgDiskWriteQueueLength Wscript.Echo "Current Disk Queue Length: " & vbTab & _ objDisk.CurrentDiskQueueLength Wscript.Echo "Disk Bytes Per Second: " & vbTab & _ objDisk.DiskBytesPerSec Wscript.Echo "Disk Read Bytes Per Second: " & vbTab & _ objDisk.DiskReadBytesPerSec Wscript.Echo "Disk Reads Per Second: " & vbTab & _ objDisk.DiskReadsPerSec Wscript.Echo "Disk Transfers Per Second: " & vbTab & _ objDisk.DiskTransfersPerSec Wscript.Echo "Disk Write Bytes Per Second: " & vbTab & _ objDisk.DiskWriteBytesPerSec Wscript.Echo "Disk Writes Per Second: " & vbTab & _ objDisk.DiskWritesPerSec Wscript.Echo "Name: " & vbTab & objDisk.Name Wscript.Echo "Percent Disk Read Time: " & vbTab & _ objDisk.PercentDiskReadTime Wscript.Echo "Percent Disk Time: " & vbTab & _ objDisk.PercentDiskTime Wscript.Echo "Percent Disk Write Time: " & vbTab & _ objDisk.PercentDiskWriteTime Wscript.Echo "Percent Idle Time: " & vbTab & _ objDisk.PercentIdleTime Wscript.Echo "Split IO Per Second: " & vbTab & _ objDisk.SplitIOPerSec Wscript.Sleep 2000 objRefresher.Refresh Next Next
Monitoring Volume Change Events
Temporary event consumer that issues an alert when local volumes are added to or deleted from a computer. (This class monitors only changes to local drives; it cannot detect the addition/deletion of network volumes.)
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colMonitoredEvents = objWMIService. _ ExecNotificationQuery("Select * from Win32_VolumeChangeEvent") Do Set objLatestEvent = colMonitoredEvents.NextEvent Wscript.Echo objLatestEvent.DriveName Wscript.Echo objLatestEvent.EventType Wscript.Echo objLatestEvent.Time_Created Loop
Mounting a Volume
Mounts volume Z to the file system. If you modify this script to mount a different volume (such as X), note that the volume name in your WQL query must include the drive letter followed by a colon and then followed by two forward slashes. Thus drive X would be listed as X:\\.
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery _ ("Select * From Win32_Volume Where Name = 'Z:\\'") For Each objItem in colItems objItem.Mount() Next
Preventing AutoChk From Running
Ensures that Autochk.exe will not run against drive C the next time the computer reboots, even if the "dirty bit" has been set on drive C.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set objDisk = objWMIService.Get("Win32_LogicalDisk") errReturn = objDisk.ExcludeFromAutoChk(Array("C:")) Wscript.Echo errReturn
Retrieving Floppy Controller Information
Defragments volume D on a computer. If you modify this script to defragment a different volume (such as X), note that your WQL query must specify the drive letter followed by a colon and then followed by two forward slashes. Thus volume X would be listed as X:\\.
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colVolumes = objWMIService.ExecQuery _ ("Select * from Win32_Volume Where Name = 'D:\\'") For Each objVolume in colVolumes errResult = objVolume.Defrag() Next
Retrieving IDE Controller Information
Retrieves information about all IDE controllers found on a computer.
On Error Resume Next strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_IDEController") For Each objItem in colItems Wscript.Echo "Configuration Manager Error Code: " & _ objItem.ConfigManagerErrorCode Wscript.Echo "Configuration Manager User Configuration: " & _ objItem.ConfigManagerUserConfig Wscript.Echo "Device ID: " & objItem.DeviceID Wscript.Echo "Manufacturer: " & objItem.Manufacturer Wscript.Echo "Name: " & objItem.Name Wscript.Echo "PNP Device ID: " & objItem.PNPDeviceID Wscript.Echo "Protocol Supported: " & objItem.ProtocolSupported Wscript.Echo Next
Retrieving Information About Mapped Network Drives
Retrieves information about mapped network drives. The information returned is similar to that available through the Win32_LogicalDisk class, which retrieves information about the logical disks found on a computer.
On Error Resume Next strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery _ ("Select * from Win32_MappedLogicalDisk") For Each objItem in colItems Wscript.Echo "Compressed: " & objItem.Compressed Wscript.Echo "Description: " & objItem.Description Wscript.Echo "Device ID: " & objItem.DeviceID Wscript.Echo "File System: " & objItem.FileSystem Wscript.Echo "Free Space: " & objItem.FreeSpace Wscript.Echo "Maximum Component Length: " & objItem.MaximumComponentLength Wscript.Echo "Name: " & objItem.Name Wscript.Echo "Provider Name: " & objItem.ProviderName Wscript.Echo "Session ID: " & objItem.SessionID Wscript.Echo "Size: " & objItem.Size Wscript.Echo "Supports Disk Quotas: " & objItem.SupportsDiskQuotas Wscript.Echo "Supports File-Based Compression: " & _ objItem.SupportsFileBasedCompression Wscript.Echo "Volume Name: " & objItem.VolumeName Wscript.Echo "Volume Serial Number: " & objItem.VolumeSerialNumber Wscript.Echo Next
Retrieving SCSI Controller Information
Returns information about all the SCSI controllers found on a computer.
On Error Resume Next strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_SCSIController") For Each objItem in colItems Wscript.Echo "Availability: " & objItem.Availability Wscript.Echo "Configuration Manager Error Code: " & _ objItem.ConfigManagerErrorCode Wscript.Echo "Configuration Manager User Configuration: " & _ objItem.ConfigManagerUserConfig Wscript.Echo "Device ID: " & objItem.DeviceID Wscript.Echo "Driver Name: " & objItem.DriverName Wscript.Echo "Name: " & objItem.Name Wscript.Echo "PNP Device ID: " & objItem.PNPDeviceID Wscript.Echo "Protocol Supported: " & objItem.ProtocolSupported Wscript.Echo "Status Information: " & objItem.StatusInfo Wscript.Echo Next
Retrieving USB Hub Information
Returns information about all the USB hubs found on a computer.
On Error Resume Next strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_USBHub") For Each objItem in colItems Wscript.Echo "Configuration Manager Error Code: " & _ objItem.ConfigManagerErrorCode Wscript.Echo "Configuration Manager User Configuration: " & _ objItem.ConfigManagerUserConfig Wscript.Echo "Description: " & objItem.Description Wscript.Echo "Device ID: " & objItem.DeviceID Wscript.Echo "PNP Device ID: " & objItem.PNPDeviceID Wscript.Echo Next
Returning a Disk Drive Collection
Demonstration script that uses the FileSystemObject to return a list of all the disk drives installed on a computer. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject") Set colDrives = objFSO.Drives For Each objDrive in colDrives Wscript.Echo "Drive letter: " & objDrive.DriveLetter Next
Retrieving USB Controller Information
Returns information about all the USB controllers found on a computer.
On Error Resume Next strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_USBController") For Each objItem in colItems Wscript.Echo "Configuration Manager Error Code: " & _ objItem.ConfigManagerErrorCode Wscript.Echo "Configuration Manager User Configuration: " & _ objItem.ConfigManagerUserConfig Wscript.Echo "Device ID: " & objItem.DeviceID Wscript.Echo "Manufacturer: " & objItem.Manufacturer Wscript.Echo "Name: " & objItem.Name Wscript.Echo "PNP Device ID: " & objItem.PNPDeviceID Wscript.Echo "Protocol Supported: " & objItem.ProtocolSupported Wscript.Echo Next
Running ChkDsk
Runs ChkDks.exe against drive D on a computer.
Const FIX_ERRORS = True strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set objDisk = objService.Get("Win32_LogicalDisk.DeviceID='D:'") errReturn = objDisk.ChkDsk(FIX_ERRORS) Wscript.Echo errReturn
Scheduling AutoChk
Schedules Autochk.exe to run against drive C the next time the computer reboots.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set objDisk = objWMIService.Get("Win32_LogicalDisk") errReturn = objDisk.ScheduleAutoChk(Array("C:")) Wscript.Echo errReturn
Verifying ChkDsk Volume Status
Indicates whether or not any hard drives on a computer have the "dirty bit" set, and should have Chkdsk.exe run.
Const LOCAL_HARD_DISK = 3 strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colDisks = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk Where DriveType = LOCAL_HARD_DISK ") For Each objDisk in colDisks Wscript.Echo "DeviceID: "& vbTab & objDisk.DeviceID Wscript.Echo "DriveType: "& vbTab & objDisk.VolumeDirty Next