none
How to change/set driver letter for boot partition using VBS RRS feed

  • Question

  • Hi,

    Tried multiple WMI queries to match (like, Win32_Volume, Win32_DiskPartition ..) to get volume label/name of "disk 0#, partition 0#" to set driver letter. 

    Searching in google almost 3 days but no luck.

    Did anyone got a chance to write a script with below requirement?

    OS: Windows 7 x64 and x86

    Logic: Set driver letter for 'Boot' ('System' Volume name in below screenshot) partition using VBS.

    Current Disk partition as below:

    Tried get match properties between 'Win32_Volume' and 'Win32_DiskPartition' using 'WMI Code Creator' tool but didn't found any logic.

    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 & " - " & wmiLogicalDisk.VolumeName
            Next
        Next
    Next

    Output as below: but Unable to retrieve 'Volume 1' label name ('System')

    Any suggestions?

    Regards

    Er Reddy

    • Edited by Er Reddy Tuesday, November 19, 2019 11:23 PM
    Tuesday, November 19, 2019 10:54 PM

All replies

  •  Get-WmiObject win32_volume | select label,deviceid

    Get the volume to partition mapping and you will have everything.


    \_(ツ)_/


    • Edited by jrv Tuesday, November 19, 2019 11:35 PM
    Tuesday, November 19, 2019 11:34 PM
  • Thanks,

    But how to check which volume's "BootPartition" is true from win32_volume?

    I able to get BootPartition device id # and partition id # as (Disk #0, Partition #0) with below code.

    strComputer = "."
    Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
    Set colItems = objWMIService.ExecQuery("Select * From Win32_DiskPartition Where BootPartition = true", , 48)
    For Each objItem in colItems
         objItem.DeviceID
    Next
    As I said previously unable to retrieve volume name for "Disk #0, Partition #0"

    Actually I got stuck here from long time.

    ~~

    Tuesday, November 19, 2019 11:54 PM
  • This is how to get the boot partition:

    Get-WmiObject win32_diskpartition|where{$_.bootpartition}

    The issue here is that the partition is hardware that preexists the OS.  An OS scans for volumes and not partitions.  Disk Manager access the low-level API to get full info and can do that.  WMI does not have a class that gives that relationship.

    The easiest way to get the mapping is with the diskpart tool.

    diskpart /?


    \_(ツ)_/

    Wednesday, November 20, 2019 12:16 AM