locked
If "start-job" started function can't to write via references? RRS feed

  • Question

  • Hi,

    I trying to use reference inside job started script-block

    for reading it's working well but if i trying to change something it generate an error.

    run this example and will see what i mean:

    $prop = [ordered]@{ID = 100; `
                       Date = ''
                      }
    $result1 = New-Object psobject -Property $prop
    
    
    $Job = Start-Job -ScriptBlock {$a=$input.Item(0).Value.ID;$input.Item(0).Value.ID=2; $a+1} -InputObject @(([ref]$result1));
    Wait-Job -Job $Job;
    Receive-Job -Keep $Job;

    as result i see

    Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
    --     ----            -------------   -----         -----------     --------             -------                  
    903    Job903          BackgroundJob   Completed     True            localhost            $a=$input.Item(0).Valu...
    Method invocation failed because [System.Management.Automation.Runspaces.PipelineReader`1+<GetReadEnumerator>d__20[[System.Object, mscorlib, 
    Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]] does not contain a method named 'Item'.
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
        + PSComputerName        : localhost
     
    101

    If it possible at all to use reference with start-job?

    Saturday, March 31, 2018 2:12 PM

Answers

  • No.  You cannot assign external variables to a job.  You can pass values back from a job.  You can pass values to a job use "ArgumentList".

    Jobs run in a separate memory space and a separate copy of PowerShell.  They are not connected.

     


    \_(ツ)_/

    Saturday, March 31, 2018 2:25 PM

All replies

  • No.  You cannot assign external variables to a job.  You can pass values back from a job.  You can pass values to a job use "ArgumentList".

    Jobs run in a separate memory space and a separate copy of PowerShell.  They are not connected.

     


    \_(ツ)_/

    Saturday, March 31, 2018 2:25 PM
  • Thanks a lot,
    so i understand right that only one way to get something from multiple running jobs -- receive-job one by one?

    And PLS if you know if i will use "Workflow"  with  "foreach -parallel" section if will be  possible to write from parallel processes to one object using reference?

    Saturday, March 31, 2018 4:14 PM
  • To get al jobs and receive them:

    Get-Job | Receive-Job

    Why do you insist on needing to use a [ref]? It is no necessary.  You can receive all results from a workflow by just outputting the objects you want to return.

    $results = myworkflow

    $results will contain all of the output.


    \_(ツ)_/

    Saturday, March 31, 2018 4:17 PM
  • Thanks for your answer
    So all that mean -- no way to get any information whats going on in the child processes until that be done and return value. just only 1 value.
    that is different from functions that can change data in the proceses from where it calling
    but several functions can not be run in parallel as processes (of any kind -- job or workfloow)
    For what i need it?
    Task is -  one PC in online mode control  and compare memory utilization on 3 other servers.
    I correctly understand that it is impossible to solve this problem by powershell?
    Saturday, March 31, 2018 8:46 PM
  • You can continuously send output and read it as it is available.  Receive-Job gets the last available data. 

    while($job.State -eq 'Running'){Receive-Job $job}

    Start be reading the help on jobs

    help about_jobs


    \_(ツ)_/

    Saturday, March 31, 2018 8:51 PM