Question

$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:)

Was it helpful?

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)"

OTHER TIPS

Exactly so, this is how I would write something similar:

#Get Services
$Services = Get-Service
#Limit results to the first 5 entries
$Services = $Services[0..4]
#Loop through services
foreach ($Service in $Services)
{
    #For each Service run a basic echo to host to prove that the Service was passed in successfully
    Start-Job -ScriptBlock { param ($Service) Write-Host "Service Name is $($Service.Name)" } -ArgumentList $Service
}

You can then retrieve the Jobs like this:

#Retrieve Jobs
Get-Job | Receive-Job
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top