Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for bootdriv.vbs

(view source code of bootdriv.vbs as plain text)

  1. ' Check command line parameters (none required)
  2. If WScript.Arguments.Count > 0 Then
  3. 	strMsg = vbCrLf _
  4. 	       & "BootDriv.vbs,  Version 1.00" & vbCrLf _
  5. 	       & "Return drive letter from which computer was booted" _
  6. 	       & vbCrLf & vbCrLf _
  7. 	       & "Usage:  CSCRIPT  BOOTDRIV.VBS" _
  8. 	       & vbCrLf & vbCrLf _
  9. 	       & "Written by Rob van der Woude" & vbCrLf _
  10. 	       & "http://www.robvanderwoude.com" _
  11. 	       & vbCrLf & vbCrLf _
  12. 	       & "Code to associate partitions with drive letters found on" & vbCrLf _
  13. 	       & "TechNet Script Center, section Disks and File Systems:" & vbCrLf _
  14. 	       & "http://www.microsoft.com/technet/treeview/default.asp?url=/technet/scriptcenter/dfs/ScrDFS37.asp" & vbCrLf _
  15. 	       & "(C) 2003 Microsoft Corporation"
  16. 	WScript.Echo strMsg
  17. 	WScript.Quit(1)
  18. End If
  19.  
  20. ' Don't stop at errors
  21. On Error Resume Next
  22.  
  23. ' For the local computer only
  24. strComputer = "."
  25.  
  26. ' Use WMI to get boot drive as disk and partition numbers
  27. Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
  28. Set colItems = objWMIService.ExecQuery( "Select * From Win32_DiskPartition Where BootPartition = true", , 48 )
  29. For Each objItem in colItems
  30.     strBootDiskIndex = objItem.DiskIndex
  31.     strBootPartition = objItem.Index
  32. Next
  33. ' Format the result to compare it with the result of next query
  34. strBootDisk = "Disk #" & strBootDiskIndex & ", Partition #" & strBootPartition
  35.  
  36. ' Associate partitions with drive letters.
  37. ' Script found on TechNet Script Center, section Disks and File Systems:
  38. ' http://www.microsoft.com/technet/treeview/default.asp?url=/technet/scriptcenter/dfs/ScrDFS37.asp
  39. ' (C) 2003 Microsoft Corporation
  40. Set objWMIService = GetObject( "winmgmts:{ impersonationLevel=Impersonate }!//" & strComputer )
  41. Set colDiskDrives = objWMIService.ExecQuery( "Select Caption, DeviceID FROM Win32_DiskDrive" )
  42. For Each objDiskDrive In colDiskDrives
  43. 	strEscapedDeviceID = Replace( objDiskDrive.DeviceID, "\", "\\", 1, -1, vbTextCompare )
  44. 	Set colDiskPartitions = objWMIService.ExecQuery( "Associators Of {Win32_DiskDrive.DeviceID=""" & strEscapedDeviceID & """} Where AssocClass = Win32_DiskDriveToDiskPartition" )
  45. 	For Each objDiskPartition In colDiskPartitions
  46. 		Set colLogicalDisks = objWMIService.ExecQuery( "Associators Of {Win32_DiskPartition.DeviceID=""" & objDiskPartition.DeviceID & """} Where AssocClass = Win32_LogicalDiskToPartition" )
  47. 		For Each objLogicalDisk In colLogicalDisks
  48. 			If objDiskPartition.DeviceID = strBootDisk Then
  49. 				WScript.Echo vbCrLf & "Boot Drive = " & objLogicalDisk.DeviceID
  50. 			End If
  51. 		Next
  52. 	Next
  53. Next
  54.  

page last uploaded: 2016-12-15, 11:20