如何正确地将Powershell对象属性数组转换为json值数组,即在数组中不使用对象属性标签。
例如:我想为chart.js创建两个json数组,我将对一些进程对象进行分组:
$processgroup = get-process | group -property name
$processgroup.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
$chartlabels = $processgroup.name | convertto-json
$chartlabels
$chartlabels
[
"ApMsgFwd",
"ApntEx",
"Apoint",
"ApplicationFrameHost",
"armsvc",
"BtwRSupportService",
"chrome",
"com.docker.proxy",
"com.docker.service",
"concentr",
"conhost",
"csrss",
"dllhost",
"Docker for Windows",
"dockerd",
"dwm",
"Everything",
"EXCEL",
"explorer",
"fontdrvhost",
"GROOVE",
"hidfind",
"HidMonitorSvc",
"Idle",
"iexplore",
"IpOverUsbSvc",
"jucheck",
"jusched",
"LicensingUI",
"lsass",
"mDNSResponder",
"Memory Compression",
"mqsvc",
"MSASCuiL",
"MsMpEng",
"MSOIDSVC",
"MSOIDSVCM",
"MySQLNotifier",
"NisSrv",
"notepad",
"notepad++",
"nvSCPAPISvr",
"nvvsvc",
"nvwmi64",
"nvxdsync",
"OfficeClickToRun",
"OneDrive",
"OUTLOOK",
"powershell",
"powershell_ise",
"prevhost",
"Receiver",
"redirector",
"rundll32",
"RuntimeBroker",
"SearchIndexer",
"SearchUI",
"Secure System",
"SecurityHealthService",
"SelfServicePlugin",
"services",
"SettingSyncHost",
"ShellExperienceHost",
"sihost",
"SkypeHost",
"smss",
"SMSvcHost",
"spiceworks",
"spiceworks-httpd",
"spoolsv",
"SppExtComObj",
"sppsvc",
"sqlwriter",
"svchost",
"System",
"SystemSettings",
"taskhostw",
"TSVNCache",
"vmcompute",
"vmms",
"vmnat",
"vmnetdhcp",
"vmware-authd",
"vmware-tray",
"vmware-usbarbitrator64",
"wfcrun32",
"wininit",
"winlogon",
"WINWORD",
"WmiPrvSE",
"WUDFHost"
]
#this is the array I want for charts labels, now for the chart value array
$chartvalues = $processgroup | select count | convertto-json
$chartvalues
[
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 30
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 5
},
{
"Count": 2
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 2
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 2
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 4
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 30
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 2
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 2
},
{
"Count": 2
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 2
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 2
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 2
},
{
"Count": 1
},
{
"Count": 2
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 75
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 2
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 1
},
{
"Count": 2
},
{
"Count": 1
},
{
"Count": 1
}
]如何省略"Count“标签,以便PowerShell只创建一个json数组,就像在进程名称数组中一样。
我试过了
$chartvalues = $processgroup.count产生组数的计数结果
我试过了
$chartvalues = $ processgroup | select count -expandproperty count | convertto-json结果与上面的示例相同
https://stackoverflow.com/questions/44435067
复制相似问题