use strict; use warnings; use IO::Handle qw( ); use IPC::Open2 qw( open2 ); sub wait_for_prompt { my ($to_chld, $fr_chld) = @_; # Force two prompts to appear. print $to_chld "\n"; my $count; while (<$fr_chld>) { print "$.: $_"; last if /^DISKPART> $/ && ++$count == 2; } } { my $pid = open2(my $fr_chld, my $to_chld, 'diskpart'); #print $to_chld "\n"; #wait_for_prompt($to_chld, $fr_chld); print $to_chld "list disk\n"; wait_for_prompt($to_chld, $fr_chld); print $to_chld "exit\n"; wait_for_prompt($to_chld, $fr_chld); close $to_chld; waitpid($pid, 0); }