32bitcrusade Posted March 15, 2013 Share Posted March 15, 2013 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:\"? Link to comment Share on other sites More sharing options...
LarryDalooza Posted March 15, 2013 Share Posted March 15, 2013 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. AutoIt has helped make me wealthy Link to comment Share on other sites More sharing options...
32bitcrusade Posted March 16, 2013 Author Share Posted March 16, 2013 (edited) 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 March 16, 2013 by 32bitcrusade Link to comment Share on other sites More sharing options...
LarryDalooza Posted March 18, 2013 Share Posted March 18, 2013 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. AutoIt has helped make me wealthy Link to comment Share on other sites More sharing options...
iamtheky Posted March 18, 2013 Share Posted March 18, 2013 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: ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
KaFu Posted March 18, 2013 Share Posted March 18, 2013 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) OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
UEZ Posted March 18, 2013 Share Posted March 18, 2013 (edited) 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 March 18, 2013 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Solution 32bitcrusade Posted March 19, 2013 Author Solution Share Posted March 19, 2013 (edited) 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 March 19, 2013 by 32bitcrusade Link to comment Share on other sites More sharing options...
kylomas Posted March 23, 2013 Share Posted March 23, 2013 (edited) 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 March 23, 2013 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now