Jump to content

Microsoft diskpart automation


dem3tre
 Share

Recommended Posts

For the ppl keenly watching this I have found the problem.

Hicks was correct it does get stuck in a loop but it has nothing to do with the run command. It is the while loop in the __dpReadCommandOutput where it gets stuck.

Func __dpReadCommandOutput( $pid, ByRef $output )
    
    If Not ProcessExists( $pid ) Then Return __dpSetError( 1, $_DP_ErrorCode_InvalidProcessID, 0 )

    $output = ""
    While ProcessExists( $pid )
        ConsoleWrite("In __dpReadCommandOutput while loop" & @LF)
        Local $_peek = StdoutRead( $pid, 0, True )
        If @error Then Return __dpSetError( 1, $_DP_ErrorCode_StdoutStreamError, 0 )
        ConsoleWrite("Peek is " & $_peek & @LF)    
        If $_peek > 0 Then
            $output &= StdoutRead( $pid, $_peek )
            ConsoleWrite("Peek is greater than 0 and output is " & $output & @LF)  
            If @error Then Return __dpSetError( 1, $_DP_ErrorCode_StdoutStreamError, 0 )
        EndIf
        ;
        ; We loop until the process terminates or we see the next prompt
        ;
        If StringRegExp( $output, "(?i)diskpart>", 0 ) Then Return __dpSetError( 0, 0, 1 )
        
    WEnd
    ;
    ; its up to the calling code to determine if this was really unexpected or not
    ; since we don't make any assumptions about the commands being issued
    ;
    Return __dpSetError( 1, $_DP_ErrorCode_UnexpectedTermination, 0 )
EndFunc

To set it up, a blank string was sent to diskpart (by _DiskpartStartConsole) after it loaded thus nothing was returned. So when the peek is done there is nothing there, this no additional prompt is given, this loop is never broken. I'm still learning the inner workings of it so i'm not sure on a solution.

I have tried removing the "__dpIssueCommand( $pid, "", $_output )" from _DiskpartStartConsole but that did not work, it must be there for a reason.

I'm open to ideas to get this working.

Also general question - I want to log all code that runs in the program. I have "#AutoIt3Wrapper_Run_Debug_Mode=Y" but it does not work for include files. I tried the "Per-line trace insertion allows you to log every line of code executed to debug errors" as per the FAQs but that cases errors with compiling and add a line for every line of code! Is there a way to get "#AutoIt3Wrapper_Run_Debug_Mode=Y" to work for all included files?

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

For the ppl keenly watching this I have found the problem.

Hicks was correct it does get stuck in a loop but it has nothing to do with the run command. It is the while loop in the __dpReadCommandOutput where it gets stuck.

which is your windows' language and version?

For example Vista's diskpart is different from Xp's diskpart on the italian version

To set it up, a blank string was sent to diskpart (by _DiskpartStartConsole) after it loaded thus nothing was returned. So when the peek is done there is nothing there, this no additional prompt is given, this loop is never broken. I'm still learning the inner workings of it so i'm not sure on a solution.

I have tried removing the "__dpIssueCommand( $pid, "", $_output )" from _DiskpartStartConsole but that did not work, it must be there for a reason.

I'm open to ideas to get this working.

"__dpIssueCommand( $pid, "", $_output )"

Who created this function use this command only to make a test

Link to comment
Share on other sites

here is the library with my change in diskpart.au3 and win32.au3

Diskpart.rar

OLD Function

Full tested on:

Windows Vista Home Premium italian

Windows Vista Businnes italian

Windows PE 2.0 italian

Some function work on:

Windows XP SP3 italian

Probably work (I hope, I can't test at the moment):

Windows Vista Home/Businnes english

Windows XP Service Pack 2 english

Windows Server 2003 SP1 english

Windows PE 1.6 (W2K3SP1) english

Windows PE 2.0 english

NEW Function

Comments are in italian, when I'll finish everythings I need I'll translate the comment

Full tested on:

Windows Vista Home Premium italian

Windows Vista Businnes italian

Windows PE 2.0 italian

Probably work good but only if there is no error (I don't know how diskpart return errors on english version ):

Windows Vista Home/Businnes english

Windows XP Service Pack 2 english

Windows Server 2003 SP1 english

Windows PE 1.6 (W2K3SP1) english

Windows PE 2.0 english

How translate, example:

if StringRegExp($_output,"(?i)nessun disco selezionato da portare fuori linea",0) Then
    Return __dpSafeReturn( __dpSetError( 1, $_DP_ErrorCode_InvalidTargetFocus, 0 ), $_pHandle )
EndIf

add your diskpart's output for the command

if StringRegExp($_output,"(?i)nessun disco selezionato da portare fuori linea",0) or _
 StringRegExp($_output,"(?i)TRANSLATION FOR YOUR DISKPART LANGUAGE",0) Then
    Return __dpSafeReturn( __dpSetError( 1, $_DP_ErrorCode_InvalidTargetFocus, 0 ), $_pHandle )
EndIf

For any problem try look for every "StringRegExp" and see if its attributes are the same when you use diskpart manually, and in every StringMid look for the start and count attributes

Link to comment
Share on other sites

This is wonderful!

I would LOVE to see the ability to Shrink, Extend, Create, and Delete partitions.

Is this possible?

Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 
Link to comment
Share on other sites

This is wonderful!

I would LOVE to see the ability to Shrink, Extend, Create, and Delete partitions.

Is this possible?

everythings are possible :o but at the moment I develop the function that I need, when I finish I can try to implement those function.

if someone extend the function with english support please post here the code :D

Link to comment
Share on other sites

which is your windows' language and version?

For example Vista's diskpart is different from Xp's diskpart on the italian version

"__dpIssueCommand( $pid, "", $_output )"

Who created this function use this command only to make a test

Windows XP SP2. Diskpart Version 5.1.5365.

From what I can tell, dpIssueCommand is required to send the diskpart commands the process, not just for testing.

Example is in _DiskpartDetailVolume.

Func _DiskpartDetailVolume( $pid, ByRef $disks )
    
    If Not ProcessExists( $pid ) Then Return __dpSetError( 1, $_DP_ErrorCode_InvalidProcessID, 0 )

    Local Const $DetailVolumeCommand = 'detail volume' & @CRLF
   ; some code removed
    __dpIssueCommand( $pid, $DetailVolumeCommand, $_output )

And remember within dpIssueCommand, __dpReadCommandOutput is called which is what sets $_output

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

Windows XP SP2. Diskpart Version 5.1.5365.

From what I can tell, dpIssueCommand is required to send the diskpart commands the process, not just for testing.

Yes yes ... When I say that I was talking about _DiskpartStartConsole ... in THIS funcition dpIssueCommand is used for test ...

Have you try my code? I did many little change in che code

Link to comment
Share on other sites

I have downloaded the file and looked at diskpart. Sorry, I have not had time to fully review your code but I see on line 273 there is still the call to __dpIssueCommand(). Is that required?

In either case, I ran it calling _DiskpartListDisks(). I am using your win32.au3 too.

While it ran no errors, it returned 20041 - last time I check I have one :) I expect a number to be returned not a string due to the function description.

I took my code from the demo - here it is.

#include <String.au3>
#include "diskpart.au3"
#include "win32.au3"

#AutoIt3Wrapper_Run_Debug_Mode=Y

Opt("OnExitFunc", "SafeScriptExit")

Global $pid, $version, $runError
Global $hdiv = _StringRepeat("-", 100) & @LF
Global $sdiv = _StringRepeat("*", 100) & @LF

;; -- DEMONSTRATION CODE --------------------------------------------------------------------------

_DiskpartStartConsole($pid, $version, $runError)

;;
;; Trap script exit to ensure we don't leave a hidden diskpart process running
;;
Func SafeScriptExit()
    If Int($pid) And ProcessExists($pid) Then _DiskpartCloseConsole($pid)
EndFunc   ;==>SafeScriptExit

Global $diskCount, $myDisks
$diskCount = _DiskpartListDisks($pid, $myDisks)

If @error Then
    ConsoleWrite("_DiskpartListDisks failed. RC = " & $diskCount & ", @ERROR = " & @error & ", @EXTENDED = " & @extended & @LF)
;~         Return 0
EndIf

ConsoleWrite("Number of disks are " & $diskCount)

SafeScriptExit()

ideas?

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 have downloaded the file and looked at diskpart. Sorry, I have not had time to fully review your code but I see on line 273 there is still the call to __dpIssueCommand(). Is that required?

No, just for test :D if test fail diskpart will close

In either case, I ran it calling _DiskpartListDisks(). I am using your win32.au3 too.

yes, I have also modify win32.au3

While it ran no errors, it returned 20041 - last time I check I have one :o I expect a number to be returned not a string due to the function description.

I took my code from the demo - here it is.

Your demo is working good on my PC. I don't understand: who returned 20041? _DiskpartListDisks()?__dpIssueCommand()? or exit code?

little change: in every list funtion (listdisk, listvolume, listpartition) before the istruction

If $_okayExtractData Then

decrement the counter:

in _DiskpartListDisks() -> $_diskCount-=1

in _DiskpartListvolumes() -> $_volumeCount-=1

in _DiskpartListPartitions() -> $_partitionCount-=1

because at the end of a list there is an empty string but it count that as a disk, volume, partition

Link to comment
Share on other sites

Ok, cool. Just for test is good - it does not get stuck in the loop now which is good.

20041 is returned from DiskpartListDisks. I use $diskCount to do a ConsoleWrite to print out the number of disks.

I am using Scite and here is my output - can you please add the "#AutoIt3Wrapper_Run_Debug_Mode=Y" to the top of your test program and post yours.

Aslo have you tested each function or are you working on parts? What can I do to help?

CODE
> Starting in DebugMode..

Line: @error-@extended: Line syntax

0004: 0-0: #include <String.au3>

0005: 0-0: #include "diskpart.au3"

0006: 0-0: #include "win32.au3"

0008: 0-0: #AutoIt3Wrapper_Run_Debug_Mode=Y

0010: 0-0: Opt("OnExitFunc", "SafeScriptExit")

0012: 0-0: Global $pid, $version, $runError

0013: 0-0: Global $hdiv = _StringRepeat("-", 100) & @LF

0014: 0-0: Global $sdiv = _StringRepeat("*", 100) & @LF

0018: 0-0: _DiskpartStartConsole($pid, $version, $runError)

0023: 0-0: Func SafeScriptExit()

0027: 0-0: Global $diskCount, $myDisks

0028: 0-0: $diskCount = _DiskpartListDisks($pid, $myDisks)

0030: 0-0: If @error Then

0035: 0-0: ConsoleWrite("Number of disks are " & $diskCount)

Number of disks are 20037: 0-0: SafeScriptExit()

0024: 0-0: If Int($pid) And ProcessExists($pid) Then _DiskpartCloseConsole($pid)

0025: 0-0: EndFunc ;==>SafeScriptExit

0024: 0-0: If Int($pid) And ProcessExists($pid) Then _DiskpartCloseConsole($pid)

0025: 0-0: EndFunc ;==>SafeScriptExit

+>11:08:29 AutoIT3.exe ended.rc:0

+>11:08:30 AutoIt3Wrapper Finished

>Exit code: 0 Time: 7.714

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

20041 is returned from DiskpartListDisks. I use $diskCount to do a ConsoleWrite to print out the number of disks.

Try to use diskpart.exe from the prompt and use LIST DISK command ... there is somethings strange?

I am using Scite and here is my output - can you please add the "#AutoIt3Wrapper_Run_Debug_Mode=Y" to the top of your test program and post yours.

Ok when I can (and remember) I try and post the verbose output.

Your console output on my PC (15 launch) :

Number of disks are 3>Exit code: 0  Time: 7.080

I see that now is 20037 and not 20041 ... why? Have you an idea?

Look at the official diskpart when you use the LIST DISK command, which is the output? How many empty string there are? How many disk have you (really :D not 20037) ?

Aslo have you tested each function or are you working on parts? What can I do to help?

I wrote before where I tested

I only have the italian version and this library (as I write in the comments on diskpart.au3) is language dependent

You can help if you add english support to the new function (I can't, I don't know what diskpart say in its output on english version, I don't have it) or send me the output of the microsoft diskpart with the untranslated command

Link to comment
Share on other sites

Try to use diskpart.exe from the prompt and use LIST DISK command ... there is somethings strange?

Ran the command and it worked -

CODE
Microsoft DiskPart version 5.1.3565

Copyright © 1999-2003 Microsoft Corporation.

On computer: ELXPD189

DISKPART> list disk

Disk ### Status Size Free Dyn Gpt

-------- ---------- ------- ------- --- ---

Disk 0 Online 74 GB 0 B

DISKPART>

I see that now is 20037 and not 20041 ... why? Have you an idea?

Look at the official diskpart when you use the LIST DISK command, which is the output? How many empty string there are? How many disk have you (really :D not 20037) ?

I would say it is reading a random value. I ran it again and it says my disk count is 2. I have to spend some time stepping trough it again - tied up atm. 20037 disk - my mate has about that many. His newest computer has 8 and does full back ups of his 6 other computers ones each week!

I wrote before where I tested

I only have the italian version and this library (as I write in the comments on diskpart.au3) is language dependent

You can help if you add english support to the new function (I can't, I don't know what diskpart say in its output on english version, I don't have it) or send me the output of the microsoft diskpart with the untranslated command

Check out - http://freetranslation.paralink.com/. If I get some time, I will translate your comments and upload the changes.

Thanks for your help Hicks!

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

Ran the command and it worked -

CODE
Microsoft DiskPart version 5.1.3565

Copyright © 1999-2003 Microsoft Corporation.

On computer: ELXPD189

DISKPART> list disk

Disk ### Status Size Free Dyn Gpt

-------- ---------- ------- ------- --- ---

Disk 0 Online 74 GB 0 B

DISKPART>

I would say it is reading a random value. I ran it again and it says my disk count is 2. I have to spend some time stepping trough it again - tied up atm. 20037 disk - my mate has about that many. His newest computer has 8 and does full back ups of his 6 other computers ones each week!

Check out - http://freetranslation.paralink.com/. If I get some time, I will translate your comments and upload the changes.

Thanks for your help Hicks!

If it return 2 it's right! It count as a disk the empty line, I wrote before how to fix this bug ... I don't understand why it give you 20037, when I have time I'll take a look at the code.

For the translation of my comment is better if I'll do ... the automatic translator are really bad with italian to english (my english is bad but better then a translator :D)

At the moment I'm working on other project but I'm here to help if you need

Link to comment
Share on other sites

Thanks for the advice for the translator. I'm working on other things too atm but expect to be back on it soon.

If I run into any issues i'll let you know, thanks again!

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

  • 1 month later...

I'm back at the moment :)

Warning: don't use the old version of diskpart.au3 and mine version too, they have the same bug (If somethings is going wrong sometimes the error is not set because autoit when you return from a call of a function of a function of a function, where @error is set to 1, it reset @error to 0) ... I think that bug is resolved but I want make some test before

[edit]

I have uploaded my last version ... is not complete but it work; remember, work only on italian diskpart if you use with other language you need to modify the regular expression with your language

diskpart_0.3.au3

Edited by Hicks
Link to comment
Share on other sites

  • 4 years later...

I know this post is long dead but I'm currently updating this automation API to be compatible with the newest versions of AutoIT as well as the new Diskpart shell for Windows 8.

If anyone wants to contribute or wants access to the scripts let me know!

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...