6

I have to automate test cases.

Tasks:-

  1. Step:-Open administrative command prompt from powershell.
  2. Step:-Execute a batch file on the administrative command prompt.
  3. Step:-Batch file includes some set of commands, including a execution of an exe.

    For Example:- runas /user:administrator /savecred someCmd.exe >>D:\output.txt

  4. Step:-Capture output of the exe in a variable for verification of the output.

i have used " start-process -verb runas cmd.exe $param" cmdlet for opening administrative command prompt(Step 1). Where $param contains the batch file to be executed.

Problem Statement:- When batch file executes the runas command mentioned above, it opens a new command prompt and the output is displayed in the prompt and it closes itself. i am not able to capture the output(not getting written in the output.txt) based on which i have to do some verification.

AJ-
  • 109
  • 1
  • 2
  • 10

5 Answers5

2

you can use the the output redirection from the batch :

$params="/C ipconfig /all 2>&1 >>c:\temp\test.txt" 
start-process -verb runas cmd.exe $params 
gc c:\temp\test.txt
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
0

Solution

  1. Open administrative command prompt from powershell.
  2. executed a batch file in the runas command. For example: runas /user:administrator /savecred mybatch.bat
  3. Batch file includes some set of commands, including a execution of an exe. For example someCmd.exe >>D:\output.txt
  4. Capture output of the exe in a variable for verification of the output.

Now the output was captured and was written into a file. My target was to capture the output of the command and this was the solution through which I solved it.

Mike G
  • 4,232
  • 9
  • 40
  • 66
AJ-
  • 109
  • 1
  • 2
  • 10
0

I ended up creating a wrapper batch file OutputWrapper.bat that takes at least two arguments:
1) output file
2) command
3) [optional] arguments

@ECHO OFF

IF "%2" == "" GOTO usage

SET OUTPUTFILE=%1
SET COMMAND=%2
SET ARGS=

SHIFT /2

:loop1
IF "%2"=="" GOTO exec
SET ARGS=%ARGS% %2
SHIFT
GOTO loop1

:exec
ECHO Command [%COMMAND%]
ECHO Arguments [%ARGS%]
ECHO Output file [%OUTPUTFILE%]

%COMMAND%%ARGS% > %OUTPUTFILE% 2>&1
GOTO end

:usage
ECHO Usage: %~nx0 outputfile command [arguments]

:end

and calling it from PowerShell like this:

$outFile = "C:\Temp\Deploy.out";

Start-Process -FilePath .\OutputWrapper.bat -ArgumentList "$outfile","whoami.exe","/priv" -Verb RunAs -Wait

Get-Content $outFile;
Sheva
  • 99
  • 1
  • 9
0

I had the same problem and solved it by the use of gsudo. It let me run the elevated command and tunneled the output back from it.

gsudo {command-to-execute}
tim
  • 2,530
  • 3
  • 26
  • 45
0

Improvement on Loïc MICHEL's answer, as without -Wait, it's likely that Get-Content will run before the process has finished. As the output isn't written until the process ends, Get-Content fails as the file does not exist.

$param = "ipconfig /all"
$args = "/C $param 2>&1 > C:\temp\test.txt"
Start-Process -FilePath cmd.exe -ArgumentList $args -Verb RunAs -Wait
Get-Content -Path C:\temp\test.txt

Alternatively, using powershell.exe instead and a random file in the temporary directory for the OS:

$CommandWithParameters = "gpresult /scope computer /z"

$OutputFile = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath ([System.IO.Path]::GetRandomFileName())
$Arguments = ("{0} 2>&1 > {1}" -f $CommandWithParameters, $OutputFile)
Start-Process -FilePath powershell.exe -ArgumentList $Arguments -Verb RunAs -Wait

Get-Content -Path $OutputFile

Using powershell.exe will save the output file using UCS-2 LE BOM encoding. If you use cmd.exe, the encoding will be ANSI.

Mortein
  • 113
  • 8