You are on page 1of 2

' Check command line parameters (none required)

If WScript.Arguments.Count > 0 Then


strMsg = vbCrLf _
& "BootDriv.vbs, Version 1.00" & vbCrLf _
& "Return drive letter from which computer was booted" _
& vbCrLf & vbCrLf _
& "Usage: CSCRIPT BOOTDRIV.VBS" _
& vbCrLf & vbCrLf _
& "Written by Rob van der Woude" & vbCrLf _
& "http://www.robvanderwoude.com" _
& vbCrLf & vbCrLf _
& "Code to associate partitions with drive letters found on" & vbCrLf
_
& "TechNet Script Center, section Disks and File Systems:" & vbCrLf _
& "http://www.microsoft.com/technet/treeview/default.asp?
url=/technet/scriptcenter/dfs/ScrDFS37.asp" & vbCrLf _
& "(C) 2003 Microsoft Corporation"
WScript.Echo strMsg
WScript.Quit(1)
End If

' Don't stop at errors


On Error Resume Next

' For the local computer only


strComputer = "."

' Use WMI to get boot drive as disk and partition numbers
Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
Set colItems = objWMIService.ExecQuery( "Select * From Win32_DiskPartition Where
BootPartition = true", , 48 )
For Each objItem in colItems
strBootDiskIndex = objItem.DiskIndex
strBootPartition = objItem.Index
Next
' Format the result to compare it with the result of next query
strBootDisk = "Disk #" & strBootDiskIndex & ", Partition #" & strBootPartition

' Associate partitions with drive letters.


' Script found on TechNet Script Center, section Disks and File Systems:
' http://www.microsoft.com/technet/treeview/default.asp?
url=/technet/scriptcenter/dfs/ScrDFS37.asp
' (C) 2003 Microsoft Corporation
Set objWMIService = GetObject( "winmgmts:{ impersonationLevel=Impersonate }!//" &
strComputer )
Set colDiskDrives = objWMIService.ExecQuery( "Select Caption, DeviceID FROM
Win32_DiskDrive" )
For Each objDiskDrive In colDiskDrives
strEscapedDeviceID = Replace( objDiskDrive.DeviceID, "\", "\\", 1, -1,
vbTextCompare )
Set colDiskPartitions = objWMIService.ExecQuery( "Associators Of
{Win32_DiskDrive.DeviceID=""" & strEscapedDeviceID & """} Where AssocClass =
Win32_DiskDriveToDiskPartition" )
For Each objDiskPartition In colDiskPartitions
Set colLogicalDisks = objWMIService.ExecQuery( "Associators Of
{Win32_DiskPartition.DeviceID=""" & objDiskPartition.DeviceID & """} Where
AssocClass = Win32_LogicalDiskToPartition" )
For Each objLogicalDisk In colLogicalDisks
If objDiskPartition.DeviceID = strBootDisk Then
WScript.Echo vbCrLf & "Boot Drive = " &
objLogicalDisk.DeviceID
End If
Next
Next
Next