powershellpowercli

Hashtable output is not matching my variable in PowerShell Script


I am retrieving a JSON output using an API call and I am putting that JSON output in a Hashtable.

I am then doing a Hashtable lookup to see if a certain value is available, and if this is the case I should get a message stating this is the case.

This is a part of my PowerShell Code:

   #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"
    }
    }
}

My result is this after I run the script:

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

You can see that the output is returning the "id" and this is "policy1". I am checking against this "id" but the "id" is not seed for some reason.

This is the output when I do an API call with Postman:

{
    "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
}

I am retrieving a JSON output using an API call and I am putting that JSON output in a Hashtable.

I am then doing a Hashtable lookup to see if a certain value is available, and if this is the case I should get a message stating this is the case.


Solution

  • This change made it work ... (based on the comments I got from @mclayton)

    foreach ($tag in $tagItems.id)
        {
            If ($tag -like "policy1")
            {
                Write-Output "Tag Replication Policy found"
                [void]$testingHash.Add('TAG',$tag)
                [void]$testingHash.Add('ISTAG',$true)
            }
        }