This commit is contained in:
Quentin Garnier 2014-02-12 18:16:27 +01:00
parent 6f2f880db0
commit bfe6c6181c
1 changed files with 70 additions and 52 deletions

View File

@ -201,6 +201,15 @@ sub execute_winshell_commands {
$node = $data->root()->add($namespace, 'DesiredStream', 'stdout stderr')
or $self->internal_exit(msg => 'Could not create XmlDoc');
$node->attr_add(undef, 'CommandId', $command_id);
my $timeout_global = 30; #seconds
my $wait_timeout_done = 0;
my $loop_out = 1;
my ($current_stdout, $current_stderr) = ('', '');
while ($loop_out == 1 && $wait_timeout_done < $timeout_global) {
$result = $self->{client}->invoke($client_options, $uri, 'http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive',
$data);
return undef if ($self->handle_dialog_fault(result => $result, msg => 'Invoke failed: ', dont_quit => $dont_quit));
@ -226,12 +235,10 @@ sub execute_winshell_commands {
next if (!defined($output) || $output eq '');
if ($stream_type eq 'stderr') {
$command_result->{$command->{label}}->{stderr} = '' if (!defined($command_result->{$command->{label}}->{stderr}));
$command_result->{$command->{label}}->{stderr} .= $output;
$current_stderr .= $output;
}
if ($stream_type eq 'stdout') {
$command_result->{$command->{label}}->{stdout} = '' if (!defined($command_result->{$command->{label}}->{stdout}));
$command_result->{$command->{label}}->{stdout} .= $output;
$current_stdout .= $output;
}
}
if ($node->name() eq 'CommandState') {
@ -247,12 +254,15 @@ sub execute_winshell_commands {
} else {
$self->internal_exit(msg => "No exit code for 'done' command");
}
$loop_out = 0;
} elsif ($state eq 'http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Running') {
# mmmm should put an error
return undef if ($self->handle_dialog_fault(result => $result, msg => 'Command still running...', dont_quit => $dont_quit));
# we wait
$wait_timeout_done += 3;
sleep 3;
} elsif ($state eq 'http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Pending') {
# no-op
# WinRM 1.1 sends this with ExitCode:0
$loop_out = 0;
} else {
# unknown
$self->internal_exit(msg => 'Unknown command state: ' . $state);
@ -261,6 +271,14 @@ sub execute_winshell_commands {
}
}
$current_stderr =~ s/\r//mg;
$current_stdout =~ s/\r//mg;
$command_result->{$command->{label}}->{stderr} = $current_stderr if ($current_stderr ne '');
$command_result->{$command->{label}}->{stdout} = $current_stdout if ($current_stdout ne '');
#
# terminate shell command
#