在json后面使用Powershell scipt将在CSV上显示

时间:2019-05-24 10:19:01

标签: powershell

JSON

{
    "members":  [
                    {
                        "id":  "4b3556f1-df58-6c3c-848b-022fc6a8668d",
                        "user":  "@{subjectKind=user; metaType=member; domain=2161a74d-1c3e-4d34-a8c8-131360d2e92c; principalName=abc@my.com; mailAddress=abc.xyz@my.com; origin=aad; originId=bc20990e-cf30-4c37-9e4b-b04e37ab2b04; displayName=ABC; }",
                        "accessLevel":  "@{licensingSource=account; accountLicenseType=express; msdnLicenseType=none; licenseDisplayName=Basic; status=active; statusMessage=; assignmentSource=unknown}",
                        "lastAccessedDate":  "2019-05-23T05:54:25.14Z",
                        "dateCreated":  "2019-05-23T05:54:21.6Z",
                        "projectEntitlements":  "",
                        "extensions":  "",
                        "groupAssignments":  ""
                    },
                    {
                        "id":  "4c07118c-8dc0-4e85-97a5-5501003d620d",
                        "user":  "@{subjectKind=user; domain=2161a74d-1c3e-4d34-a8c8-131360d2e92c; principalName=xyz@gmail.com; mailAddress=xyz@gmail.com; origin=aad; originId=; displayName=xyz}",
                        "accessLevel":  "@{licensingSource=account; accountLicenseType=stakeholder; msdnLicenseType=none; licenseDisplayName=Stakeholder; status=pending; statusMessage=; assignmentSource=unknown}",
                        "lastAccessedDate":  "0001-01-01T00:00:00Z",
                        "dateCreated":  "2019-05-23T10:05:41.663Z",
                        "projectEntitlements":  "",
                        "extensions":  "",
                        "groupAssignments":  ""
                    }
                ],
    "continuationToken":  "",
    "totalCount":  2 }

在CSV中,我要跟踪输出:

id                                     principalName       licenseDisplayName    

4b3556f1-df58-6c3c-848b-022fc6a8668d    abc@my.com         Basic    

1 个答案:

答案 0 :(得分:1)

可以使用Select-Object,带有正则表达式模式的Select-StringConvertTo-Csv来完成。

$j = @"
{
    "members":  [
                    {
                        "id":  "4b3556f1-df58-6c3c-848b-022fc6a8668d",
                        "user":  "@{subjectKind=user; metaType=member; domain=2161a74d-1c3e-4d34-a8c8-131360d2e92c; principalName=abc@my.com; mailAddress=abc.xyz@my.com; origin=aad; originId=bc20990e-cf30-4c37-9e4b-b04e37ab2b04; displayName=ABC; }",
                        "accessLevel":  "@{licensingSource=account; accountLicenseType=express; msdnLicenseType=none; licenseDisplayName=Basic; status=active; statusMessage=; assignmentSource=unknown}",
                        "lastAccessedDate":  "2019-05-23T05:54:25.14Z",
                        "dateCreated":  "2019-05-23T05:54:21.6Z",
                        "projectEntitlements":  "",
                        "extensions":  "",
                        "groupAssignments":  ""
                    },
                    {
                        "id":  "4c07118c-8dc0-4e85-97a5-5501003d620d",
                        "user":  "@{subjectKind=user; domain=2161a74d-1c3e-4d34-a8c8-131360d2e92c; principalName=xyz@gmail.com; mailAddress=xyz@gmail.com; origin=aad; originId=; displayName=xyz}",
                        "accessLevel":  "@{licensingSource=account; accountLicenseType=stakeholder; msdnLicenseType=none; licenseDisplayName=Stakeholder; status=pending; statusMessage=; assignmentSource=unknown}",
                        "lastAccessedDate":  "0001-01-01T00:00:00Z",
                        "dateCreated":  "2019-05-23T10:05:41.663Z",
                        "projectEntitlements":  "",
                        "extensions":  "",
                        "groupAssignments":  ""
                    }
                ],
    "continuationToken":  "",
    "totalCount":  2 }
"@ | ConvertFrom-Json

$j.members | Select-Object id,
    @{n='principalName';e={($_.user | 
       Select-String "(?<=principalname=).*?(?=;)").matches.value}},
    @{n='licenseDisplayName';e={($_.accessLevel |
       Select-String "(?<=licenseDisplayName=).*?(?=;)").matches.value}} | 
    ConvertTo-Csv -NoTypeInformation

如果要在控制台中以表格形式显示,只需删除| ConvertTo-Csv

另一种替代方法是从要访问的属性中删除周围的@{},然后使用ConvertFrom-StringData返回具有可轻松访问的子属性值的哈希表。

$j.members | Select-Object id,
    @{n='principalName';e={($_.user -replace "^@{|}$" -split ';' |
        ConvertFrom-StringData).principalName}},
    @{n='licenseDisplayName';e={($_.accesslevel -replace "^@{|}$" -split ';' |
        ConvertFrom-StringData).licenseDisplayName}} |
    ConvertTo-Csv -NoTypeInformation