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
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, January 21, 2020 2:19 AM
    Moderator
  • Hi, 

    If u do this way (multiple computers in one job) instead of maybe hundreds jobs, each job representing single computername, u will have to look how to work with childjobs..

    U can create background job named "Script###", each job can contain several computers, some of them will return some data, some will fail (offline, psremoting problem etc..) (I call it parent job)

    Then u would need to check status for each Job (completed/failed). Collect completed jobs.  For failed jobs u need to determine which computer inside parent job failed, so there u list all childjobs and check status. 

    $failedjobs = get-job $j.Id -IncludeChildJob |?{$_.name -notlike "*Script*"}  #u created job named "Script", childjobs have different name, u dont really care what name is it
       foreach ($f in $failedjobs) 
       {
       if ($f.State -eq "Completed")
        {
         <custom code>
        }
       elseif ($f.State -eq "Failed")
        {
         <custom code>
        }
    

    Either u can create multiple jobs suggested by JRV, or one job but u need to work with childjobs...

    Wednesday, January 22, 2020 7:43 AM
  • -includechildjob is key


    $list = get-content list.txt
    invoke-command $list { 'whatever' } -asjob | wait-job
    get-job -includechildjob | select state,location



    State     Location
    -----     --------
    Failed    COMP1
    Completed COMP2
    Completed COMP3

    • Edited by JS2010 Thursday, January 23, 2020 4:06 PM
    Wednesday, January 22, 2020 5:08 PM