Search code examples
powershellargumentsstart-job

Passing variable with properties to argumentlist, loosing properties


$Computers = Get-QADComputer -sizelimit 5

returns a list of five computers. I loop with

foreach($computer in $computers) {
echo "and then I can do this $computer.name"

to get only the computername from $computers. But When i try to pass it to start-job like this:

    Start-Job -FilePath $ScriptFile -Name $Computer.Name -ArgumentList $Computer

I am unable to do a $computer.name inside $scriptfile. I have to pass it like $computer.name and call it like $args[0]. But then I loose all the other properties (I am using a bunch inside $scriptfile.)

What am I not getting here? What would you call $computer? And what would you call $computer.name ?

Sune:)


Solution

  • You should be able to get the Name property with $args[0].Name. If you want to access the name parameter like so: $computer.name, then you need to define a parameter in $ScriptFile:

    param(
       $Computer
    )
    
    $Computer.name
    

    By the way' you can't do this:

    echo "and then I can do this $computer.name"
    

    PowerShell expands the value $computer only. Put it in a sub-expression:

    echo "and then I can do this $($computer.name)"