Start-Job and inputObject

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #5908

    by gloop at 2012-08-27 05:00:55

    Hello Guys,

    i try to comit a object to Start-Job via InputObject.

    $inputObj = @{
    "test" = "Inhalt";
    "test2" = "Noch ein Inhalt";
    }
    $inputObj.test
    Start-Job -Name yxc -InputObject $inputObj -ScriptBlock {
    $input
    $input.test
    }
    Get-Job -Name yxc | Wait-Job | Receive-Job | remove-job

    $inputObj.test and $input works, but why doesn't work the $input.test?

    Is there a trick? I didn't find any explenation about this.

    Can anyone help me?
    Thanks gloop

    by poshoholic at 2012-08-27 05:28:49

    The -InputObject parameter is used if you want to pipe something into a command that is inside of Start-Job. What you want to do is pass parameters to Start-Job. For that you should use -ArgumentList, like this:

    [script=powershell]$inputObj = @{
    "test" = "Inhalt";
    "test2" = "Noch ein Inhalt";
    }
    $inputObj.test
    Start-Job -Name yxc -ArgumentList $inputObj -ScriptBlock {
    param(
    $value
    )
    $value
    $value.test
    }
    Get-Job -Name yxc | Wait-Job | Receive-Job # Don't pipe this to remove; this is where you get the results of the job
    Remove-Job -Name yxc # Remove the job after you have the results[/script]

    Note: I could have given the parameter any name I wanted; I just picked value as an example.

    by poshoholic at 2012-08-27 05:31:34

    Also, you should note that $input is a special parameter, an enumerator. Once you have processed an enumerator (by invoking $input), you need to either reset it if you want to process it again or you need to store the results of processing the enumerator so that you can process it again. You can't simply use an enumerator twice in a row without resetting it , because when you use it a second time it will be pointing to the end of the collection you just processed the first time you used it.

    by gloop at 2012-08-27 06:04:16

    Wow big thanks to your fast reply 🙂

    Works perfekt!!!

You must be logged in to reply to this topic.