You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell中Hashtable查找API返回的policy1值失败问题

问题:无法检测到已存在的VM标签复制策略

通过API调用获取VM标签复制策略的JSON输出并转换为Hashtable后,尝试遍历$tagItems.results查找id为"policy1"的条目,脚本执行后提示未找到该策略,但输出显示Hashtable中确实存在id=policy1的项。

相关PowerShell代码

#Removed the code that connects to my server

Write-Output "================================"

#Create a Hashtable to hold the testables
$testingHash = [hashtable]::new()

#Get VM Tag Replication Policy    
$resultstag = Invoke-WebRequest -Uri "$uri/global-manager/api/v1/global-infra/vm-tag-replication-policies/policy1" -Headers $header

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

Write-Output "==============================="
Write-Output $tagItems
Write-Output "==============================="
Write-Output $resultstag.Content
Write-Output "==============================="

#Find the Policy ID and store it in the testing hashtable
foreach ($tag in $tagItems.results)
{
    If ($tag.id -like "policy1")
    {
        Write-Output "Tag Replication Policy found"
        [void]$testingHash.Add('TAG',$tag)
        [void]$testingHash.Add('ISTAG',$true)
    }
}

If ($null -eq $testingHash.TAG)
{
    [void]$testingHash.Add('ISTAG',$false)
    Write-Error "Tag Replication Policy not found"
}

#Output
If (($testingHash.ISTAG -eq $true))
{
    Write-Output "Tag Replication Policy in GM configured"
}
else {
    Write-Output "Tag Replication Policy NOT in GM configured"
}
}
}

脚本执行结果

PS C:\hol\podStatusCheck> pwsh -command "& { . C:\hol\podStatusCheck\Lab-9.7.ps1; Resolve-Lab-9.7 }"

===============================

Name                           Value
----                           -----
protected_site                 /global-infra/sites/lm-site-a
recovery_sites                 {/global-infra/sites/lm-site-b}
vm_match_criteria              MATCH_BIOS_UUID_NAME
groups                         {/global-infra/domains/default/groups/g-web-stretched, /global-infra/domains/default/groups/g-db-stretched}
resource_type                  VMTagReplicationPolicy
id                             policy1
display_name                   vm tag replication policy lm-site-a to lm-site-b
description                    vm tag replication policy1
path                           /global-infra/vm-tag-replication-policies/policy1
relative_path                  policy1
parent_path                    /global-infra
remote_path
unique_id                      b5b79f3e-bf39-457b-949c-3eaec745d096
owner_id                       11458d41-235c-4efe-816e-84513ffc4d3b
origin_site_id                 11458d41-235c-4efe-816e-84513ffc4d3b
marked_for_delete              False
overridden                     False
_create_time                   1684291183352
_create_user                   admin
_last_modified_time            1684291183352
_last_modified_user            admin
_system_owned                  False
_protection                    NOT_PROTECTED
_revision                      0
===============================
{
  "protected_site" : "/global-infra/sites/lm-site-a",
  "recovery_sites" : [ "/global-infra/sites/lm-site-b" ],
  "vm_match_criteria" : "MATCH_BIOS_UUID_NAME",
  "groups" : [ "/global-infra/domains/default/groups/g-web-stretched", "/global-infra/domains/default/groups/g-db-stretched" ],
  "resource_type" : "VMTagReplicationPolicy",
  "id" : "policy1",
  "display_name" : "vm tag replication policy lm-site-a to lm-site-b",
  "description" : "vm tag replication policy1",
  "path" : "/global-infra/vm-tag-replication-policies/policy1",
  "relative_path" : "policy1",
  "parent_path" : "/global-infra",
  "remote_path" : "",
  "unique_id" : "b5b79f3e-bf39-457b-949c-3eaec745d096",
  "owner_id" : "11458d41-235c-4efe-816e-84513ffc4d3b",
  "origin_site_id" : "11458d41-235c-4efe-816e-84513ffc4d3b",
  "marked_for_delete" : false,
  "overridden" : false,
  "_create_time" : 1684291183352,
  "_create_user" : "admin",
  "_last_modified_time" : 1684291183352,
  "_last_modified_user" : "admin",
  "_system_owned" : false,
  "_protection" : "NOT_PROTECTED",
  "_revision" : 0
}
===============================
Write-Error: Tag Replication Policy not found
Tag Replication Policy NOT in GM configured

Postman返回的API结果

{
    "protected_site": "/global-infra/sites/lm-site-a",
    "recovery_sites": [
        "/global-infra/sites/lm-site-b"
    ],
    "vm_match_criteria": "MATCH_BIOS_UUID_NAME",
    "groups": [
        "/global-infra/domains/default/groups/g-web-stretched",
        "/global-infra/domains/default/groups/g-db-stretched"
    ],
    "resource_type": "VMTagReplicationPolicy",
    "id": "policy1",
    "display_name": "vm tag replication policy lm-site-a to lm-site-b",
    "description": "vm tag replication policy1",
    "path": "/global-infra/vm-tag-replication-policies/policy1",
    "relative_path": "policy1",
    "parent_path": "/global-infra",
    "remote_path": "",
    "unique_id": "b5b79f3e-bf39-457b-949c-3eaec745d096",
    "owner_id": "11458d41-235c-4efe-816e-84513ffc4d3b",
    "origin_site_id": "11458d41-235c-4efe-816e-84513ffc4d3b",
    "marked_for_delete": false,
    "overridden": false,
    "_create_time": 1684291183352,
    "_create_user": "admin",
    "_last_modified_time": 1684291183352,
    "_last_modified_user": "admin",
    "_system_owned": false,
    "_protection": "NOT_PROTECTED",
    "_revision": 0
}

解决方案

问题根源

你调用的API是获取单个特定策略(URL末尾明确指定了policy1),返回的JSON本身就是该策略对象,并不包含results数组。脚本中遍历$tagItems.results时,这个属性根本不存在,所以循环体从未执行,导致无法检测到策略。

修复代码

将遍历$tagItems.results的逻辑替换为直接检查$tagItems.id:

# 替换原遍历逻辑的部分
If ($tagItems.id -eq "policy1")
{
    Write-Output "Tag Replication Policy found"
    [void]$testingHash.Add('TAG',$tagItems)
    [void]$testingHash.Add('ISTAG',$true)
}

完整修改后的核心片段

#Find the Policy ID and store it in the testing hashtable
If ($tagItems.id -eq "policy1")
{
    Write-Output "Tag Replication Policy found"
    [void]$testingHash.Add('TAG',$tagItems)
    [void]$testingHash.Add('ISTAG',$true)
}

If ($null -eq $testingHash.TAG)
{
    [void]$testingHash.Add('ISTAG',$false)
    Write-Error "Tag Replication Policy not found"
}

#Output
If (($testingHash.ISTAG -eq $true))
{
    Write-Output "Tag Replication Policy in GM configured"
}
else {
    Write-Output "Tag Replication Policy NOT in GM configured"
}

内容的提问来源于stack exchange,提问作者I-1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 07:39:54