Powershell script is only returning one value from Invoke-Command not all the objects I would expect.
I am adding a property to an existing object on a remote server then returning it back.
$inventory
contains application objects that look like this:
Name : Centinel-Dev-AmazonServer
Platform : Centinel
Type : Windows Service
Tier : APP
Status : Running
Name : Portal-QA-Walmart
Platform : Portal
Type : Windows Service
Tier : APP
Status : Running
There are many more application objects similiar to the two given when I run my script it only returns the first object not all.
function verify_workingDir {
param($server)
$inventory = GetInventory -Server $server
$return_object = @()
$return_object += Invoke-Command -ComputerName $server -ScriptBlock {
$inventory = $args[0]
$return_object = @()
foreach ($Application in $inventory) {
$applicationParamters = Get-ItemProperty -Path x:\x\x\x\$($Application.Name)\Parameters
$verify_object = [pscustomobject] @{
WorkingDirectory = $applicationParamters.ServiceWorkingDir
}
$ExpandVerifyObject = $verify_object | Select-Object -Property @{
Name = "MyProperties"
Expression = {$_.WorkingDirectory }
} | Select-Object -ExpandProperty MyProperties
Add-Member -MemberType NoteProperty -Name WorkingDirectory -InputObject $Application -TypeName PSObject -Value $ExpandVerifyObject
$return_object += $Application
}
return $return_object
} -ArgumentList ($inventory) #| Select Name, Platform, Type, Tier, Status, ServerName, WorkingDirectory
return $return_object
}
Answer:
$inventory = $args[0] ---> $inventory = $args