PowerShell嵌套属性迭代

时间:2017-07-14 19:42:13

标签: powershell pscustomobject

在下面的脚本中寻找一些输入。基本上我用Invoke-WebRequest检索一些json数据。我需要将许多属性导出为CSV。从Invoke-WebRequest返回的数据包含在数组中:$devicedetails。此脚本适用于输出到CSV。

& {
    Foreach ($PC in $DeviceDetails)
    {     
        Foreach ($AppName in $PC.categories)
        {
            ForEach ($App in $AppName.apps)
            {
                   ForEach($Status in $App.health_status)
                   {
                        [pscustomobject] @{
                        "Device ID" = $PC.device_id
                        "Device Type" = $PC.device_type
                        "Device Name" = $PC.device_name
                        Nickname = $PC.nick_name
                        Last_Seen = $PC.last_seen
                        Compliance_Category_Status = $Appname.issue
                        Compliance_Category = $Appname.category_id 
                        Product_Name = $App.name
                        Product_Vendor = $App.vendor
                        Product_Version = $App.version
                        Product_Health_Item = $Status.status 
                        Product_Health_Status = $Status.issue 
                        }
                    }
            }
        }

    }
} | Export-CSV -PAth $Outfile -NoTypeInformation 

好奇这是否是将属性输出到CSV的最佳方式。另外,我现在需要对我创建的自定义对象执行一些额外的处理,但如果我将变量分配给该自定义对象,如下所示,则需要几分钟才能完成,而只需导出到CSV需要12- 13秒为什么表现如此糟糕?

$DeviceOutput= @()
    Foreach ($PC in $DeviceDetails)
    {     
        Foreach ($AppName in $PC.categories)
        {
            ForEach ($App in $AppName.apps)
            {
                   ForEach($Status in $App.health_status)
                   {
                        $DeviceOutput += [pscustomobject] @{
                        "Device ID" = $PC.device_id
                        "Device Type" = $PC.device_type
                        "Device Name" = $PC.device_name
                        "Nickname" = $PC.nick_name
                        Compliance_Category_Status = $Appname.issue
                        Compliance_Category = $Appname.category_id 
                        Product_Name = $App.name
                        Product_Vendor = $App.vendor
                        Product_Version = $App.version
                        Product_Health_Item = $Status.status 
                        Product_Health_Status = $Status.issue 
                        }
                    }
            }
        }

    }

0 个答案:

没有答案