Powershell 尝试按嵌套值对对象进行分组

Powershell Attempting to Group object by a nested value

我有一个 json 文件,我正在将其转换为一个名为 $sets 的对象,并且我试图通过嵌套在每个对象中的值对这些对象进行分组。每个对象看起来像这样

{
    "$type": "DeviceIos",
    "ActivationLockBypassCode": "example bypass code",
    "ActivationLockBypassStatus": "CodeValid",
    "AgentVersion": null,
    "Memory": {
      "$type": "Memory",
      "AvailableExternalStorage": null,
      "AvailableMemory": null,
      "AvailableSDCardStorage": null,
      "AvailableStorage": 24552280064,
      "TotalExternalStorage": null,
      "TotalMemory": null,
      "TotalSDCardStorage": null,
      "TotalStorage": 34359738368
    },
    "BatteryStatus": 58,
    "BuildVersion": "19C56",
    "CarrierSettingsVersion": null,
    "CellularCarrier": null,
    "CellularTechnology": "None",
    "CurrentMCC": null,
    "CurrentMNC": null,
    "DataRoamingEnabled": false,
    "DeviceTerms": null,
    "DeviceUserInfo": null,
    "MultiUserDeviceInfo": null,
    "ExchangeBlocked": false,
    "ExchangeAccessRequest": "None",
    "ExchangeStatus": "Accepted",
    "FirmwareVersion": null,
    "HardwareEncryptionCaps": 3,
    "HardwareEncryption": "Both",
    "ICCID": null,
    "IMEI_MEID_ESN": null,
    "InRoaming": false,
    "Ipv6": null,
    "IsActivationLockEnabled": false,
    "IsAgentCompatible": true,
    "IsAgentless": true,
    "IsDeviceLocatorServiceEnabled": false,
    "IsDoNotDisturbInEffect": false,
    "IsEncrypted": true,
    "IsEnrolled": true,
    "IsITunesStoreAccountActive": false,
    "IsMDMLostModeEnabled": false,
    "IsOSSecure": true,
    "IsPersonalHotspotEnabled": null,
    "IsSupervised": true,
    "ItunesIdHash": null,
    "LastCheckInTime": null,
    "LastAgentConnectTime": null,
    "LastAgentDisconnectTime": null,
    "LastLoggedOnUser": null,
    "ManagedAppleId": null,
    "LastStatusUpdate": "2022-04-13T14:07:11.417Z",
    "ManufacturerSerialNumber": "example serial number",
    "ModelNumber": "MVHW2LL",
    "ModemFirmwareVersion": null,
    "NetworkConnectionType": "Unknown",
    "PasscodeEnabled": true,
    "PasscodeStatus": "DataProtectionEnabled",
    "PersonalizedName": "examplename",
    "PhoneNumber": null,
    "ProductName": "iPod9,1",
    "SIMCarrierNetwork": null,
    "SubscriberMCC": null,
    "SubscriberMNC": null,
    "SubscriberNumber": null,
    "UserIdHash": null,
    "VoiceRoamingEnabled": false,
    "ManagementProfileUpdateTime": "2022-03-24T19:01:02.193Z",
    "ManagementProfileSigningCertificateExpiry": "2045-01-27T00:00:00Z",
    "MDMClientCertificateHash": "example hash",
    "AppleBusinessManagerUserId": 0,
    "ExchangeOnlineEmailAccess": [],
    "Kind": "Ios",
    "CompliancePolicyStatus": "Compliant",
    "ComplianceStatus": true,
    "ComplianceItems": [
      {
        "$type": "ComplianceItem",
        "ComplianceType": "IsSecured",
        "ComplianceValue": true
      },
      {
        "$type": "ComplianceItem",
        "ComplianceType": "IsDeviceAdmin",
        "ComplianceValue": null
      },
      {
        "$type": "ComplianceItem",
        "ComplianceType": "NotWiped",
        "ComplianceValue": true
      },
      {
        "$type": "ComplianceItem",
        "ComplianceType": "IsEnabled",
        "ComplianceValue": true
      },
      {
        "$type": "ComplianceItem",
        "ComplianceType": "IsEnrolled",
        "ComplianceValue": true
      }
    ],
    "DeviceId": "example device id",
    "DeviceName": "example device name",
    "EnrollmentType": "Device",
    "EnrollmentTime": "2022-03-24T19:01:02.107Z",
    "Family": "Apple",
    "HostName": null,
    "IsAgentOnline": false,
    "CustomAttributes": [
      {
        "$type": "DeviceCustomAttribute",
        "Name": "Account_Name",
        "Value": "example account name",
        "DataType": "Text"
      },
      {
        "$type": "DeviceCustomAttribute",
        "Name": "Account_Number",
        "Value": "0001",
        "DataType": "Text"
      },
      {
        "$type": "DeviceCustomAttribute",
        "Name": "example name",
        "Value": "True",
        "DataType": "Boolean"
      },
      {
        "$type": "DeviceCustomAttribute",
        "Name": "example wave",
        "Value": "example value",
        "DataType": "Enumerator"
      }
    ],
    "MACAddress": "example",
    "BluetoothMAC": "example",
    "WifiMAC": "example",
    "Manufacturer": "Apple",
    "Mode": "Active",
    "Model": "iPod",
    "OSVersion": "exmaple osversion",
    "Path": "example path",
    "Platform": "platformname",
    "ServerName": null
  }

有许多与此类似的对象,它们的值不同,但我想根据自定义属性下 account_number 的值对它们进行分组,因为这些是唯一帐户。 根据我阅读其他帖子的理解,您应该能够使用计算出的 属性 来执行此操作,但我终究无法弄清楚该怎么做。 我试过了

$sets | Select-Object Account_Number, @{Name = 'Account_Number'; Expression = { $_ | Select-Object -ExpandProperty CustomAttributes | Where-Object { Name -eq "Account_Number" } | Select-Object -Property Value } } | Group-Object -Property Account_Number

当我尝试时,我收到了消息

The property cannot be processed because the property "Account_Number" already exists.

我相信我对对象分组有相当的把握,因为我可以通过任何 属性 在第一级 IE 中相当容易地做到这一点我可以通过说 BatteryStatus 或 BuildVersion 轻松地做到这一点,但我似乎没有要掌握的是如何从对象的自定义属性中select取正确的值。 我一直在寻找最接近我想做的事情的主要事情是这个条目 试图从提供的答案中收集我自己的答案 johnlbevan

给定 CustomAttributes 嵌套对象:

$type                 Name           Value                DataType
-----                 ----           -----                --------
DeviceCustomAttribute Account_Name   example account name Text
DeviceCustomAttribute Account_Number 0001                 Text
DeviceCustomAttribute example name   True                 Boolean
DeviceCustomAttribute example wave   example value        Enumerator

并将问题分解成小块,可以得到 Value 属性 的 Value Name 属性 等于 Account_Number 的对象的以下表达式(使用问题中发布的 Json 作为示例):

$json.CustomAttributes.where{ $_.Name -eq 'Account_Number' }.Value # => 0001

从这个意义上说,假设您有一组这些对象作为发布的对象,您可以使用与 Group-Object:

相同的表达式对它们进行分组
$json | Group-Object { $_.CustomAttributes.where{ $_.Name -eq 'Account_Number' }.Value }

例如,我重新创建了 6 个对象并修改了其中一些值,结果是:

Count Name                      Group
----- ----                      -----
    4 0001                      {@{$type=DeviceIos; ActivationLockBypassCode=example....
    1 0002                      {@{$type=DeviceIos; ActivationLockBypassCode=example....
    1 0003                      {@{$type=DeviceIos; ActivationLockBypassCode=example....