试图通过嵌套值分组对象的Powershell
我有一个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.
,我相信我对对象进行分组的掌握,因为我可以在第一层的任何属性中都很容易做到这一点,即我可以通过说电池status或buildersion来轻松地做到这一点,但是我要做的似乎没有掌握的是如何从对象自定义属性中选择正确的值。 我一直在看的主要事情似乎最接近我想做的是这个条目 powershell select select and and and yess yested field 试图从提供的答案中收集我自己的答案 johnlbevan> johnlbevan
I have a json file i am converting to an object called $sets and i am trying to group those objects by a value that is nest in each object. each object looks like this
{
"$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
}
there are many object similar to this one they vary with different values but i would like to group them by the value of account_number under customattributes as these are the unique accounts.
from what i understand from reading other posts you should be able to use a calculated property to do this but i cannot for the life of me figure how to do so.
i have tried
$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
when i try i get the message back
The property cannot be processed because the property "Account_Number" already exists.
i believe i have a fair grasp on grouping objects as i can do this fairly easy by any property at the first level IE i can do this easily by say BatteryStatus or BuildVersion but what i do not seem to grasp is how to select the right value from the object custom attributes.
the main thing i have been looking at the seems closest that i can find to what i want to do is this entry
PowerShell select and group by nested field
trying to glean my own answer from the answer provided by
johnlbevan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
给定
自定义attributes
嵌套对象:并将问题分为小块,您可以获得 value
value
Account_number
的对象的属性(使用以下表达式)name
属性等于 感觉,假设您像发布的对象一样具有这些对象的数组,则可以使用group-object
的相同表达式对它们进行分组,例如,我已经重新创建了6个对象并修改了其中一些值,结果是:
Given the
CustomAttributes
nested object:And breaking the problem in small pieces, you can get the Value of the
Value
property of the object where theName
property is equal toAccount_Number
with the following expression (using the Json posted in question as example):In that sense, assuming you have an array of these objects as the one posted, you can group them using the same expression with
Group-Object
:As example, I have recreated 6 objects and modified some of these Values, the result is: