Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WMI Logical to Physical disk translation

Status
Not open for further replies.

gregn1234

MIS
Jun 16, 2009
5
0
0
US
Hi,

I found the following script on the MS site and i'm having trouble converting it to work the way I want. I'm trying to find the physical disk from a given volume label. Here is the original script:

Code:
'---------------------------

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colDiskDrives = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive")
 
For Each objDrive In colDiskDrives
    Wscript.Echo "Physical Disk: " & objDrive.Caption & " -- " & objDrive.DeviceID 
    strDeviceID = Replace(objDrive.DeviceID, "\", "\\")
    Set colPartitions = objWMIService.ExecQuery _
        ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & _
            strDeviceID & """} WHERE AssocClass = " & _
                "Win32_DiskDriveToDiskPartition")
 
    For Each objPartition In colPartitions
        Wscript.Echo "Disk Partition: " & objPartition.DeviceID
        Set colLogicalDisks = objWMIService.ExecQuery _
            ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
                objPartition.DeviceID & """} WHERE AssocClass = " & _
                    "Win32_LogicalDiskToPartition")
 
        For Each objLogicalDisk In colLogicalDisks
            Wscript.Echo "Logical Disk: " & objLogicalDisk.DeviceID
        Next
        Wscript.Echo
    Next
    Wscript.Echo
Next


'---------------------------

and I've tried to convert it like this:

Code:
'---------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colLogicalDisks = objWMIService.ExecQuery _
    ("Select * From Win32_LogicalDisk Where VolumeName = 'System'")


For Each objLogicalDisk In colLogicalDisks
    WScript.Echo "Logical Disk: " & objLogicalDisk.DeviceID 
    
    Set colPartitions = objWMIService.ExecQuery _
        ("ASSOCIATORS OF {Win32_LogicalDiskToPartition.DeviceID=""" & _
            objLogicalDisk.DeviceID & """} WHERE AssocClass = " & _
                "Win32_DiskPartition")
 
    For Each objPartition In colPartitions
        Wscript.Echo "Disk Partition: " & objPartition.DeviceID
        Set colDiskDrives = objWMIService.ExecQuery _
            ("ASSOCIATORS OF {Win32_DiskDriveToDiskPartition.DeviceID=""" & _
                objPartition.DeviceID & """} WHERE AssocClass = " & _
                    "Win32_DiskDrive")
 
        For Each objDrive In colDiskDrives
            Wscript.Echo "Physical Disk: " & objDrive.DeviceID
        Next

    Next

Next
'---------------------------

Every time I run my version it errors out with a 0x8004103A

Please can you help me as this is driving me nuts!

Many thanks,

Greg.
 
Not really sure what you are trying to accomplish. But I think this may be it.

Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colDiskDrives = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive")
 
For Each objDrive In colDiskDrives
    Wscript.Echo "Physical Disk: " & objDrive.Caption & " -- " & objDrive.DeviceID 
    strDeviceID = Replace(objDrive.DeviceID, "\", "\\")
    Set colPartitions = objWMIService.ExecQuery _
        ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & _
            strDeviceID & """} WHERE AssocClass = " & _
                "Win32_DiskDriveToDiskPartition")
 
    For Each objPartition In colPartitions
        Wscript.Echo "Disk Partition: " & objPartition.DeviceID
        Set colLogicalDisks = objWMIService.ExecQuery _
            ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
                objPartition.DeviceID & """} WHERE AssocClass = " & _
                    "Win32_LogicalDiskToPartition")
 
        For Each objLogicalDisk In colLogicalDisks
        	If objLogicalDisk.VolumeName = "System" Then
            	WScript.Echo "Logical Disk: " & objLogicalDisk.DeviceID
            End If
        Next
        Wscript.Echo
    Next
    Wscript.Echo
Next

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Hi Mark,

Thanks so much for helping me ;-)

I've tested your script and i'm sure it's a step in the right direction. However, what I'm trying to do is to identify the phyisical disk number associated to a given logical disk volume label.

So for example, I have a local hard disk with a C:\ partition that has the volume label "System" - I'm trying to write a script that can identify which physical disk this "System" partition is located on.

I need this because my overall requirement is to write a script that applies the GRUB bootloader - obviously I need to ensure that I apply the GRUB bootloader to the correct hard disk and I thought the best way to do this would be to detect the HD by using the known volume label.

If you have any other ideas or suggestions on how this might be done, please let me know.

Many thanks for your help - it's hugely appreciated ;-)

Kind regards,

Greg.
 
I'll need to play with this a bit. I hate the associators classes, they are so darn confusing. Will let you know if I figure this out.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
OK, thanks Mark - glad it wasn't just me :)

I tried very hard just to try and reverse the logic - so take the logical disk info and use that to discover the physical disk info - but I didn't get very far, as you can see!!!

Thanks so much for taking the time to help me, I really appreciate your assistance.

Kind regards,

Greg.
 
It looks like this will be easier to do in PowerShell. can you use PowerShell instead of VBScript?

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Give this a try in PowerShell.

Code:
$strComputer = "."
$drives = Get-WmiObject Win32_DiskDrive -ComputerName $strComputer 


$s = New-Object System.Management.ManagementObjectSearcher 
$s2 = New-Object System.Management.ManagementObjectSearcher 


$qPartition = new-object System.Management.RelatedObjectQuery 
$qPartition.RelationshipClass = 'Win32_DiskDriveToDiskPartition' 


$qLogicalDisk = new-object System.Management.RelatedObjectQuery 
$qLogicalDisk.RelationshipClass = 'Win32_LogicalDiskToPartition' 


$drives |% { 
  $_.Caption 
  $qPartition.SourceObject = $_ 
  $s.Query = $qPartition 
  $s.get() |% {   
    $_.DeviceID 
    $qLogicalDisk.SourceObject = $_ 
    $s2.query = $qLogicalDisk 
    $s2.get() 
  } 
}

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Hi Mark,

Sorry for the delayed reply. I'm also sorry to complicate matters, but I'm trying to run this script in a WinPE 2.0 session. Do you know how to make PowerShell work under WinPE?

Alternatively, if there's a way of making VBscript do the job that would be my preference - but I understand if it's not possible.

Many thanks,

Greg
 
I'm sure it is possible, it is just a matter of better understanding the associators which I fully admit a deficiency at. If your WinPE system is connected to the network, then you should be able to point this code to it from a remote system.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
No worries, thanks for you help and advice ;-)

Kind regards,

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top