无法从嵌套循环内部访问对象属性

时间:2019-07-18 14:56:35

标签: powershell loops hash properties

下面是我的代码,我试图将对象也设为全局变量,但这似乎无济于事。进行+=语法会产生与.add()语法相同的错误。

foreach ($x in $groups_data) {
    $group_name        = $x.targetName
    $group_shifts_path = "$base/api/xm/1/groups/$group_name/shifts"
    $group_shifts_req  = Invoke-WebRequest -Credential $cred -Uri $group_shifts_path
    $group_shifts_res  = ConvertFrom-Json $group_shifts_req.Content
    $group_shifts_data = $group_shifts_res.data

    foreach ($y in $group_shifts_data) {
        $shift_name = $y.name
        $shift_id   = $y.id

        $group_info = @{
            'group_name'       = $group_name
            'offending_shifts' = @{}
        }

        $group_info = $group_info | ConvertTo-Json

        $group_shift_members_path = "$group_shifts_path/$shift_id/members"
        $group_shift_members_req  = Invoke-WebRequest -Credential $cred -Uri $group_shift_members_path
        $group_shift_members_res  = ConvertFrom-Json $group_shift_members_req.Content
        $group_shift_members_data = $group_shift_members_res.data

        foreach ($z in $group_shift_members_data) {
            if ($z.recipient.recipientType -ne 'PERSON') {
                $group_info.offending_shifts += $shift_name  # HERE'S MY ISSUE
                break
            }
        }

    }
}

我得到的错误如下:

The property 'offending_shifts' cannot be found on this object. Verify that the
property exists and can be set.
At \\etoprod\xMatters\PowerShell Scripts\Group Shift Cleanup & Notify.ps1:42 char:17
+                 $group_info.offending_shifts += $shift_name
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

为什么我不能访问此属性?

0 个答案:

没有答案