【问题标题】:Return Object Array from Invoke-Command从 Invoke-Command 返回对象数组
【发布时间】:2018-05-19 09:15:35
【问题描述】:

Powershell 脚本只从 Invoke-Command 返回一个值,而不是我期望的所有对象。

我正在向远程服务器上的现有对象添加一个属性,然后将其返回。

$inventory 包含如下所示的应用程序对象:

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

当我运行我的脚本时,它只返回第一个对象而不是全部。

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

【问题讨论】:

    标签: powershell


    【解决方案1】:

    尝试$inventory = $args 而不是$inventory = $args[0]

    将数组传递给脚本块并不是最简单的事情,因为$args 会将所有内容扁平化为单个数组。如果你传递了-ArgumentList @(1,2,3), 4,那么$Args 将是单个数组@(1,2,3,4)$Args[0] 将是1

    如果您需要使用 -ArgumentList 传递复杂的参数,请将它们全部作为单个 HashTable 传递:-ArgumentList @{FirstArg = @(1,2,3); SecondArg = 4}

    【讨论】:

    • 将数组传递给脚本块并不是最简单的事情,因为$args 将所有内容都扁平化为一个数组。 你能举个例子吗?我正在尝试Invoke-Command -Command { ConvertTo-Json -InputObject $args -Compress } -ArgumentList @(1,2,3),4 并得到[[1,2,3],4] 作为结果。这里没有展平。
    • @PetSerAl Thisthis 一定是我正在考虑的问题。当您需要将多个数组作为参数传递时,真正的问题就出现了,因为-Arg (,$Arr1),$Val1,(,$Arr2) 可能根本不起作用。
    • 因为-Arg (,$Arr1),$Val1,(,$Arr2) 可能根本不起作用。 也许是因为您应该改用$Arr1,$Val1,$Arr2。你不应该这样写(,$Arr1),除非你只传递一个参数。
    • @PetSerAl “你不应该这样写” 为什么?这很好用:$x=@(1,2,3); $y = @((,$x),4,(,$x)); $y[0]; $y[1]; $y[2];。使用Invoke-Command 你必须说$x=@(1,2,3); Invoke-Command -Command { $args[0][0]; $args[1]; $args[2][0] } -ArgumentList @((,$x),4,(,$x)) 的事实是异常的。该语言应该表现一致,并且由于所有背景封装,它不在这里。这就是为什么我说:将所有论点放在HashTable 中并以这种方式传递论点。这是最一致和最明显的行为。
    • 这很好用:$x=@(1,2,3); $y = @((,$x),4,(,$x)); $y[0]; $y[1]; $y[2];$y[0].Length; $y[2].Length; 将返回11,而不是33。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多