Sign in to follow this  
Followers 0
doestergaard

Get disk & partition index number from driver letter

13 posts in this topic

This is a tough one and I hope some of you guys can help me out.

I need to know the disk & partition index numbers from a partition which has Windows installed.

The script will search for the Windows folder and retrieve its drive letter.

I just don't how to manage this. I have tried WMI, but seems tricky and not what im after.

 

Any help is appreciated!

Thanks in advance

Share this post


Link to post
Share on other sites



Did you search the forum? I know this has already been discussed.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

How about >this thread?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Yes i did, but as far as I can tell it only shows how to get the drive letter only, i need the disk number and partition number FROM a driver letter, for instance:

Drive C: is on disk 0 partition 2

Share this post


Link to post
Share on other sites

When running this script (generated by the AutoIt tool ScriptoMatic) do you get the correct result?

; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDiskToPartition", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output = $Output & "Antecedent: " & $objItem.Antecedent & @CRLF
      $Output = $Output & "Dependent: " & $objItem.Dependent & @CRLF
      $Output = $Output & "EndingAddress: " & $objItem.EndingAddress & @CRLF
      $Output = $Output & "StartingAddress: " & $objItem.StartingAddress & @CRLF
      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_LogicalDiskToPartition" )
Endif

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Yes

But how does it know its drive C? :)

Thank you very much for your help! Really appreciate it

Share this post


Link to post
Share on other sites

Because the driver letter is part of $obj.Dependent


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

This should display a MsgBox for all drives displaying disk # and partition #.

$objWMIService = ObjGet("winmgmts:\\.\root\CIMV2")
$WMIQuery = $objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDiskToPartition")
For $obj In $WMIQuery
    $sAntecedent = $obj.Antecedent
    $sDependent = $obj.Dependent
    $iDisk = StringMid($sAntecedent, StringInStr($sAntecedent, "Disk #") + 6, 1)
    $iPartitiion = StringMid($sAntecedent, StringInStr($sAntecedent, "Partition #") + 11, 1)
    $sDrive = StringMid($sDependent, StringInStr($sDependent, 'DeviceID="') + 10, 1)
    MsgBox(0, $sDrive & " Drive", $sDrive & " Drive is on Disk #" & $iDisk  & ", partition #" & $iPartitiion)
Next

Works for one digit disk # and partition #.

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Holy, sh*t! Thanks!

You are mig hero :zorro:

Just a final question. I will only need the integer for Building a diskpart script for Windows PE. The above code will detect the Windows partition yes?

Once Again, thank you very much!!

Share this post


Link to post
Share on other sites

Full description of the Win32_LogicalDiskToPartition class can be found here.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Whoops, sry, it only gives back the int, thanks! :D

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

One more thing. Diskpart says that drive C has partition number 2 The result i get from your code is 1

Edited by doestergaard

Share this post


Link to post
Share on other sites

What does the Windows Device Manager display?

I'm running Windows in a VM and get partition number 1 because partition 0 is used by Windows itself.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0