Home > Software design >  powershell output not array type but string type?
powershell output not array type but string type?

Time:11-25

Hi I'm writing a script to call to AZ migration api

        $siteuri= 'https://management.azure.com/subscriptions/'   $metadata.compute.subscriptionID  '/resourceGroups/'   $AzMigreateResourceGroup  '/providers/Microsoft.Migrate/migrateProjects/'   $ProjectName   '/solutions/Servers-Discovery-ServerDiscovery?api-version=2018-09-01-preview'
        $siteoutput=(Invoke-RestMethod -Headers $Authtoken -uri $siteuri).properties.details.extendeddetails.applianceNameToSiteIdMapV3

the result I get is this

[
  {
    "lab3dev-app01": {
      "ApplianceName": "xxx",
      "SiteId": "xxx",
      "KeyVaultId": "xxx",
      "KeyVaultUrl": "xxx",
      "ApplianceDetails": {
        "machineID": "xxx",
        "IPAddress": "192.168.50.210",
        "HostName": "WIN-ETP6NTN8B65",
        "isRegistered": true,
        "discoveryStatus": "Success",
        "deepDiscoveryDisabled": false
      },
      "CertificateContents": {
        "xxx": ""
      },
      "AadAppDetails": {
        "TenantID": "xxx",
        "AppName": "xxx",
        "AppID": "xxx",
        "ObjectID": "xxx"
      },
      "ScaleOutList": null,
      "isV2Site": false
    }
  },
  {
    "l3devhyper01": {
      "ApplianceName": "xxx",
      "SiteId": "xxx",
      "KeyVaultId": "xxx",
      "KeyVaultUrl": "xxx",
      "ApplianceDetails": {
        "machineID": "xxx",
        "IPAddress": "192.168.50.143",
        "HostName": "WIN-PKKCDSLE6OD",
        "isRegistered": true,
        "discoveryStatus": "Success",
        "deepDiscoveryDisabled": false
      },
      "CertificateContents": {
        "l3devhyper017a74agentauthcertv2": ""
      },
      "AadAppDetails": {
        "TenantID": "xxx",
        "AppName": "xxx",
        "AppID": "xxx",
        "ObjectID": "xxx"
      },
      "ScaleOutList": null,
      "isV2Site": false
    }
  }
]

I was hoping this can be an array type, so I can do some search, but gettype() tells me this is a string?

is there anyway to output this as array not a string?

CodePudding user response:

Invoke-RestMethod does return a Object, you're even using it to access a particular property. You'd need to have a look at the API description to see whenever it's just a string attribute (as that might be the case).

As you can see from that string that it is a JSON object you could use ConvertFrom-Json to turn it into an object.

The easiest case without additional error handling would be:

$siteoutput = Invoke-RestMethod -Headers $Authtoken -uri $siteuri
$siteoutput = $siteoutput.properties.details.extendeddetails.applianceNameToSiteIdMapV3 | ConvertFrom-Json
  • Related