2

I am trying to execute an .exe file with PowerShell by doing:

$myExe = Join-Path $scriptDir "\bin\my\myProgram.exe" 
 $job = Start-Job -ScriptBlock{ 
        & $myExe }

But it doesn't execute. When in type in the file path in $myExe, the program works as expected, but when I use the above code, nothing happens. Can someone explain what I'm doing wrong; or, why I'm not getting the expected results?

To be clear, the expected result (at this point) is to get a firewall warning. Again, when I type in the path directly to the PS console, the firewall warning comes up, but using the above code, nothing seems to happen. I've also tried to check the status using Get-Process but the result is the $job variable, saying it's running.

UPDATE:

Running:

$myExe = Join-Path $scriptDir "\bin\my\myProgram.exe"
& $prndExe

Causes PowerShell to hang

UPDATE 2:

When I do the following, the program works and the process starts:

 Start-Job -ScriptBlock{ & "C:\path\Music\source\p\android\test\Automation\bin\mym\myProgram.exe"}

But using the above code doesn't. This leads me to believe I'm doing something wrong with the Start-Job cmdlet?

14
  • Have you debugged [$myExe] to make sure the full path is correct? Also, Start-Job is for background processes. Is this supposed to be a background process? Commented Jul 30, 2013 at 19:52
  • so & $myExe works outside of the start-job scriptblock? Commented Jul 30, 2013 at 19:53
  • What is the value of $myExe when its used in Start-Job? Is it a valid path? Have you verified? Commented Jul 30, 2013 at 19:55
  • @TheOptimusPrimus I am writing the content of $myExe to the console, and then taking that output and putting it into the console to get it to 'work'. When I use & $myExe outside of the scriptBlock the program just hangs. Commented Jul 30, 2013 at 19:56
  • Yes, the path is valid: C:\my\Music\source\lk\android\test\Automation\bin\myfolder\myProgram.exe. I have taken that output and put it into the console, and had the expected result. Would running this as a background process make it run differently? Commented Jul 30, 2013 at 19:58

1 Answer 1

3

So the solution to this issue (one that is NOT listed in the documentation, and should be) is that if you are using variables inside ScriptBlock you have to use the ArgumentList and pass in those variables.

So to get my scriptblock working, I did:

$job = Start-Job -ScriptBlock{  
    $myExe = $args[0]

    & $myExe
} -ArgumentList @($myExe)
Sign up to request clarification or add additional context in comments.

3 Comments

I'm confused...the first line sets $myExe to a path to an exe file ($myExe = Join-Path $scriptDir "\bin\my\myProgram.exe"), and your entire script block is & $myExe...so where are the variables?
That is my point. When I set $myExe it is set outside the script block, so in order for me to use it, I have to pass it in as a parameter. Don't ask me why, but that is how it's working.
You have to pass it as a parameter because jobs run in a different process. The variables don't exist there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.