方法调用失败,因为[System.Collections.ObjectModel.Collection`1 [[]]不包含我添加到pscustomobject的方法

时间:2019-01-09 11:58:47

标签: json powershell pass-by-reference

我们有一个json文件,其中包含一个数组,该数组包含具有销售价格的产品以及一些(我们必须购买的)产品的零件号和购买价格。例如:

{
"Prices":[
    {
        "Product":"AD User",
        "Price":3.52
    },
    {
        "Product":"Skype for Business Plus SAL",
        "Price":4.68,
        "Part number":"6SH-00002",
        "Buy price":3.90
    },
    {
        "Product":"Citrix CAL",
        "Price":6.40,
        "Part number":"ROYSPLACXABASE",
        "Buy price":5.33
    }
    ]
}

我使用以下代码检索json的内容,并为每个带有部件号的对象将一个对象放置到PSCustomObjects数组中,并添加一个int OrderCount和一个用于增加此OrderCount的函数:

$PurchaseOrder = @()
[object[]]$Prices = Get-Content 'C:\Pricelist.json' | Out-String | ConvertFrom-Json
foreach($_price  in ($Prices.prices | Where-Object {$_."Part Number"})){ 
    $PurchaseOrderItem = $_price.psobject.copy()
    $PurchaseOrderItem | Add-Member -Name "OrderCount" -MemberType NoteProperty -Value 0
    $PurchaseOrderItem | Add-Member -MemberType ScriptMethod -Name IncrementOrderCount {
        param([int]$amount)
        $this.OrderCount = $this.OrderCount + $amount
    }
    $PurchaseOrder += $PurchaseOrderItem
}

如果想出以下函数来更新$ PurchaseOrder:

function Update-PurchaseOrder{
    [Cmdletbinding()]
    param(
        [ref]$PurchaseOrder,
        [string]$ProductName,
        [int]$Count
    )

    $PurchaseOrder.Value.where({$_.Product -eq $ProductName}).IncrementOrderCount($Count)
}

相反,在运行此函数时出现以下错误

Update-PurchaseOrder -PurchaseOrder ([ref]$PurchaseOrder) -ProductName "Citrix CAL" -Count 5

Method invocation failed because [System.Collections.ObjectModel.Collection`1[[System.Management.Automation.PSObject, System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]] does not contain a
method named 'IncrementOrderCount'.
At line:9 char:5
+     $PurchaseOrder.Value.where({$_.Name -eq $ProductName}).IncrementO ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

如果我使用手动创建的$Prices对象运行此代码,例如:

$Prices = @([pscustomobject]@{Product="Product1";Price=1},[pscustomobject]@{Product="Product2";Price=2})

它的运行没有任何问题。我试图将多个对象转换为不同的类型,但是我没有选择。


我添加了一段额外的代码来检查产品是否存在于我的PurchaseOrder中,现在它可以正常运行了。

Write-Verbose "Updating PurchaseOrder for product $Title "
if($PurchaseOrder.Value.where({$_.Product -eq $Title})){
    try{
        $PurchaseOrder.Value.where({$_.Product -eq $Title}).IncrementOrderCount($Count)
    } catch {
        Write-Warning "Error while incrementing OrderCount for product $($Title): $($_.Exception.Message)"
    }
} else {
    Write-Verbose "Product $Title not found in PurchaseOrder."
}

1 个答案:

答案 0 :(得分:0)

使用ForEach-Object遍历集合,然后在每个对象上调用该方法。

另外,产品名称为Product而不是名称的属性。

function Update-PurchaseOrder{
    [Cmdletbinding()]
    param(
        [ref]$PurchaseOrder,
        [string]$ProductName,
        [int]$Count
    )

    $PurchaseOrder.Value.where({$_.Product -eq $ProductName}) | ForEach-Object {$_.IncrementOrderCount($Count)}
}

我还相信$PurchaseOrder已通过引用传递,因此您可以跳过多余的麻烦,而只需传递$PurchaseOrder而不是([ref]$PurchaseOrder)。然后,不需要在函数中拆包值的额外步骤。