转换System.Object []和解除属性



azure cli return system.Object [],它不方便。例如,我可以使用脚本创建一个应用程序ignights,AZ CLI返回系统。对象[],包括仪器密钥。我可以编写在数组上迭代并提取所需字段的方法,但是必须有一种更简单的方法

我试图将对象转换为不同格式,但我找不到将CLI命令的结果转换为具有Reerencable属性的对象的可能性

$a = az monitor app-insights component show -a myfuntions -g my-group
$a
{
  "appId": "<id>",
  "applicationId": "myfunctions",
  "applicationType": "web",
  "creationDate": "2019-05-17T15:22:05.042607+00:00",
  "etag": ""8000ece5-0000-0200-0000-4edfe57e0000"",
  "flowType": "Bluefield",
  "hockeyAppId": null,
  "hockeyAppToken": null,
  "id": "/subscriptions/1111111-4d7d-4df4-8dae-5311111111/resourceGroups/dev-api/providers/microsoft.insights/components/dev-validierung-functions"
  "instrumentationKey": "3232323-23e05-32da-bf03-a8b8b4d18783",
  "kind": "web",
  "location": "westeurope",
  "name": "myfunctions",
  "provisioningState": "Succeeded",
  "requestSource": "rest",
  "resourceGroup": "mygroup",
  "samplingPercentage": null,
  "tags": {},
  "tenantId": "<the tenant id>",
  "type": "microsoft.insights/components"
}

var $ a包含system.Object []。可以在自定义功能中迭代阵列中的所有对象,但我认为我缺少一些细节。必须有一种简单的方法将转换为可反发的对象。

您可以使用PsObject属性:

$object = @"
{
"appId": "<id>",
"applicationId": "myfunctions",
"applicationType": "web",
"creationDate": "2019-05-17T15:22:05.042607+00:00",
"etag": ""8000ece5-0000-0200-0000-4edfe57e0000"",
"flowType": "Bluefield",
"hockeyAppId": null,
"hockeyAppToken": null,
"id": "/subscriptions/1111111-4d7d-4df4-8dae-5311111111/resourceGroups/dev-api/providers/microsoft.insights/components/dev-validierung-functions",
"instrumentationKey": "3232323-23e05-32da-bf03-a8b8b4d18783",
"kind": "web",
"location": "westeurope",
"name": "myfunctions",
"provisioningState": "Succeeded",
"requestSource": "rest",
"resourceGroup": "mygroup",
"samplingPercentage": null,
"tags": {},
"tenantId": "<the tenant id>",
"type": "microsoft.insights/components"
}
"@
$j = $object | ConvertFrom-Json
$j.PsObject.Properties | % { Write-Host "Name $($_.Name) ; Value $($_.Value)"}

输出:

Name appId ; Value <id>
Name applicationId ; Value myfunctions
Name applicationType ; Value web
Name creationDate ; Value 2019-05-17T15:22:05.042607+00:00
Name etag ; Value "8000ece5-0000-0200-0000-4edfe57e0000"
Name flowType ; Value Bluefield
Name hockeyAppId ; Value
Name hockeyAppToken ; Value
Name id ; Value /subscriptions/1111111-4d7d-4df4-8dae-5311111111/resourceGroups/dev-api/providers/microsoft.insights/components/dev-validierung-functions
Name instrumentationKey ; Value 3232323-23e05-32da-bf03-a8b8b4d18783
Name kind ; Value web
Name location ; Value westeurope
Name name ; Value myfunctions
Name provisioningState ; Value Succeeded
Name requestSource ; Value rest
Name resourceGroup ; Value mygroup
Name samplingPercentage ; Value
Name tags ; Value
Name tenantId ; Value <the tenant id>
Name type ; Value microsoft.insights/components

希望会有所帮助。

从json。

转换它
$obj = $a | convertfrom-json

最新更新