简体   繁体   English

如何将 JSON 解析并转换为 Powershell 中的自定义字符串?

[英]How to parse and convert JSON Into custom string in Powershell?

I have a JSON file, which needs to be parsed and converted to a custom string.我有一个 JSON 文件,需要对其进行解析并转换为自定义字符串。 I managed to iterate through the JSON file and find key value pairs that I need, but I'm struggling to change it to desirable format.我设法遍历 JSON 文件并找到我需要的键值对,但我正在努力将其更改为所需的格式。

$json = 
'{
  "value": [
    {
      "id": "/subscriptions/e65c8621-9ded-4164-878c-654bbd1ed607/resourceGroups/Sentinel-Automation-Mainstream/providers/Microsoft.OperationalInsights/workspaces/SentinelAutomationMainstream/providers/Microsoft.SecurityInsights/Watchlists/DomainControllerList/WatchlistItems/f55754ec-9ef8-4a3a-a9f1-3356ac686ae4",
      "name": "f55754ec-9ef8-4a3a-a9f1-3356ac686ae4",
      "etag": "\"2601511d-0000-0d00-0000-6326d31b0000\"",
      "type": "Microsoft.SecurityInsights/Watchlists/WatchlistItems",
      "systemData": {
        "createdAt": "2022-09-18T08:13:06.7222366Z",
        "createdBy": "87275b2a-c86e-410b-b69b-1cf07390846f",
        "createdByType": "Application",
        "lastModifiedAt": "2022-09-18T08:13:06.7222366Z",
        "lastModifiedBy": "87275b2a-c86e-410b-b69b-1cf07390846f",
        "lastModifiedByType": "Application"
      },
      "properties": {
        "watchlistItemType": "watchlist-item",
        "watchlistItemId": "f55754ec-9ef8-4a3a-a9f1-3356ac686ae4",
        "tenantId": "4fb568d9-5b2c-4cf4-92d4-41125b3c2202",
        "isDeleted": false,
        "created": "2022-09-18T10:13:06.7222366+02:00",
        "updated": "2022-09-18T10:13:06.7222366+02:00",
        "createdBy": {
          "objectId": "cc822887-2f84-420d-843a-2e48308c67a8",
          "name": "87275b2a-c86e-410b-b69b-1cf07390846f"
        },
        "updatedBy": {
          "objectId": "cc822887-2f84-420d-843a-2e48308c67a8",
          "name": "87275b2a-c86e-410b-b69b-1cf07390846f"
        },
        "itemsKeyValue": {
          "DcName": "MKDC01",
          "DcHostName": "MKDC01.domain.local",
          "IpAddress": "10.20.30.1",
          "DcType": "GC",
          "Site": "Prague"
        },
        "entityMapping": {}
      }
    },
    {
      "id": "/subscriptions/e65c8621-9ded-4164-878c-654bbd1ed607/resourceGroups/Sentinel-Automation-Mainstream/providers/Microsoft.OperationalInsights/workspaces/SentinelAutomationMainstream/providers/Microsoft.SecurityInsights/Watchlists/DomainControllerList/WatchlistItems/b8b714a0-93f7-440a-acf9-fa3855b73599",
      "name": "b8b714a0-93f7-440a-acf9-fa3855b73599",
      "etag": "\"2601521d-0000-0d00-0000-6326d31b0000\"",
      "type": "Microsoft.SecurityInsights/Watchlists/WatchlistItems",
      "systemData": {
        "createdAt": "2022-09-18T08:13:06.7222366Z",
        "createdBy": "87275b2a-c86e-410b-b69b-1cf07390846f",
        "createdByType": "Application",
        "lastModifiedAt": "2022-09-18T08:13:06.7222366Z",
        "lastModifiedBy": "87275b2a-c86e-410b-b69b-1cf07390846f",
        "lastModifiedByType": "Application"
      },
      "properties": {
        "watchlistItemType": "watchlist-item",
        "watchlistItemId": "b8b714a0-93f7-440a-acf9-fa3855b73599",
        "tenantId": "4fb568d9-5b2c-4cf4-92d4-41125b3c2202",
        "isDeleted": false,
        "created": "2022-09-18T10:13:06.7222366+02:00",
        "updated": "2022-09-18T10:13:06.7222366+02:00",
        "createdBy": {
          "objectId": "cc822887-2f84-420d-843a-2e48308c67a8",
          "name": "87275b2a-c86e-410b-b69b-1cf07390846f"
        },
        "updatedBy": {
          "objectId": "cc822887-2f84-420d-843a-2e48308c67a8",
          "name": "87275b2a-c86e-410b-b69b-1cf07390846f"
        },
        "itemsKeyValue": {
          "DcName": "MKDC02",
          "DcHostName": "MKDC02.domain.local",
          "IpAddress": "10.20.30.2",
          "DcType": "GC",
          "Site": "NewYork"
        },
        "entityMapping": {}
      }
    }
  ]
}'

$json | ConvertFrom-Json | foreach-Object {$_.value.properties.itemsKeyValue} | Out-File ("test2.json")

I do get output like this我确实像这样得到 output


DcName     : MKDC01
DcHostName : MKDC01.domain.local
IpAddress  : 10.20.30.1
DcType     : GC
Site       : Prague

DcName     : MKDC02
DcHostName : MKDC02.domain.local
IpAddress  : 10.20.30.2
DcType     : GC
Site       : NewYork

Unfortunately this is not sufficient for me as I need it in this format不幸的是,这对我来说还不够,因为我需要这种格式

DcName,DcHostName,IpAddress,DcType,Site\r\nMKDC01,MKDC01.domain.local,10.20.30.1,GC,Prague\r\nMKDC02,MKDC02.domain.local,10.20.30.2,GC,NewYork\r\n

Anyone knows an elegant solutions as "keys" in "itemsKeyValue" can change in the future?任何人都知道一个优雅的解决方案,因为“itemsKeyValue”中的“键”可以在未来改变?

It looks like a CSV-file.它看起来像一个 CSV 文件。

Change the last file in your script:更改脚本中的最后一个文件:

$json | ConvertFrom-Json | foreach-Object {$_.value.properties.itemsKeyValue}|export-csv test.csv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM