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

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

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

问题

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

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

在你的原始代码中,使用了错误的键('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:

  1. "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:

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

This is my code:

  1. #I have left out the previous code where I retrieve the ID of the T0 Gateway.
  2. #
  3. #Verify if the T0 path is retrieved correctly
  4. Write-Host "========================================"
  5. Write-Host $t1t0connection
  6. Write-Host "========================================"
  7. #Get Tier 1 Gateways
  8. $resultsT1g = Invoke-WebRequest -Uri "$uri/global-manager/api/v1/global-infra/tier-1s" -Headers $header
  9. #Parse return and create an object from the results
  10. $t1gItems = ConvertFrom-Json -InputObject $resultsT1g.Content -Depth 10 -AsHashtable
  11. #Find the Tier 1 Gateway and store it in the testing hashtable
  12. foreach ($t1g in $t1gItems.results)
  13. {
  14. #If ($t1gItems.results){continue}
  15. If ($t1g.tier0_path -eq $t1t0connection)
  16. {
  17. Write-Output "t1-stretched is connected to t0-gateway-stretched"
  18. [void]$testingHash.Add('T1G',$t1g)
  19. [void]$testingHash.Add('IST1G',$true)
  20. }
  21. }
  22. If ($null -eq $testingHash.T1G)
  23. {
  24. [void]$testingHash.Add('IST1G',$false)
  25. Write-Error "t1-stretched is not connected to t0-gateway-stretched"
  26. }
  27. #Output pass/fail
  28. If (($testingHash.T1G -eq $true))
  29. {
  30. Write-Output "YES!!!"
  31. }
  32. else {
  33. Write-Output "OH NO!"
  34. }
  35. }
  36. }

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

  1. {
  2. "tier0_path": "/global-infra/tier-0s/89bde5c0-83d1-4f76-af90-1fa88b1ab462",
  3. "failover_mode": "NON_PREEMPTIVE",
  4. "enable_standby_relocation": false,
  5. "route_advertisement_types": [
  6. "TIER1_CONNECTED",
  7. "TIER1_IPSEC_LOCAL_ENDPOINT"
  8. ],
  9. "force_whitelisting": false,
  10. "default_rule_logging": false,
  11. "disable_firewall": false,
  12. "ipv6_profile_paths": [
  13. "/global-infra/ipv6-ndra-profiles/default",
  14. "/global-infra/ipv6-dad-profiles/default"
  15. ],
  16. "federation_config": {
  17. "transit_segment_id": "35f39031-7668-45b5-b838-1dc8ef173201",
  18. "global_overlay_id": 195833
  19. },
  20. "pool_allocation": "ROUTING",
  21. "intersite_config": {
  22. "last_admin_active_epoch": 1684291104,
  23. "intersite_transit_subnet": "169.254.32.0/20",
  24. "primary_site_path": "/global-infra/sites/lm-site-a"
  25. },
  26. "ha_mode": "ACTIVE_STANDBY",
  27. "advanced_config": {
  28. "traffic_back_to_source": false,
  29. "centralized_mode_enabled": false
  30. },
  31. "resource_type": "Tier1",
  32. "id": "t1-stretched",
  33. "display_name": "t1-stretched",
  34. "path": "/global-infra/tier-1s/t1-stretched",
  35. "relative_path": "t1-stretched",
  36. "parent_path": "/global-infra",
  37. "remote_path": "",
  38. "unique_id": "2d73d4cb-0c41-4c7c-ba30-8259bb80a352",
  39. "owner_id": "11458d41-235c-4efe-816e-84513ffc4d3b",
  40. "origin_site_id": "11458d41-235c-4efe-816e-84513ffc4d3b",
  41. "marked_for_delete": false,
  42. "overridden": false,
  43. "_create_time": 1684290540239,
  44. "_create_user": "admin",
  45. "_last_modified_time": 1684291104842,
  46. "_last_modified_user": "admin",
  47. "_system_owned": false,
  48. "_protection": "NOT_PROTECTED",
  49. "_revision": 1
  50. }

These are the failing lines:

  1. [void]$testingHash.Add('T1G',$t1g)
  2. [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

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

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

  1. 但需要是:

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

  1. 而且我还需要更改属性查找方式:
  2. ```plaintext
  3. foreach ($t1g in $t1gItems.results)

改成:

  1. foreach ($t1g in $t1gItems)

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

  1. #获取Tier 1网关
  2. $resultsT1g = Invoke-WebRequest -Uri "$uri/global-manager/api/v1/global-infra/tier-1s/t1-stretched" -Headers $header
  3. #解析返回值并从结果中创建对象
  4. $t1gItems = ConvertFrom-Json -InputObject $resultsT1g.Content -Depth 10 -AsHashtable
  5. #查找Tier 1网关并将其存储在测试哈希表中
  6. foreach ($t1g in $t1gItems)
  7. {
  8. #如果($t1gItems.results){continue}
  9. If ($t1g.tier0_path -eq $t1t0connection)
  10. {
  11. Write-Output "t1-stretched连接到t0-gateway-stretched"
  12. [void]$testingHash.Add('T1G',$t1g)
  13. [void]$testingHash.Add('IST1G',$true)
  14. }
  15. }
  16. 如果($null -eq $testingHash.T1G)
  17. {
  18. [void]$testingHash.Add('IST1G',$false)
  19. Write-Error "t1-stretched未连接到t0-gateway-stretched"
  20. }
  21. #输出通过/不通过
  22. 如果($testingHash.IST1G -eq $true)
  23. {
  24. Write-Output "是的!!!"
  25. }
  26. else {
  27. Write-Output "哦不!"
  28. }
  29. }
  30. }
  1. <details>
  2. <summary>英文:</summary>
  3. 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"

  1. But needed to be:

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

  1. And I also needed to change the property lookup from:
  1. foreach ($t1g in $t1gItems.results)
  1. To:
  1. foreach ($t1g in $t1gItems)
  1. The final code is now like this:
  1. #Get Tier 1 Gateways
  2. $resultsT1g = Invoke-WebRequest -Uri &quot;$uri/global-manager/api/v1/global-infra/tier-1s/t1-stretched&quot; -Headers $header
  3. #Parse return and create an object from the results
  4. $t1gItems = ConvertFrom-Json -InputObject $resultsT1g.Content -Depth 10 -AsHashtable
  5. #Find the Tier 1 Gateway and store it in the testing hashtable
  6. foreach ($t1g in $t1gItems)
  7. {
  8. #If ($t1gItems.results){continue}
  9. If ($t1g.tier0_path -eq $t1t0connection)
  10. {
  11. Write-Output &quot;t1-stretched is connected to t0-gateway-stretched&quot;
  12. [void]$testingHash.Add(&#39;T1G&#39;,$t1g)
  13. [void]$testingHash.Add(&#39;IST1G&#39;,$true)
  14. }
  15. }
  16. If ($null -eq $testingHash.T1G)
  17. {
  18. [void]$testingHash.Add(&#39;IST1G&#39;,$false)
  19. Write-Error &quot;t1-stretched is not connected to t0-gateway-stretched&quot;
  20. }
  21. #Output pass/fail
  22. If (($testingHash.IST1G -eq $true))
  23. {
  24. Write-Output &quot;YES!!!&quot;
  25. }
  26. else {
  27. Write-Output &quot;OH NO!&quot;
  28. }
  29. }

}

  1. </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:

确定