Receive-Job output to a file












3















how can i output the receive-job result to a file?



I tried this but it doesn't work :



$log = "C:springfieldcitrixCitrixAutomation.log"
Get-Job | Receive-Job | Out-File $log


I also tried to save the output of Get-Job | Receive-Job to a variable, but it doesn't work..



$log = "C:springfieldcitrixCitrixAutomation.log"
$getjobarr = @()
Function LogWrite
{
Param ([array]$logstring)
$logstring | Out-File $log -Append
}

$getjobarr += Get-Job | Receive-Job

LogWrite $getjobarr


I think that Get-Job and Receive-Job can only output to console only, so how can i achieve it?



Thanks for your help










share|improve this question



























    3















    how can i output the receive-job result to a file?



    I tried this but it doesn't work :



    $log = "C:springfieldcitrixCitrixAutomation.log"
    Get-Job | Receive-Job | Out-File $log


    I also tried to save the output of Get-Job | Receive-Job to a variable, but it doesn't work..



    $log = "C:springfieldcitrixCitrixAutomation.log"
    $getjobarr = @()
    Function LogWrite
    {
    Param ([array]$logstring)
    $logstring | Out-File $log -Append
    }

    $getjobarr += Get-Job | Receive-Job

    LogWrite $getjobarr


    I think that Get-Job and Receive-Job can only output to console only, so how can i achieve it?



    Thanks for your help










    share|improve this question

























      3












      3








      3








      how can i output the receive-job result to a file?



      I tried this but it doesn't work :



      $log = "C:springfieldcitrixCitrixAutomation.log"
      Get-Job | Receive-Job | Out-File $log


      I also tried to save the output of Get-Job | Receive-Job to a variable, but it doesn't work..



      $log = "C:springfieldcitrixCitrixAutomation.log"
      $getjobarr = @()
      Function LogWrite
      {
      Param ([array]$logstring)
      $logstring | Out-File $log -Append
      }

      $getjobarr += Get-Job | Receive-Job

      LogWrite $getjobarr


      I think that Get-Job and Receive-Job can only output to console only, so how can i achieve it?



      Thanks for your help










      share|improve this question














      how can i output the receive-job result to a file?



      I tried this but it doesn't work :



      $log = "C:springfieldcitrixCitrixAutomation.log"
      Get-Job | Receive-Job | Out-File $log


      I also tried to save the output of Get-Job | Receive-Job to a variable, but it doesn't work..



      $log = "C:springfieldcitrixCitrixAutomation.log"
      $getjobarr = @()
      Function LogWrite
      {
      Param ([array]$logstring)
      $logstring | Out-File $log -Append
      }

      $getjobarr += Get-Job | Receive-Job

      LogWrite $getjobarr


      I think that Get-Job and Receive-Job can only output to console only, so how can i achieve it?



      Thanks for your help







      powershell powershell-v3.0






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 12 '14 at 15:19









      Adeel ASIFAdeel ASIF

      1,20962037




      1,20962037
























          2 Answers
          2






          active

          oldest

          votes


















          5














          Job "output" can be redirected to a file e.g.:



          PS> Start-Job {Get-ChildItem C:userskeith}

          Id Name PSJobTypeName State HasMoreData Location Command
          -- ---- ------------- ----- ----------- -------- -------
          2 Job2 BackgroundJob Running True localhost Get-ChildItem C:users...


          PS> Receive-Job -id 2 | Out-File job.log
          PS> gc .job.log


          Directory: C:userskeith


          Mode LastWriteTime Length Name
          ---- ------------- ------ ----
          d---- 1/21/2014 8:24 PM <DIR> .ssh
          d---- 9/9/2014 10:00 PM <DIR> Bin
          d-r-- 9/11/2014 9:20 PM <DIR> Contacts
          d-r-- 9/11/2014 9:20 PM <DIR> Desktop


          If whatever you're running in the job writes to host then yeah, you're hosed. If you control that, use write-output instead of write-host. Also, make sure you wait until each job is complete before receiving its output unless you can sit in a loop wait until the job state changes to Completed or Failed. You can use Wait-Job to wait for a job to finish before asking for its output.






          share|improve this answer

































            0














            To add to Keith's answer:

            I spent a day banging my head against this for a complex script with a dozen functions. My Out-File would only output to Console, or to Console & txt file, but I could never hide it from the Console.



            My fix had two steps:

            1. *>&1 on the Receive-Job cmdlet



            $logfile = "$env:LOCALAPPDATAtempScriptResult.txt"
            Get-Job | foreach-object {
            Receive-Job -name $_.name *>&1 >> $logfile
            "`r`n=====================================" | Out-File -FilePath $logfile -Append
            }


            The *>&1 was key. It forced all data streams (Information and Error) into the output so everything appeared in in the log file

            (You can pipe to Out-File instead of >> if you would prefer to write it out.)

            2. Find & Replace all Write-Host cmdlets with Write-Output

            Most of my functions had Write-Host explanations for error messages. Even when I used *>&1 to redirect the stream it caused the output to show on the Console. Replacing with Write-Output fixed that.



            Thanks Keith for the critical info I could not find anywhere else! :D

            (Works on PSVersion v5.1, Win10)






            share|improve this answer

























              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%2f25811604%2freceive-job-output-to-a-file%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              Job "output" can be redirected to a file e.g.:



              PS> Start-Job {Get-ChildItem C:userskeith}

              Id Name PSJobTypeName State HasMoreData Location Command
              -- ---- ------------- ----- ----------- -------- -------
              2 Job2 BackgroundJob Running True localhost Get-ChildItem C:users...


              PS> Receive-Job -id 2 | Out-File job.log
              PS> gc .job.log


              Directory: C:userskeith


              Mode LastWriteTime Length Name
              ---- ------------- ------ ----
              d---- 1/21/2014 8:24 PM <DIR> .ssh
              d---- 9/9/2014 10:00 PM <DIR> Bin
              d-r-- 9/11/2014 9:20 PM <DIR> Contacts
              d-r-- 9/11/2014 9:20 PM <DIR> Desktop


              If whatever you're running in the job writes to host then yeah, you're hosed. If you control that, use write-output instead of write-host. Also, make sure you wait until each job is complete before receiving its output unless you can sit in a loop wait until the job state changes to Completed or Failed. You can use Wait-Job to wait for a job to finish before asking for its output.






              share|improve this answer






























                5














                Job "output" can be redirected to a file e.g.:



                PS> Start-Job {Get-ChildItem C:userskeith}

                Id Name PSJobTypeName State HasMoreData Location Command
                -- ---- ------------- ----- ----------- -------- -------
                2 Job2 BackgroundJob Running True localhost Get-ChildItem C:users...


                PS> Receive-Job -id 2 | Out-File job.log
                PS> gc .job.log


                Directory: C:userskeith


                Mode LastWriteTime Length Name
                ---- ------------- ------ ----
                d---- 1/21/2014 8:24 PM <DIR> .ssh
                d---- 9/9/2014 10:00 PM <DIR> Bin
                d-r-- 9/11/2014 9:20 PM <DIR> Contacts
                d-r-- 9/11/2014 9:20 PM <DIR> Desktop


                If whatever you're running in the job writes to host then yeah, you're hosed. If you control that, use write-output instead of write-host. Also, make sure you wait until each job is complete before receiving its output unless you can sit in a loop wait until the job state changes to Completed or Failed. You can use Wait-Job to wait for a job to finish before asking for its output.






                share|improve this answer




























                  5












                  5








                  5







                  Job "output" can be redirected to a file e.g.:



                  PS> Start-Job {Get-ChildItem C:userskeith}

                  Id Name PSJobTypeName State HasMoreData Location Command
                  -- ---- ------------- ----- ----------- -------- -------
                  2 Job2 BackgroundJob Running True localhost Get-ChildItem C:users...


                  PS> Receive-Job -id 2 | Out-File job.log
                  PS> gc .job.log


                  Directory: C:userskeith


                  Mode LastWriteTime Length Name
                  ---- ------------- ------ ----
                  d---- 1/21/2014 8:24 PM <DIR> .ssh
                  d---- 9/9/2014 10:00 PM <DIR> Bin
                  d-r-- 9/11/2014 9:20 PM <DIR> Contacts
                  d-r-- 9/11/2014 9:20 PM <DIR> Desktop


                  If whatever you're running in the job writes to host then yeah, you're hosed. If you control that, use write-output instead of write-host. Also, make sure you wait until each job is complete before receiving its output unless you can sit in a loop wait until the job state changes to Completed or Failed. You can use Wait-Job to wait for a job to finish before asking for its output.






                  share|improve this answer















                  Job "output" can be redirected to a file e.g.:



                  PS> Start-Job {Get-ChildItem C:userskeith}

                  Id Name PSJobTypeName State HasMoreData Location Command
                  -- ---- ------------- ----- ----------- -------- -------
                  2 Job2 BackgroundJob Running True localhost Get-ChildItem C:users...


                  PS> Receive-Job -id 2 | Out-File job.log
                  PS> gc .job.log


                  Directory: C:userskeith


                  Mode LastWriteTime Length Name
                  ---- ------------- ------ ----
                  d---- 1/21/2014 8:24 PM <DIR> .ssh
                  d---- 9/9/2014 10:00 PM <DIR> Bin
                  d-r-- 9/11/2014 9:20 PM <DIR> Contacts
                  d-r-- 9/11/2014 9:20 PM <DIR> Desktop


                  If whatever you're running in the job writes to host then yeah, you're hosed. If you control that, use write-output instead of write-host. Also, make sure you wait until each job is complete before receiving its output unless you can sit in a loop wait until the job state changes to Completed or Failed. You can use Wait-Job to wait for a job to finish before asking for its output.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 13 '14 at 4:16

























                  answered Sep 12 '14 at 16:36









                  Keith HillKeith Hill

                  144k24263302




                  144k24263302

























                      0














                      To add to Keith's answer:

                      I spent a day banging my head against this for a complex script with a dozen functions. My Out-File would only output to Console, or to Console & txt file, but I could never hide it from the Console.



                      My fix had two steps:

                      1. *>&1 on the Receive-Job cmdlet



                      $logfile = "$env:LOCALAPPDATAtempScriptResult.txt"
                      Get-Job | foreach-object {
                      Receive-Job -name $_.name *>&1 >> $logfile
                      "`r`n=====================================" | Out-File -FilePath $logfile -Append
                      }


                      The *>&1 was key. It forced all data streams (Information and Error) into the output so everything appeared in in the log file

                      (You can pipe to Out-File instead of >> if you would prefer to write it out.)

                      2. Find & Replace all Write-Host cmdlets with Write-Output

                      Most of my functions had Write-Host explanations for error messages. Even when I used *>&1 to redirect the stream it caused the output to show on the Console. Replacing with Write-Output fixed that.



                      Thanks Keith for the critical info I could not find anywhere else! :D

                      (Works on PSVersion v5.1, Win10)






                      share|improve this answer






























                        0














                        To add to Keith's answer:

                        I spent a day banging my head against this for a complex script with a dozen functions. My Out-File would only output to Console, or to Console & txt file, but I could never hide it from the Console.



                        My fix had two steps:

                        1. *>&1 on the Receive-Job cmdlet



                        $logfile = "$env:LOCALAPPDATAtempScriptResult.txt"
                        Get-Job | foreach-object {
                        Receive-Job -name $_.name *>&1 >> $logfile
                        "`r`n=====================================" | Out-File -FilePath $logfile -Append
                        }


                        The *>&1 was key. It forced all data streams (Information and Error) into the output so everything appeared in in the log file

                        (You can pipe to Out-File instead of >> if you would prefer to write it out.)

                        2. Find & Replace all Write-Host cmdlets with Write-Output

                        Most of my functions had Write-Host explanations for error messages. Even when I used *>&1 to redirect the stream it caused the output to show on the Console. Replacing with Write-Output fixed that.



                        Thanks Keith for the critical info I could not find anywhere else! :D

                        (Works on PSVersion v5.1, Win10)






                        share|improve this answer




























                          0












                          0








                          0







                          To add to Keith's answer:

                          I spent a day banging my head against this for a complex script with a dozen functions. My Out-File would only output to Console, or to Console & txt file, but I could never hide it from the Console.



                          My fix had two steps:

                          1. *>&1 on the Receive-Job cmdlet



                          $logfile = "$env:LOCALAPPDATAtempScriptResult.txt"
                          Get-Job | foreach-object {
                          Receive-Job -name $_.name *>&1 >> $logfile
                          "`r`n=====================================" | Out-File -FilePath $logfile -Append
                          }


                          The *>&1 was key. It forced all data streams (Information and Error) into the output so everything appeared in in the log file

                          (You can pipe to Out-File instead of >> if you would prefer to write it out.)

                          2. Find & Replace all Write-Host cmdlets with Write-Output

                          Most of my functions had Write-Host explanations for error messages. Even when I used *>&1 to redirect the stream it caused the output to show on the Console. Replacing with Write-Output fixed that.



                          Thanks Keith for the critical info I could not find anywhere else! :D

                          (Works on PSVersion v5.1, Win10)






                          share|improve this answer















                          To add to Keith's answer:

                          I spent a day banging my head against this for a complex script with a dozen functions. My Out-File would only output to Console, or to Console & txt file, but I could never hide it from the Console.



                          My fix had two steps:

                          1. *>&1 on the Receive-Job cmdlet



                          $logfile = "$env:LOCALAPPDATAtempScriptResult.txt"
                          Get-Job | foreach-object {
                          Receive-Job -name $_.name *>&1 >> $logfile
                          "`r`n=====================================" | Out-File -FilePath $logfile -Append
                          }


                          The *>&1 was key. It forced all data streams (Information and Error) into the output so everything appeared in in the log file

                          (You can pipe to Out-File instead of >> if you would prefer to write it out.)

                          2. Find & Replace all Write-Host cmdlets with Write-Output

                          Most of my functions had Write-Host explanations for error messages. Even when I used *>&1 to redirect the stream it caused the output to show on the Console. Replacing with Write-Output fixed that.



                          Thanks Keith for the critical info I could not find anywhere else! :D

                          (Works on PSVersion v5.1, Win10)







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 14 '18 at 20:01

























                          answered Nov 14 '18 at 18:57









                          R_C_IIIR_C_III

                          32




                          32






























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25811604%2freceive-job-output-to-a-file%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

                              Florida Star v. B. J. F.

                              Danny Elfman

                              Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues