Python popen wait command not behaving as expected
<p>I've a powershell script which calls an API and returns 0 or 99 based on if the POST request went through. My powershell code:</p>

<pre><code>try
{
    Invoke-RestMethod -uri $url -Method Post -Body $body -ContentType 'application/json' -ErrorAction Stop

    return 0
}
catch
{
    return 99
}
</code></pre>

<p>This is how I'm calling the script from my python script:</p>

<pre><code>req =    subprocess.Popen([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
                             '-ExecutionPolicy',
                             'Unrestricted',
                             './call.ps1',
                             param1, param2], cwd=os.getcwd())

    print("^^^^^^^")
    result = req.wait() #something is going wrong here
    print("****************")
    print(result)

    if result == 0:
        # do something
    else:
        # do something else
</code></pre>

<p>Now this is where its going wrong... even when the POST request is failing the "result" variable still has "0" and not "99". When I separately run the powershell script I can see that its returning 99 when the POST request is failing. So the problem is not with the powershell file it seems.</p>

<p>On debugging python file, this is what i see on console: (notice the order of print statements in the python code)</p>

<pre><code>^^^^^^^
99
****************
0
</code></pre>

<p>One thing that I don't understand is.. what is "99" doing in the output? I'm not even printing the result between "^^^^^" and "*****". So if the powershell script is returning 99, why does it change to 0 after "*****".</p>

<p>I'm really lost here. Please help.</p>

<p>The 99 is output created by PowerShell because you used <code>return</code> instead of <code>exit</code>. Because of that the script prints 99 and exits with an exit code of 0 (which is what <code>req.wait()</code> receives). Also, your PowerShell commandline isn't built in a way to actually return a proper exit code from a script, so even if you had used <code>exit</code> instead of <code>return</code> the process would only have returned 0 on success or 1 in case of an error.</p>

<p>To have PowerShell return a script's exit code you need to either return the exit code yourself</p>

<pre class="lang-none prettyprint-override"><code>powershell.exe -Command "&amp;{.\call.ps1; exit $LASTEXITCODE}"
</code></pre>

<p>or call the script using the <code>-File</code> parameter</p>

<pre class="lang-none prettyprint-override"><code>powershell.exe -File .\call.ps1
</code></pre>

<p>To fix the problem change your PowerShell code to this:</p>

<pre class="lang-bsh prettyprint-override"><code>try {
    Invoke-RestMethod -uri $url -Method Post -Body $body -ContentType 'application/json' -ErrorAction Stop
    exit 0
} catch {
    exit 99
}
</code></pre>

<p>and your Python code to this:</p>

<pre class="lang-py prettyprint-override"><code>req = subprocess.Popen([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
      '-ExecutionPolicy', 'Unrestricted',
      '-File', './test.ps1',
      param1, param2], cwd=os.getcwd())

print("^^^^^^^")
result = req.wait()
print("****************")
print(result)
</code></pre>

python powershell popen