0

Here is my issue. I run a command line application within a PowerShell script using the Call Operator (i.e. the & symbol), such that it is:

& $PathToExe /switch:"lots of args" /anotherswitch:"with more parms" /etc:"$WithAlsoVariables"

This works well, in fact a little too well. The process is called but the script will continue whilst the process is still running. In the most part, this is OK, as the rest of the script doesn't rely on the output of this process, but I need to clean up after the process has finished.

I was possibly looking at the Start-ProcessCmdLet with the -wait parameter, which I'm sure would do what I need, but also slow down things a little. The EXE could be run in parallel, although I would need to know when the process was finished. I could run a simple Do-While loop at the end, checking to see if the process had finished before clean up, but I don't know how to get the specific Process ID for the invoked EXE. I can't check simply for the process name as there could be other manually started processes of the same name running.

I have spent a time trawling some site (such has here) but nothing has popped up yet. Help would be welcome.

I have tried:

$proc = & $PathToExe /switch:"lots of args" /anotherswitch:"with more parms" /etc:"$WithAlsoVariables"

And although the process starts, the arguments are now incorrect as it stops immediately

5
  • Actually, after a thought, I think I can use something like $proc = Start-Process without the -wait parameter, then loop on $proc.id. Tis late now will try tomorrow.
    – Swinster
    May 7 '16 at 2:26
  • Hmm, not quite. So I tried $ArgList = "/switch:'"lots of args'" /anotherswitch:'"with more parms'" /etc:'"$WithAlsoVariables'"(the single quotes are really back ticks to escape the double quotes, but it messes up Markdown here), then $proc = Start-Process $PathToExe -ArgumentList $ArgList, and again the process starts, but stops immediately
    – Swinster
    May 7 '16 at 2:44
  • Arghh. Right, got the above working with silly use of quotes, but I now want to check for the Process ID with $ProcessActive = Get-Process -Id $proc.Id -ErrorAction SilentlyContinue, but $proc appears to be null, even when the process is still running, so I get a Get-Process : Cannot bind argument to parameter 'Id' because it is null. . Hmm
    – Swinster
    May 7 '16 at 3:15
  • When using Get-Process to get the process ID of the running process in addition to filter for the name, you can also filter for the StartTime which you know because you just started it. Use a range of two seconds should be good enough. May 7 '16 at 10:26
  • Hey Peter, thank for this. although I woke up this morning with a fresher head and realised I had not added the -PassThrough parameter to my Start-Process CmdLet. Now I can simply Run Wait-Process $proc.id at the end of the script, before I run my clean up code.
    – Swinster
    May 7 '16 at 13:55
1

So, after a few false starts as seen in the commentary above, I thought I would clarify the answer that I have come up with (I'm sure there are other ways to achieve this):

$ArgList = """/switch:""lots of args"" /anotherswitch:""with more parms" /etc:""$WithAlsoVariables"""
$proc = Start-Process $PathToExe -ArgumentList $ArgList -PassThru
.
.
# The "-ErrorAction SilentlyContinue" parameter stops an error being thrown if the process has alread finished.
Wait-Process $proc.id -ErrorAction SilentlyContinue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.