I am trying to do validation. checking the deployed values with the given values. I extract the vnet values from Azure resources using RestAPI method and convertto-json from Object because of vnet object is giving me empty object (@{value=System.Object[]}). The following is the Json code I am getting:
{
"value": [
{
"properties": "@{virtualNetworkSubnetId=/subscriptions/<XXXX>/resourceGroups/<XXXX>/providers/Microsoft.Network/virtualNetworks/<XXXX>/subnets/<XXXX>; ignoreMissingVnetServiceEndpoint=True; state=Ready}",
"id": "/subscriptions/<XXXX>/resourceGroups/<XXXX>/providers/Microsoft.DBforPostgreSQL/servers/<XXXX>/virtualNetworkRules/<XXXX>",
"name": "<XXXX>",
"type": "Microsoft.DBforPostgreSQL/servers/virtualNetworkRules"
}
]
}
The following powershell command is to compare the value but getting an error saying $vnet.name and $vnet.id is $null
$vnet= ( $vnet | ConvertTo-Json)
It "has this number of vNet Rules defined: $($config.vnetRules.count)"
{
$vnet.count | Should -Be $config.vnetRules.count
}
#$vnet.count is working and giving an success message
foreach ($vnetRule in $config.vNetRules) {
Write-Host $vnet #-> getting Json
Write-Host $vnet.Name #-> return as Empty($null)
Write-Host $vnet.value.Name #-> return as Empty($null)
Write-Host $vnet.id #-> return as Empty($null)
Write-Host $vnet.value.id #-> return as Empty($null)
it "has a vNet rule named: $($vnetRule.ruleName)" {
$vnet.name | Should -Be $vnetRule.ruleName
}
it "has a vNet Rule Subnet ID of: $($vNetRule.subnetId)" {
$vnet.value.id | Should -Be $vNetRule.subnetId
}
}
Returns $null.
In my attempt to recreate your invoke-restmethod
output, I used your JSON packet as input to create the $vnet
variable.
I believe the problem is you are using Write-host
to display the object instead of simply the object name. Write-host
will attempt to convert the complex object to a string and hence you see the weird output as you can see below. see the difference when i simply out the object?
Now, $vnet
has 4 properties id, name, properties, type
and can be invoked as shown.
In you case, you have converted the variable $vnet
to json
and then attempting to display its properties. But Json
does not have any properties, except length
. And hence, invoking those properties will give you null.