JulianHayward / Azure-MG-Sub-Governance-Reporting

Azure Governance Visualizer aka AzGovViz is a PowerShell script that captures Azure Governance related information such as Azure Policy, RBAC (a lot more) by polling Azure ARM, Storage and Microsoft Graph APIs.
MIT License
814 stars 293 forks source link

Command 'ConvertFrom-Json -AsHashtable' failed. #236

Closed DahliaRane closed 4 days ago

DahliaRane commented 1 month ago

'Getting Private Endpoints for Subscription: -----------Command 'ConvertFrom-Json' failed: Cannot convert the JSON string because it contains keys with different casing. Please use the -AsHashTable switch instead. The key that was attempted to be added to the existing key 'Provisioner' was 'provisioner'. [AzAPICall 1.2.1] 'Getting Private Endpoints for Subscription------------------Trying command 'ConvertFrom-Json -AsHashtable'

[AzAPICall 1.2.1] 'Getting Private Endpoints for Subscription--------------------Command 'ConvertFrom-Json -AsHashtable' succeeded. Please file an issue at the AzGovViz GitHub repository (aka.ms/AzGovViz) and provide a dump

[AzAPICall 1.2.1] 'Getting Private Endpoints for Subscription-------------------Command 'ConvertFrom-Json -AsHashtable' failed. Please file an issue at the AzGovViz GitHub repository (aka.ms/AzGovViz) and provide a dump (scrub subscription Id and company identifyable names) of the resource (portal JSOn view) - Thank you!

JulianHayward commented 4 weeks ago

@DahliaRane hmm, can you check the pe in the portal (json view) and see if the key 'provisioner'/'Provisioner' is reasonable?

DahliaRane commented 4 weeks ago

I could not find the key 'provisioner'/'Provisioner' in any private end point for the subscription it failed.

JulianHayward commented 4 weeks ago

can you try this:

$subscriptionId = "subId" #example: fb66b6e2-879e-4e99-97a8-72c767d9bd75
$uri = "https://management.azure.com/subscriptions/$($subscriptionId)/providers/Microsoft.Network/privateEndpoints?api-version=2022-05-01"
$method = 'GET'
$res = Invoke-AzRestMethod -Uri $uri -Method $method
$resObj = ($res.Content | ConvertFrom-Json)
$resObjAsHashtable = ($res.Content | ConvertFrom-Json -AsHashtable)
DahliaRane commented 4 weeks ago

ConvertFrom-Json: Line | 6 | $resObj = ($res.Content | ConvertFrom-Json) | ~~~~ | Cannot convert the JSON string because it contains keys with different casing. Please use the -AsHashTable switch instead. The key that was attempted to be added to the existing key 'Provisioner' was 'provisioner'.

When I remove the line $resObj = ($res.Content | ConvertFrom-Json) it worked

JulianHayward commented 4 weeks ago

well, now you can inspect the PEs to find the keys with different casing. Curious to see what/why..

DahliaRane commented 4 weeks ago

"tags": { "AppName": "", "CostCenter": "", "Environment": "dev", "Provisioner": "Terraform", "module_name": "private_endpoint", "module_version": "1.1.0", "provisioner": "Terraform", "test_tag": "Testing Dev" },

JulianHayward commented 4 weeks ago

so, is the tagName with differing casing intended?

DahliaRane commented 4 weeks ago

I am not sure but seeing the values I think the two different cases are referring same value.

In some other private endpoints i don't see lower case "provisioner".

"tags": {
    "AppName": "",
    "CostCenter": "",
    "Environment": "",
    "Provisioner": "Terraform",
    "UseCase": "Vault",
    "module_name": "private_endpoint",
    "module_version": "1.1.0"
}
JulianHayward commented 4 weeks ago

I need to think about handling such scenarios with the azapicall/azgovviz.. not sure how common such scenario is, can you check with the team that deployed that PE with terraform?

DahliaRane commented 4 weeks ago

Its quite common . We have some subscriptions which has such scenarios.

JulianHayward commented 3 weeks ago

@DahliaRane sorry for the delay. In general, Azure resource JSON do not come with multicased keys. In your case the Tags are the problem / such issue has not been reported before (so I assume a rare edge case here).

DahliaRane commented 2 weeks ago

Ok looks like we cant delete keys which are multicased for some restrictions. Can you let me know where to modify the code or some solution for this rare edge case.

JulianHayward commented 2 weeks ago

give it a try using the parameter -noNetwork.

Can you share some insights why you are using these multicased tagsNames?

JulianHayward commented 1 week ago

@DahliaRane any update from your side?