Jump to content
Sign in to follow this  
32bitcrusade

Partition numbers to drive letters

Recommended Posts

32bitcrusade

Hi,

I am creating an automated Windows installer using WIM images. I've got most of it covered execpt for converting a parition object into a drive letter. I took a look at this code:

$oWMISvc = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
$colDiskDrives = $oWMISvc.ExecQuery("SELECT * FROM Win32_DiskDrive")
For $oDiskDrive In $colDiskDrives
ConsoleWrite("DiskDrive = " & $oDiskDrive.DeviceId & " Caption = " & $oDiskDrive.Caption & @LF)

$sQuery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & $oDiskDrive.DeviceId & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
$colPartitions = $oWMISvc.ExecQuery($sQuery)

For $oPartition In $colPartitions
ConsoleWrite(@TAB & "Partition = " & $oPartition.DeviceId & @LF)

$sQuery = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $oPartition.DeviceId & "'} WHERE AssocClass = Win32_LogicalDiskToPartition"
$colLogicalDisks = $oWMISvc.ExecQuery($sQuery)

For $oLogicalDisk In $colLogicalDisks
ConsoleWrite(@TAB & @TAB & "LogicalDisk = " & $oLogicalDisk.DeviceId & @LF)
Next
Next
Next

and it works, but I'm confused. How can I fix it so it converts a partition object, like "Disk #0, Partition #1", into a drive letter, like "C:\"?

Share this post


Link to post
Share on other sites
LarryDalooza

You mean assigning a drive letter to a partition? or reassigning letters? I do what you are doing for a living.... I can help, if I have more info.

Lar.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites
32bitcrusade

No. Getting the drive letter from a partition ID. Basically, I would like to apply a WIM image to a specific partition on a specific disk.

The script would do these things, in order.

1 : Initialize WinPE/Networking

2 : Set Source - Contained in a INI file, residing in the System32 directory.

3 : Partition the first disk using a diskpart script.

4 : Find the first partition of the first disk.

5 : Apply the image to that partition

6 : Install the boot sector to the partition

7 : Copy post-installation files

8 : Reboot

And I would like to use Windows Managment Insterumentation (WMI) to accopmlish this trival task.

Edited by 32bitcrusade

Share this post


Link to post
Share on other sites
LarryDalooza

I admit, I never use WMI. I use AutoIt to deploy... here is a link that may help...

http://stackoverflow.com/questions/9346320/wmi-to-get-drive-letter-association-with-physical-drive-path-misses-cdroms

But, if you are using Diskpart, then you should already know what drive letter you assigned.

I use AutoIt, DiskPart, imagex. Wipe clean and apply. Occasionally I use WMIC trapping stdout to get a physical disk size to dynamically create a recovery partition.

Lar.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites
iamtheky

i agree completely with larry, we do many 10s of thousands of deployments in the same fashion; save for the physical disk size detection in which i use WinAPiEx:


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
KaFu

Using WinAPIEx you can create a reference array to search for the matching combo too.

#include <Array.au3>
#include <WinAPIEx.au3>

Global $aDrive_To_Disk_And_Partition[26][3], $aDriveNumber
For $i = 97 To 122
ConsoleWrite("Analyze Drive: " & Chr($i) & @CRLF)
$aDriveNumber = _WinAPI_GetDriveNumber(Chr($i) & ":")
If Not @error Then
$aDrive_To_Disk_And_Partition[$i - 97][0] = Chr($i)
$aDrive_To_Disk_And_Partition[$i - 97][1] = $aDriveNumber[1]
$aDrive_To_Disk_And_Partition[$i - 97][2] = $aDriveNumber[2]
Else
$aDrive_To_Disk_And_Partition[$i - 97][0] = Chr($i)
$aDrive_To_Disk_And_Partition[$i - 97][1] = "n/a"
$aDrive_To_Disk_And_Partition[$i - 97][2] = "n/a"
EndIf
Next

; [0] - Drive letter
; [1] - The device number
; [2] - The partition number, or (-1) if device cannot be partitioned.
_ArrayDisplay($aDrive_To_Disk_And_Partition)

Share this post


Link to post
Share on other sites
UEZ

Hmm, with $oLogicalDisk.DeviceId you get your drive letter. I don't understand

How can I fix it so it converts a partition object, like "Disk #0, Partition #1", into a drive letter, like "C:"?

My result:

DiskDrive = \\.\PHYSICALDRIVE0 Caption = ST9320423AS
Partition = Disk #0, Partition #0
LogicalDisk = C:
Partition = Disk #0, Partition #1

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
32bitcrusade

I found a solution to my problem after a bit of debugging. Basically, it's a bunch of For loops running in sequence to return the drive letter. Here's the code:

$oWMISvc = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
$colDiskDrives = $oWMISvc.ExecQuery("SELECT * FROM Win32_DiskDrive")
For $oDiskDrive In $colDiskDrives
$CurrentPhDisk = $oDiskDrive.DeviceId
If $CurrentPhDisk = $CmdLine[1] Then
ExitLoop
EndIf
Next
ConsoleWrite("Selected right disk, selecting parition..." & @CRLF)
$sQuery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & $CurrentPhDisk & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
$colPartitions = $oWMISvc.ExecQuery($sQuery)
For $oPartition In $colPartitions
$CurrentPartit = $oPartition.DeviceId
$PCMD = StringRight($CurrentPartit, 1)
If $PCMD = $CmdLine[2] Then
ConsoleWrite("Selected partition: " & $CurrentPartit & @CRLF)
ExitLoop
EndIf
Next
ConsoleWrite("Selected parition, converting to letter..." & @CRLF)
$sQuery = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $CurrentPartit & "'} WHERE AssocClass = Win32_LogicalDiskToPartition"
$colLogicalDisks = $oWMISvc.ExecQuery($sQuery)
For $oLogicalDisk In $colLogicalDisks
$EndingDLetter = $oLogicalDisk.DeviceId
Next
ConsoleWrite("Drive letter is: " & $EndingDLetter & @CRLF)

And the console output is:

Selected right disk, selecting parition...
Selected partition: Disk #0, Partition #0
Selected parition, converting to letter...
Drive letter is: F:
Edited by 32bitcrusade

Share this post


Link to post
Share on other sites
kylomas

32bitcrusade,

I was goofing around with scriptomatic and came up with this after I remembered this thread. Thought it might interest you.

; Generated by AutoIt Scriptomatic

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

local $output

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

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

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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  

×