ConvertFrom-Json“添加”空项到数组

时间:2017-04-22 02:51:40

标签: json powershell powershell-v5.0

我正在处理一个包含JSON字符串的文件。

{
    "ConfigurationItems": [{
        "ActiveDirectory": [{
                "Users": [
                    { "FirstName": "U1", "LastName": "L1", "Department": "D1", "UserName": "UN1" },
                    { "FirstName": "U2", "LastName": "L2", "Department": "D2", "UserName": "UN2" },
                    { "FirstName": "U3", "LastName": "L3", "Department": "D3", "UserName": "UN3" },
                    { "FirstName": "U4", "LastName": "L4", "Department": "D4", "UserName": "UN4" },
                    { "FirstName": "U5", "LastName": "L5", "Department": "D5", "UserName": "UN5" }
                ]
            },
            {
                "Groups": [
                    { "Name": "G1", "Scope": "Global" },
                    { "Name": "G2", "Scope": "Global" },
                    { "Name": "G3", "Scope": "Global" }
                ]
            },
            {
                "OU": [
                    { "Name": "N1" },
                    { "Name": "N2" },
                    { "Name": "N3" }
                ]
            }
        ]
    }]
}

我通过执行以下操作将此值分配给变量:

$t = Get-content -path $pathtoJSON -raw | ConvertFrom-Json

现在出现的部分是我无法解释的,如果我得到用户的计数它返回7

$t.ConfigurationItems.ActiveDirectory.Users.Count

主要问题是当我迭代这个时,我总是会在我的循环中结束两个$null项,这会抛弃我的代码。我如何确保从ConvertFrom-Json获得正确的计数。暂时我通过检查我的数组项是否不是$null

来解决这个问题

我正在使用PS 5.1。

0 个答案:

没有答案