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
Try $inventory = $args
instead of $inventory = $args[0]
.
Passing arrays to script blocks is not the easiest thing because $args
flattens everything to a single array. If you pass -ArgumentList @(1,2,3), 4
, then $Args
will be a single array @(1,2,3,4)
and $Args[0]
will be 1
.
If you ever need to pass complex arguments with -ArgumentList
, pass them all as a single HashTable: -ArgumentList @{FirstArg = @(1,2,3); SecondArg = 4}
-ArgumentList @{FirstArg = @(1,2,3); SecondArg = 4}
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.