none
Run background jobs RRS feed

  • Question

  • I want to run background jobs on multiple computers using powershell remoting. I can do that. However I want to know how can I get the results on those from those remote computers to my computer and take some action if the job was failed or was successful. I have something like below:

    $all = new-pssession -Computername ( Get-Content C:\Servers.txt)
    Invoke-Command -Session $all -ScriptBlock { Get-eventlog system -EntryType Error} -AsJob

    I know I can use Receive job, however I want to know how to know the job failed from which remote computer/s and was successful from which remote computer/s?

    Tuesday, January 14, 2020 1:52 PM

All replies

  • The receive command will be clearly marked success or failed and the job name can be the computer name.

    Get-Content C:\Servers.txt |
        ForEach-Object{
            Invoke-Command  -AsJob -JobName $_ -ComputerName $_ -ScriptBlock { Get-eventlog system -EntryType Error}
        }

    You do not want to create sessions for this.


    \_(ツ)_/

    Tuesday, January 14, 2020 2:30 PM
    Moderator
  • The following should run faster:

    $sb = {
        Get-WinEvent -FilterHashtable @{Logname='System';Level=2} 
    }
    Get-Content C:\Servers.txt |
        ForEach-Object{
            Invoke-Command  -AsJob -JobName $_ -ComputerName $_ -ScriptBlock $sb
        }


    \_(ツ)_/

    Tuesday, January 14, 2020 2:38 PM
    Moderator
  • Thanks. This will run the jobs on the local computer or the remote computers. I want to distribute the load and run the jobs on the remote computers and then find which jobs failed or were successful from the local computer.

    Can you give an example of how I can receive the jobs from the remote computers and find for which computers the job failed and for which computers it was successful?


    • Edited by Admin66 Tuesday, January 14, 2020 11:35 PM
    Tuesday, January 14, 2020 11:34 PM
  • Invoke command runs on the remote computer no matter how you call it. The results will always be tagged with the computer name.

    I recommend reading the help for jobs and remoting to learn how this works.

    help about_Jobs
    help about_Remote_Jobs
    help Invoke-Command -online


    \_(ツ)_/

    Tuesday, January 14, 2020 11:49 PM
    Moderator
  • I read the examples in the about_remote_Jobs on the below link:

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_jobs?view=powershell-7

    It says:

    By default, background jobs run on the local computer. However, you can use several different procedures to run background jobs on remote computers.

    In the section:

    START A REMOTE JOB THAT RETURNS THE RESULTS TO THE LOCAL COMPUTER (ASJOB)

    The job name is Job1. When I am running the jobs on multiple computers. How do I know the name of the jobs which are created each time and on which computers the job failed and on which computers the job was successful. I want to run this as a scheduled task everyday.


    Wednesday, January 15, 2020 12:43 AM
  • Invoke command always runs on the remote computer. The local job is just a container for the session.  No CPU cycles are used locally and all jobs run in parallel.

    I think you need to learn a bit more about computer technology and PowerShell.  Take some time to read the full set of links more carefully. If you still don't understand then ask a specific question about the bit you do not understand.

    First understand what a job is then understand what Invoke-Command is doing.  It is executing a remote process on the remote system.

    Once you can understand how this works you will understand why I posted the code I posted for you.


    \_(ツ)_/

    Wednesday, January 15, 2020 2:12 AM
    Moderator
  • This is not what I meant. I understand what you are saying about invoke-command. My specific question is that:

    When running commands remotely as a background job. How can I receive jobs from the remote computers on my local computer and know for which remote computer the job failed and for which computers the job was successful. 

    Wednesday, January 15, 2020 1:24 PM
  • This is not what I meant. I understand what you are saying about invoke-command. My specific question is that:

    When running commands remotely as a background job. How can I receive jobs from the remote computers on my local computer and know for which remote computer the job failed and for which computers the job was successful. 

    The job name is the name of the remote computer and the results on Invoke-Command also have the name of the remote.  YOU will only learn how this works by learning basic PowerShell and understanding how PowerShell does things.  Also you will need to learn what objects are, why we use them, why they are more powerful than other methods and how to get information from objects that are returned from CmdLets.  Without a clear understanding of this you will be going in circles for a very long time.

    We cannot teach you PowerShell.  Technical forums are not scoped or resourced to teach a technology.  They are only useful after you have studied the technology and then have questions about the fine points.


    \_(ツ)_/

    Wednesday, January 15, 2020 5:44 PM
    Moderator