How to Run Python Script from Remote Machine via Powershell Asynchronously?












0














I have a powershell function that only works synchronously. I would like to update this function so that I can fire multiple calls to a Python script in parallel. Does anybody have any insight how to call a Python script asynchronously using powershell keeping in mind that each Python Script is essentially a while loop that doesn't end until 24 hours passes. The powershell function takes in a remote machine, python virtual environment, path to the python script and the arguments.



Here's what has been done so far:



function Run-Remote($computerName, $pname, $scriptPath, $pargs)
{
$cred = Get-QRemote-Credential
Invoke-Command -ComputerName $computerName -Credential $cred -ScriptBlock {powershell -c "$Using:pname '$Using:scriptPath' $Using:pargs"} -ConfigurationName QRemoteConfiguration
}









share|improve this question





























    0














    I have a powershell function that only works synchronously. I would like to update this function so that I can fire multiple calls to a Python script in parallel. Does anybody have any insight how to call a Python script asynchronously using powershell keeping in mind that each Python Script is essentially a while loop that doesn't end until 24 hours passes. The powershell function takes in a remote machine, python virtual environment, path to the python script and the arguments.



    Here's what has been done so far:



    function Run-Remote($computerName, $pname, $scriptPath, $pargs)
    {
    $cred = Get-QRemote-Credential
    Invoke-Command -ComputerName $computerName -Credential $cred -ScriptBlock {powershell -c "$Using:pname '$Using:scriptPath' $Using:pargs"} -ConfigurationName QRemoteConfiguration
    }









    share|improve this question



























      0












      0








      0







      I have a powershell function that only works synchronously. I would like to update this function so that I can fire multiple calls to a Python script in parallel. Does anybody have any insight how to call a Python script asynchronously using powershell keeping in mind that each Python Script is essentially a while loop that doesn't end until 24 hours passes. The powershell function takes in a remote machine, python virtual environment, path to the python script and the arguments.



      Here's what has been done so far:



      function Run-Remote($computerName, $pname, $scriptPath, $pargs)
      {
      $cred = Get-QRemote-Credential
      Invoke-Command -ComputerName $computerName -Credential $cred -ScriptBlock {powershell -c "$Using:pname '$Using:scriptPath' $Using:pargs"} -ConfigurationName QRemoteConfiguration
      }









      share|improve this question















      I have a powershell function that only works synchronously. I would like to update this function so that I can fire multiple calls to a Python script in parallel. Does anybody have any insight how to call a Python script asynchronously using powershell keeping in mind that each Python Script is essentially a while loop that doesn't end until 24 hours passes. The powershell function takes in a remote machine, python virtual environment, path to the python script and the arguments.



      Here's what has been done so far:



      function Run-Remote($computerName, $pname, $scriptPath, $pargs)
      {
      $cred = Get-QRemote-Credential
      Invoke-Command -ComputerName $computerName -Credential $cred -ScriptBlock {powershell -c "$Using:pname '$Using:scriptPath' $Using:pargs"} -ConfigurationName QRemoteConfiguration
      }






      python function powershell parallel-processing asynchronously






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 4:01

























      asked Nov 21 at 3:03









      Riley Hun

      7731922




      7731922
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Really simple fix, just add "-AsJob" to the invoke command. This will get powershell to spin up a new process for each command that you send. Then Use "Get-Job | Wait-Job" at the end of the script to wait for all the processes to finish. And "Get-Job | Receive-Job" to get any data back.



          I recommend reading about powershell jobs they are supper useful but unintuitive.



          function Run-Remote($computerName, $pname, $scriptPath, $pargs)
          {
          $cred = Get-QRemote-Credential
          Invoke-Command -AsJob -ComputerName $computerName -Credential $cred -ScriptBlock {powershell -c "$Using:pname '$Using:scriptPath' $Using:pargs"} -ConfigurationName QRemoteConfiguration
          }

          Get-Job | Wait-Job





          share|improve this answer





















          • Excellent! Thanks so much. I'll give it a try.
            – Riley Hun
            Nov 21 at 3:59










          • I tried using AsJob parameter, but it didn't generate a python process in the task machine.
            – Riley Hun
            Nov 22 at 18:05










          • That's very odd, did the job complete properly? The only thing I can think of is is the script exited before the job completed
            – BaronW
            Nov 23 at 20:12










          • it did not. I used -indisconnectedsession instead. Do you foresee any issues with using that instead?
            – Riley Hun
            Nov 26 at 16:33











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404700%2fhow-to-run-python-script-from-remote-machine-via-powershell-asynchronously%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Really simple fix, just add "-AsJob" to the invoke command. This will get powershell to spin up a new process for each command that you send. Then Use "Get-Job | Wait-Job" at the end of the script to wait for all the processes to finish. And "Get-Job | Receive-Job" to get any data back.



          I recommend reading about powershell jobs they are supper useful but unintuitive.



          function Run-Remote($computerName, $pname, $scriptPath, $pargs)
          {
          $cred = Get-QRemote-Credential
          Invoke-Command -AsJob -ComputerName $computerName -Credential $cred -ScriptBlock {powershell -c "$Using:pname '$Using:scriptPath' $Using:pargs"} -ConfigurationName QRemoteConfiguration
          }

          Get-Job | Wait-Job





          share|improve this answer





















          • Excellent! Thanks so much. I'll give it a try.
            – Riley Hun
            Nov 21 at 3:59










          • I tried using AsJob parameter, but it didn't generate a python process in the task machine.
            – Riley Hun
            Nov 22 at 18:05










          • That's very odd, did the job complete properly? The only thing I can think of is is the script exited before the job completed
            – BaronW
            Nov 23 at 20:12










          • it did not. I used -indisconnectedsession instead. Do you foresee any issues with using that instead?
            – Riley Hun
            Nov 26 at 16:33
















          1














          Really simple fix, just add "-AsJob" to the invoke command. This will get powershell to spin up a new process for each command that you send. Then Use "Get-Job | Wait-Job" at the end of the script to wait for all the processes to finish. And "Get-Job | Receive-Job" to get any data back.



          I recommend reading about powershell jobs they are supper useful but unintuitive.



          function Run-Remote($computerName, $pname, $scriptPath, $pargs)
          {
          $cred = Get-QRemote-Credential
          Invoke-Command -AsJob -ComputerName $computerName -Credential $cred -ScriptBlock {powershell -c "$Using:pname '$Using:scriptPath' $Using:pargs"} -ConfigurationName QRemoteConfiguration
          }

          Get-Job | Wait-Job





          share|improve this answer





















          • Excellent! Thanks so much. I'll give it a try.
            – Riley Hun
            Nov 21 at 3:59










          • I tried using AsJob parameter, but it didn't generate a python process in the task machine.
            – Riley Hun
            Nov 22 at 18:05










          • That's very odd, did the job complete properly? The only thing I can think of is is the script exited before the job completed
            – BaronW
            Nov 23 at 20:12










          • it did not. I used -indisconnectedsession instead. Do you foresee any issues with using that instead?
            – Riley Hun
            Nov 26 at 16:33














          1












          1








          1






          Really simple fix, just add "-AsJob" to the invoke command. This will get powershell to spin up a new process for each command that you send. Then Use "Get-Job | Wait-Job" at the end of the script to wait for all the processes to finish. And "Get-Job | Receive-Job" to get any data back.



          I recommend reading about powershell jobs they are supper useful but unintuitive.



          function Run-Remote($computerName, $pname, $scriptPath, $pargs)
          {
          $cred = Get-QRemote-Credential
          Invoke-Command -AsJob -ComputerName $computerName -Credential $cred -ScriptBlock {powershell -c "$Using:pname '$Using:scriptPath' $Using:pargs"} -ConfigurationName QRemoteConfiguration
          }

          Get-Job | Wait-Job





          share|improve this answer












          Really simple fix, just add "-AsJob" to the invoke command. This will get powershell to spin up a new process for each command that you send. Then Use "Get-Job | Wait-Job" at the end of the script to wait for all the processes to finish. And "Get-Job | Receive-Job" to get any data back.



          I recommend reading about powershell jobs they are supper useful but unintuitive.



          function Run-Remote($computerName, $pname, $scriptPath, $pargs)
          {
          $cred = Get-QRemote-Credential
          Invoke-Command -AsJob -ComputerName $computerName -Credential $cred -ScriptBlock {powershell -c "$Using:pname '$Using:scriptPath' $Using:pargs"} -ConfigurationName QRemoteConfiguration
          }

          Get-Job | Wait-Job






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 3:38









          BaronW

          1215




          1215












          • Excellent! Thanks so much. I'll give it a try.
            – Riley Hun
            Nov 21 at 3:59










          • I tried using AsJob parameter, but it didn't generate a python process in the task machine.
            – Riley Hun
            Nov 22 at 18:05










          • That's very odd, did the job complete properly? The only thing I can think of is is the script exited before the job completed
            – BaronW
            Nov 23 at 20:12










          • it did not. I used -indisconnectedsession instead. Do you foresee any issues with using that instead?
            – Riley Hun
            Nov 26 at 16:33


















          • Excellent! Thanks so much. I'll give it a try.
            – Riley Hun
            Nov 21 at 3:59










          • I tried using AsJob parameter, but it didn't generate a python process in the task machine.
            – Riley Hun
            Nov 22 at 18:05










          • That's very odd, did the job complete properly? The only thing I can think of is is the script exited before the job completed
            – BaronW
            Nov 23 at 20:12










          • it did not. I used -indisconnectedsession instead. Do you foresee any issues with using that instead?
            – Riley Hun
            Nov 26 at 16:33
















          Excellent! Thanks so much. I'll give it a try.
          – Riley Hun
          Nov 21 at 3:59




          Excellent! Thanks so much. I'll give it a try.
          – Riley Hun
          Nov 21 at 3:59












          I tried using AsJob parameter, but it didn't generate a python process in the task machine.
          – Riley Hun
          Nov 22 at 18:05




          I tried using AsJob parameter, but it didn't generate a python process in the task machine.
          – Riley Hun
          Nov 22 at 18:05












          That's very odd, did the job complete properly? The only thing I can think of is is the script exited before the job completed
          – BaronW
          Nov 23 at 20:12




          That's very odd, did the job complete properly? The only thing I can think of is is the script exited before the job completed
          – BaronW
          Nov 23 at 20:12












          it did not. I used -indisconnectedsession instead. Do you foresee any issues with using that instead?
          – Riley Hun
          Nov 26 at 16:33




          it did not. I used -indisconnectedsession instead. Do you foresee any issues with using that instead?
          – Riley Hun
          Nov 26 at 16:33


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404700%2fhow-to-run-python-script-from-remote-machine-via-powershell-asynchronously%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'