locked
Run executable from powershell passing parameters and Return result RRS feed

  • Question

  • I want to run an external 'exe' program passing parameter from powershell. in command prompt it usually works

    as (c:\wsengineering\executables\myprocess.exe -x "val1" -y "val2" -z "val3").

    Most important is, i must have to get the return-result from the exe-process back to powershell, that's why i'm trying to use this piece of code to get the EXITCODE and STANDARDOUTPUT. BUT i am not getting any idea how to set the arguments in the $arg variable?

        $prog = "c:\wsengineering\executables\myprocess.exe"
        $arg = xxx
     
        $extjob = New-Object System.Diagnostics.Process
        $extjob.StartInfo = New-Object System.Diagnostics.ProcessStartInfo
        $returnexitcode = $false
     
        $extjob.StartInfo.FileName = $prog
        $extjob.StartInfo.Arguments = $arg
        $extjob.StartInfo.UseShellExecute = $shell
        $extjob.StartInfo.WindowStyle = 1
        $extjob.StartInfo.RedirectStandardOutput = $true
     
        $null = $extjob.Start()
        $extjob.WaitForExit()
        $returnexitcode = $extjob.ExitCode
        $resulttxt = $extjob.StandardOutput.ReadToEnd()

    So, can anyone please help on how to set the arguments in the $arg variable?
    Friday, January 20, 2012 1:52 AM

Answers

  • I have done this with

    $arguments = " -x 'val1' -y 'val2' -z 'val'"

    or, if you must evaluate the string with double quotes, escape them,

    $arguments = " -x `"val1 `"  -y  `" val2 `"  -z  `"val3`""

    Then, pass the $arguments (or $arg in your case) variable as you did to set the $extJob.StartInfo.Arguments property.

    • Marked as answer by ThePathFinder Saturday, January 21, 2012 4:32 PM
    Friday, January 20, 2012 4:33 PM

All replies

  • does this need to run in a separate process? If not, try this:

      $result = c:\wsengineering\executables\myprocess.exe -x "val1" -y "val2" -z "val3"
      write-output "exit code = $lastexitcode "
      write-output "result = $result"

     

    Friday, January 20, 2012 3:55 AM
  • I have done this with

    $arguments = " -x 'val1' -y 'val2' -z 'val'"

    or, if you must evaluate the string with double quotes, escape them,

    $arguments = " -x `"val1 `"  -y  `" val2 `"  -z  `"val3`""

    Then, pass the $arguments (or $arg in your case) variable as you did to set the $extJob.StartInfo.Arguments property.

    • Marked as answer by ThePathFinder Saturday, January 21, 2012 4:32 PM
    Friday, January 20, 2012 4:33 PM