“Add” with “2” argument(s)” PowerCLI 在尝试使用哈希表时出错

huangapple go评论119阅读模式
英文:

"Add" with "2" argument(s)" PowerCLI error when trying to use a hash table

问题

以下是你的代码中错误的部分以及修复建议:

#Find the Tier 1 Gateway and store it in the testing hashtable
foreach ($t1g in $t1gItems.results)
{
    #If ($t1gItems.results){continue}
    If ($t1g.tier0_path -eq $t1t0connection)
    {
        Write-Output "t1-stretched is connected to t0-gateway-stretched"
        # 使用正确的键来添加到哈希表中
        [void]$testingHash.Add('T1G',$t1g)
        [void]$testingHash.Add('IST1G',$true)
    }
}

在你的原始代码中,使用了错误的键('T1G''IST1G')尝试将数据添加到哈希表中。应该使用普通的单引号(')来包围键名,而不是HTML转义字符。

请将上述代码片段中的哈希表添加部分更改为上述建议的方式,然后重新运行你的脚本。这应该解决你遇到的问题。

英文:

I am retrieving a JSON file that I am placing in a hashing table.
When a certain value matches
I want to make sure that this is matched:

"tier0_path": "/global-infra/tier-0s/89bde5c0-83d1-4f76-af90-1fa88b1ab462",

So the tier "tier0_path" needs to be exactly the value. If this is the case I should get a SUCCESS message if not I should get a NOT SUCCESS message.

My script is kind of working, but I am getting the following error:


========================================
/global-infra/tier-0s/89bde5c0-83d1-4f76-af90-1fa88b1ab462
========================================
t1-stretched is connected to t0-gateway-stretched
t1-stretched is connected to t0-gateway-stretched
MethodInvocationException: C:\hol\podStatusCheck\Lab-9.8.ps1:54
Line |
  54 |              [void]$testingHash.Add('T1G',$t1g)
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "Add" with "2" argument(s): "Item has already been added. Key in dictionary: 'T1G'  Key being added: 'T1G'"
MethodInvocationException: C:\my\podStatusCheck\Lab-9.8.ps1:55
Line |
  55 |              [void]$testingHash.Add('IST1G',$true)
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "Add" with "2" argument(s): "Item has already been added. Key in dictionary: 'IST1G'  Key being added: 'IST1G'"
OH NO!

This is my code:

#I have left out the previous code where I retrieve the ID of the T0 Gateway.
#
#Verify if the T0 path is retrieved correctly
    Write-Host "========================================"
    Write-Host $t1t0connection
    Write-Host "========================================"

    #Get Tier 1 Gateways    
    $resultsT1g = Invoke-WebRequest -Uri "$uri/global-manager/api/v1/global-infra/tier-1s" -Headers $header

    #Parse return and create an object from the results
    $t1gItems = ConvertFrom-Json -InputObject $resultsT1g.Content -Depth 10 -AsHashtable

    #Find the Tier 1 Gateway and store it in the testing hashtable
    foreach ($t1g in $t1gItems.results)
    {
        #If ($t1gItems.results){continue}
        If ($t1g.tier0_path -eq $t1t0connection)
        {
            Write-Output "t1-stretched is connected to t0-gateway-stretched"
            [void]$testingHash.Add('T1G',$t1g)
            [void]$testingHash.Add('IST1G',$true)
        }
    }

    If ($null -eq $testingHash.T1G)
    {
        [void]$testingHash.Add('IST1G',$false)
        Write-Error "t1-stretched is not connected to t0-gateway-stretched"
    }

    #Output pass/fail
    If (($testingHash.T1G -eq $true))
    {
        Write-Output "YES!!!"
    }
    else {
        Write-Output "OH NO!"
    }
    }
}

These are the results of the API call (in Postman):

{
    "tier0_path": "/global-infra/tier-0s/89bde5c0-83d1-4f76-af90-1fa88b1ab462",
    "failover_mode": "NON_PREEMPTIVE",
    "enable_standby_relocation": false,
    "route_advertisement_types": [
        "TIER1_CONNECTED",
        "TIER1_IPSEC_LOCAL_ENDPOINT"
    ],
    "force_whitelisting": false,
    "default_rule_logging": false,
    "disable_firewall": false,
    "ipv6_profile_paths": [
        "/global-infra/ipv6-ndra-profiles/default",
        "/global-infra/ipv6-dad-profiles/default"
    ],
    "federation_config": {
        "transit_segment_id": "35f39031-7668-45b5-b838-1dc8ef173201",
        "global_overlay_id": 195833
    },
    "pool_allocation": "ROUTING",
    "intersite_config": {
        "last_admin_active_epoch": 1684291104,
        "intersite_transit_subnet": "169.254.32.0/20",
        "primary_site_path": "/global-infra/sites/lm-site-a"
    },
    "ha_mode": "ACTIVE_STANDBY",
    "advanced_config": {
        "traffic_back_to_source": false,
        "centralized_mode_enabled": false
    },
    "resource_type": "Tier1",
    "id": "t1-stretched",
    "display_name": "t1-stretched",
    "path": "/global-infra/tier-1s/t1-stretched",
    "relative_path": "t1-stretched",
    "parent_path": "/global-infra",
    "remote_path": "",
    "unique_id": "2d73d4cb-0c41-4c7c-ba30-8259bb80a352",
    "owner_id": "11458d41-235c-4efe-816e-84513ffc4d3b",
    "origin_site_id": "11458d41-235c-4efe-816e-84513ffc4d3b",
    "marked_for_delete": false,
    "overridden": false,
    "_create_time": 1684290540239,
    "_create_user": "admin",
    "_last_modified_time": 1684291104842,
    "_last_modified_user": "admin",
    "_system_owned": false,
    "_protection": "NOT_PROTECTED",
    "_revision": 1
}

These are the failing lines:

            [void]$testingHash.Add('T1G',$t1g)
            [void]$testingHash.Add('IST1G',$true)

I did a Google search, and I have either the same keys that I am trying to store, or the value of a key is empty, I have tried several fixes, but none of them worked ...

Can someone help me with this, please?
I really appreciate any help you can provide.

Busy trying to fix this the whole evening now...

答案1

得分: 1

我在我的URI中犯了一个错误(感谢@mclayton指出),我的URI是:

"$uri/global-manager/api/v1/global-infra/tier-1s"


但需要是:

"$uri/global-manager/api/v1/global-infra/tier-1s/t1-stretched"


而且我还需要更改属性查找方式:

```plaintext
    foreach ($t1g in $t1gItems.results)

改成:

    foreach ($t1g in $t1gItems)

最终的代码现在是这样的:

    #获取Tier 1网关    
    $resultsT1g = Invoke-WebRequest -Uri "$uri/global-manager/api/v1/global-infra/tier-1s/t1-stretched" -Headers $header

    #解析返回值并从结果中创建对象
    $t1gItems = ConvertFrom-Json -InputObject $resultsT1g.Content -Depth 10 -AsHashtable

    #查找Tier 1网关并将其存储在测试哈希表中
    foreach ($t1g in $t1gItems)
    {
        #如果($t1gItems.results){continue}
        If ($t1g.tier0_path -eq $t1t0connection)
        {
            Write-Output "t1-stretched连接到t0-gateway-stretched"
            [void]$testingHash.Add('T1G',$t1g)
            [void]$testingHash.Add('IST1G',$true)   
        }
    }

    如果($null -eq $testingHash.T1G)
    {
        [void]$testingHash.Add('IST1G',$false)
        Write-Error "t1-stretched未连接到t0-gateway-stretched"
    }

    #输出通过/不通过
    如果($testingHash.IST1G -eq $true)
    {
        Write-Output "是的!!!"
    }
    else {
        Write-Output "哦不!"
    }
    }
}

<details>
<summary>英文:</summary>

I made a mistake in my URI (thanks @mclayton for pointing this out) my URI was:

"$uri/global-manager/api/v1/global-infra/tier-1s"


But needed to be:

"$uri/global-manager/api/v1/global-infra/tier-1s/t1-stretched"


And I also needed to change the property lookup from:


foreach ($t1g in $t1gItems.results)

To:

foreach ($t1g in $t1gItems)

The final code is now like this:

#Get Tier 1 Gateways    
$resultsT1g = Invoke-WebRequest -Uri &quot;$uri/global-manager/api/v1/global-infra/tier-1s/t1-stretched&quot; -Headers $header

#Parse return and create an object from the results
$t1gItems = ConvertFrom-Json -InputObject $resultsT1g.Content -Depth 10 -AsHashtable

#Find the Tier 1 Gateway and store it in the testing hashtable
foreach ($t1g in $t1gItems)
{
    #If ($t1gItems.results){continue}
    If ($t1g.tier0_path -eq $t1t0connection)
    {
        Write-Output &quot;t1-stretched is connected to t0-gateway-stretched&quot;
        [void]$testingHash.Add(&#39;T1G&#39;,$t1g)
        [void]$testingHash.Add(&#39;IST1G&#39;,$true)   
    }
}

If ($null -eq $testingHash.T1G)
{
    [void]$testingHash.Add(&#39;IST1G&#39;,$false)
    Write-Error &quot;t1-stretched is not connected to t0-gateway-stretched&quot;
}

#Output pass/fail
If (($testingHash.IST1G -eq $true))
{
    Write-Output &quot;YES!!!&quot;
}
else {
    Write-Output &quot;OH NO!&quot;
}
}

}




</details>



huangapple
  • 本文由 发表于 2023年6月5日 05:26:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定