Wait for PsExec calling cmd /c to finish in PowerShell
<p>I have a dynamically generated batch file that I push out to a remote PC and then execute it using PsExec. The issue I am facing is that as soon as that line is called, the PowerShell script moves on and doesn't wait for it to finish. Here is what I have:</p>

<pre><code>psexec -accepteula \\$Server -u Username -p Password-d -i 2 cmd /c C:\Call.bat
</code></pre>

<p><code>Call.bat</code> calls an executable on the remote machine with a few parameters passed in. This file is dynamically generated and is different every time, but can look like this:</p>

<pre><code>cd C:\20161212-175524
C:\20161212-175524\RE.exe /pa1 /pa2 /pa3 /pa4 /pa5 /pa6 /pa7 /pa8 /pa9 /pa10
</code></pre>

<p>The batch file needs to run as an interactive script as that specific user, but I need it to at least wait for the spawned process to finish. I have tried adding <code>2&gt;&amp;1</code> and <code>| Out-Null</code></p>

<p>Ideally, I would like to retrieve the exit code returned by the spawned process, but that may be too much.</p>