Jump to content

Microsoft diskpart automation


dem3tre
 Share

Recommended Posts

Microsoft Diskpart Automation API

* UPDATED: 9 January 2007 ** Version 0.21 *

Added _DiskpartDetailVolume, _DiskpartDetailPartition, and updated the demo to show their use

The structure of the API is fairly stable now and should change little going forward as new functions are added.

The following functions are currently available:

  • Process commands:
  • _DiskpartStartConsole......Begin an interactive diskpart session
  • _DiskpartCloseConsole......Terminate an interactive diskpart session
  • _DiskpartTerminationCode...Returns the last recorded diskpart process termination code
  • Focus commands:
  • _DiskpartSelectDisk.........Sets focus to the given disk
  • _DiskpartSelectVolume.......Sets focus to the given volume
  • _DiskpartSelectPartition....Sets focus to the given partition
  • _DiskpartCurrentDisk........Returns the current in-focus disk
  • _DiskpartCurrentVolume......Returns the current in-focus volume
  • _DiskpartCurrentPartition...Returns the current in-focus partition
  • Information commands:
  • _DiskpartListDisks................Lists all fixed disks recognized by diskpart
  • _DiskpartListVolumes..............Lists all volumes (including cd/dvd-rom and removeable)
  • _DiskpartListPartitions...........Lists all partitions for the in-focus disk or volume
  • _DiskpartListPartitionsByDisk.....Sets focus to the given disk and lists its partitions
  • _DiskpartListPartitionsByVolume...Sets focus to the given volume and lists its partitions
  • _DiskpartDetailDisk...............Obtains detailed information for the in-focus disk
  • _DiskpartDetailVolume.............Obtains detailed information for the in-focus volume
  • _DiskpartDetailPartition..........Obtains detailed information for the in-focus partition
  • Management commands:
  • TODO
  • Helpful functions:
  • TODO _DiskpartFindDiskByDiskID.........Returns the index of the disk with matching Disk ID (ie., 1CCE2CD3)
  • TODO _DiskpartFindDisksByName..........Returns an array of disks that match the given name (ie., Maxtor OneTouch IIIs USB Device)
  • TODO _DiskpartFindDisksByType..........Returns an array of disks of the given type (ie., USB, IDE)
Please see the attached dpdemo.au3 file for an example on how to use this. I've done my best to document the code and make things easy to understand. The dpdemo-output.txt is the console output from the sample as run on my test box.

win32.au3

diskpart.au3

dpdemo.au3

dpdemo_output.txt

Edited by dem3tre
Link to comment
Share on other sites

Excellent idea,

That sort of thing to me is a great memory aid more than anything else. I use diskpart once every 6 months or so and I don't like worrying with getting the syntax right.

Also since I am a noob with AutoIT the script should provide me with some insite.

:P

****************************noob[quote]"I never did give them hell. I just told the truth, and they thought it was hell."- Harry S Truman[/quote]

Link to comment
Share on other sites

Will add the remaining basic disk commands: assign, active, create, clean, delete, and remove tonight. I don't deal much with dynamic disks and the more server centric commands but will add them on request - just let me know which one you need. I'll also add a function that allows you to send raw commands to the console.

Link to comment
Share on other sites

If you can stomach using an internal API function you can issue any command you wish to the diskpart console and get the following benefits automatically:

  • If diskpart coredumps/exits you can retrieve its termination code.
  • If the command you send isn't recognized by diskpart, you'll be notified.
  • The output returned is constrained to the current command session only so you don't have to worry about previous command results when parsing the output.
Here's the function you should check out:

;;
;; Function     : __dpIssueCommand
;; Description  : Issues a command to the diskpart interactive console and captures its result. 
;; Parameters   : $pid      : [in]  running diskpart process identifier
;;                $cmd      : [in]  command to issue
;;                $output   : [out] command output
;; Returns      : Success   : 1
;;                Failure   : 0
;;                @ERROR    : 0  = Success
;;                          : 1  = Failure
;;                @EXTENDED : 2  = $_DP_ErrorCode_InvalidProcessID
;;                          : 3  = $_DP_ErrorCode_UnexpectedTermination
;;                          : 4  = $_DP_ErrorCode_StdoutStreamError
;;                          : 5  = $_DP_ErrorCode_StdinStreamError
;;                          : 7  = $_DP_ErrorCode_UnrecognizedCommand
;; Remarks      : If @extended = 3 use _DiskpartTerminationCode to retreive its exit code
;;

Checkout any of the _Diskpart*** functions to see how its used.

I do intend to wrap most diskpart commands to trap errors and simplify getting the results, but it may take a while with my current work schedule. In the mean time, I'll do my best to answer any questions.

Thanks !!

Link to comment
Share on other sites

Here is a script you can use to display a dialog to a user and ask them to select a disk while simulatenously showing them the details of those disks. As output, it returns the disk id that can be fed back into diskpart.

;;
;; Function     : Ask_TargetDisk
;; Description  : Displays a selection GUI containing the current disks available in
;;                the system and asks the user to choose one.
;; Parameters   : None
;; Returns      : Success   : diskpart disk number of the selected disk
;;                @ERROR    : 0  = Success
;;                          : 1  = Failure
;;                @EXTENDED : 0  = Script error
;;                            1  = User aborted operation
;;                            2  = Diskpart failed in some way, unable to obtain necessary disk information
;;                            3  = Diskpart did not report any disks!
;;

Posted Image

Requires diskpart.au3 version 0.21 or greater

Ask_TargetDisk.au3

Link to comment
Share on other sites

  • 2 weeks later...

It hasn't been abandoned but other priorities needed to be dealt with. Expect an update in the next day or two with most of the management functions added (at least those that i could readily test).

Thanks for the continued interest!

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 months later...
  • 1 year later...

are there any news about this diskpart.au3?

I miss functions like to set disk and partition (size,letter, etc.), does anyone based on "dem3tre" further develped his work on diskpart.au3?

regards

vope

Edited by vope
Link to comment
Share on other sites

It doesn't work on Win 2000 :-(

Executing: list disk

****************************************************************************************************

No disks were returned

****************************************************************************************************

Executing: list volume

****************************************************************************************************

No volumes were returned

No volumes were returned

****************************************************************************************************

Microsoft diskpart process termination code was 65535

Why ?????

Link to comment
Share on other sites

  • 1 month later...

I'm trying to use your library but it doesn't work. When I use _DiskpartStartConsole the program make an infinite loop, I don't know how but I think that the problem is in this istruction

$pid     = Run( 'diskpart.exe', @SystemDir, @SW_HIDE, $STDIN_CHILD+$STDOUT_CHILD )

I replace that istruction with this as the autoit manual for the command line application

$pid     = Run(@ComSpec & " /c " & 'diskpart.exe', @SW_HIDE, $STDIN_CHILD+$STDOUT_CHILD )

Now it "work" as wooltown but Vista

I'll try to do something

Link to comment
Share on other sites

I'm trying to use your library but it doesn't work. When I use _DiskpartStartConsole the program make an infinite loop, I don't know how but I think that the problem is in this istruction

$pid     = Run( 'diskpart.exe', @SystemDir, @SW_HIDE, $STDIN_CHILD+$STDOUT_CHILD )

I replace that istruction with this as the autoit manual for the command line application

$pid     = Run(@ComSpec & " /c " & 'diskpart.exe', @SW_HIDE, $STDIN_CHILD+$STDOUT_CHILD )

Now it "work" as wooltown but Vista

I'll try to do something

Errata corrige

$pid     = Run(@ComSpec & " /c " & 'diskpart',"c:\", @SW_HIDE, $STDIN_CHILD+$STDOUT_CHILD )

In this library I found these problems:

- language: functions work only with english, for example when it read the stdout and looking for a regular expression

If StringRegExp( $_outputLines[$idx], "(?i)disk ###[ ]+status[ ]+size[ ]+free[ ]+dyn[ ]+gpt", 0 ) or _
                    StringRegExp( $_outputLines[$idx], "(?i)disco n[.][ ]+stato[ ]+dim[.][ ]+libera[ ]+din[ ]+gpt", 0 ) or _         ; in italian on XP
                    StringRegExp( $_outputLines[$idx], "(?i)disk ###[ ]+stato[ ]+dim[.][ ]+libera[ ]+din[ ]+gpt", 0 )Then; in italian on VISTA

probably is the same problem of wooltown

- in the library win32.au3, function _Win32GetExitCodeProcess didn't work ... I replace the local variable declaration with

Local $result = DllCall('kernel32.dll','ptr', 'GetExitCodeProcess','ptr', $handle,'int*', $ptrErrCode)
Link to comment
Share on other sites

  • 2 weeks later...

Hi all

I am having the same trouble as Hicks. I downloaded all files, ran the demo and find it hangs.

I added "#AutoIt3Wrapper_run_debug_mode=Y" to see what is going on it hangs on the call to _DiskpartStartConsole

Here is the console output

>Running:(3.2.12.1):C:\Program Files\AutoIt3\autoit3.exe "H:\DiskPart\dpdemo.au3"   
!> Starting in DebugMode..
Line: @error-@extended: Line syntax
0008: 0-0: #include <Misc.au3>
0009: 0-0: #include <String.au3>
0011: 0-0: #include "diskpart.au3"
0012: 0-0: #include "win32.au3"
0013: 0-0: #AutoIt3Wrapper_run_debug_mode=Y
0014: 0-0: Opt("OnExitFunc", "SafeScriptExit")
0016: 0-0: Global $pid, $version, $runError
0017: 0-0: Global $hdiv = _StringRepeat( "-", 100 ) & @LF
0018: 0-0: Global $sdiv = _StringRepeat( "*", 100 ) & @LF
0022: 0-0: _DiskpartStartConsole( $pid, $version, $runError )

Ideas?

Edit: Mods - I know this is not a help section of the forum but didn't want to post in two different sections - i'll move it if you want.

Edited by bo8ster

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

I've looked into it a bit more added a few lines to diskpart.au3

Func _DiskpartStartConsole( ByRef $pid, ByRef $version, $runerror = Default )
ConsoleWrite("starting _DiskpartStartConsole")
    If Not $runerror = Default Then $runerror = 0

    $version = ""
    ConsoleWrite("calling dispart.exe" & @LF)
    $pid = Run( 'diskpart.exe', @SystemDir, @SW_HIDE, $STDIN_CHILD+$STDOUT_CHILD )
    ConsoleWrite("called dispart.exe - now running" & @LF)
    If @error Then 
        If Not $runerror = Default Then $runerror = @extended
        Return __dpSetError( 1, $_DP_ErrorCode_ConsoleStartFailed, 0 )
    EndIfoÝ÷ Ù8b²z0¦º¹Ç¬r^n`µ§)àßmvÔ-=Øú趦)^³Ovëh"Ýô÷f®¶­Ýì^ªê-Ov+$=ªíÓÝ+JjíêºwÞRµªíx"7º(uââç«®ÞÆקuçKw¬Ê{ZÇM4ãM"Énuém~)^jíà·M4çM"ÉnuémjºÚÉ«·Ý4Ó­4w%¹×¥´*'²Ö§¶Æ®ÞtÓO4Ò)Üç^Ó"±Æ®ÞtÓOtÒ)Üç^Ô­®)àjíà·M5×M"Énuꮢ×b²JZ®Ö®Þ«¨·M5ÛM"ÉnuꮢÜ"}»z®¢Ý4×4ëh"ÝÖ­ªizºî׺ ¨u

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...