使用JSON返回获取PowerShell中的嵌套对象

时间:2016-07-01 18:03:45

标签: json powershell

我目前正在尝试通过Powershell REST服务器从Hyper-V服务器检索信息。目前,我在powershell中设置了一个HTTP-Listener,用于接收命令,运行它,并以JSON形式返回结果。

如果我发送命令: localhost:8888?command = get-vm -ComputerName ServerName

我得到了一个非常LOOOONG的机器和参数列表,我真的不需要。

{
    "Id": "cd12132a-424d-4a90-9be2-cc521c5cdf8a",
    "Name": "3Y6S9438",
    "State": 2,
    "IntegrationServicesState": "Up to date",
    "OperationalStatus": [
      2
    ],
    "PrimaryOperationalStatus": 2,
    "SecondaryOperationalStatus": null,
    "StatusDescriptions": [
      "Operating normally"
    ],
    "PrimaryStatusDescription": "Operating normally",
    "SecondaryStatusDescription": null,
    "Status": "Operating normally",
    "Heartbeat": 5,
    "ReplicationState": 0,
    "ReplicationHealth": 0,
    "ReplicationMode": 0,
    "CPUUsage": 0,
    "MemoryAssigned": 2147483648,
    "MemoryDemand": 0,
    "MemoryStatus": "",
    "SmartPagingFileInUse": false,
    "Uptime": {
      "Ticks": 9672280000000,
      "Days": 11,
      "Hours": 4,
      "Milliseconds": 0,
      "Minutes": 40,
      "Seconds": 28,
      "TotalDays": 11.194768518518519,
      "TotalHours": 268.6744444444444,
      "TotalMilliseconds": 967228000,
      "TotalMinutes": 16120.466666666667,
      "TotalSeconds": 967228
    },
    "IntegrationServicesVersion": {
      "Major": 6,
      "Minor": 3,
      "Build": 9600,
      "Revision": 17831,
      "MajorRevision": 0,
      "MinorRevision": 17831
    },
    "ResourceMeteringEnabled": false,
    "ConfigurationLocation": "C:\\ClusterStorage\\Volume1\\3Y6S9438\\3Y6S9438",
    "SnapshotFileLocation": "C:\\ClusterStorage\\Volume1\\3Y6S9438\\3Y6S9438\\",
    "AutomaticStartAction": 2,
    "AutomaticStopAction": 4,
    "AutomaticStartDelay": 0,
    "SmartPagingFilePath": "C:\\ClusterStorage\\Volume1\\3Y6S9438\\3Y6S9438\\",
    "NumaAligned": true,
    "NumaNodesCount": 1,
    "NumaSocketCount": 1,
    "Key": {
      "Id": "cd12132a-424d-4a90-9be2-cc521c5cdf8a"
    },
    "IsDeleted": false,
    "ComputerName": "AB124322",
    "Version": "5.0",
    "Notes": "#CLUSTER-INVARIANT#:{f5f3cfe0-28e7-479a-a39d-679975bcd81f}",
    "Generation": 1,
    "Path": "C:\\ClusterStorage\\Volume1\\3Y6S9438\\3Y6S9438",
    "CreationTime": "/Date(1466293818382)/",
    "IsClustered": true,
    "SizeOfSystemFiles": 55480,
    "ParentSnapshotId": null,
    "ParentSnapshotName": null,
    "MemoryStartup": 2147483648,
    "DynamicMemoryEnabled": false,
    "MemoryMinimum": 536870912,
    "MemoryMaximum": 4294967296,
    "ProcessorCount": 2,
    "RemoteFxAdapter": null,
    "NetworkAdapters": [
      "Microsoft.HyperV.PowerShell.VMNetworkAdapter"
    ],
    "FibreChannelHostBusAdapters": [],
    "ComPort1": {
      "Path": "",
      "ComputerName": "AB124322",
      "Name": "COM 1",
      "Id": "Microsoft:CD12132A-424D-4A90-9BE2-CC521C5CDF8A\\8E3A359F-559A-4B6A-98A9-1690A6100ED7\\0",
      "IsDeleted": false,
      "VMId": "cd12132a-424d-4a90-9be2-cc521c5cdf8a",
      "VMName": "3Y6S9438",
      "VMSnapshotId": "00000000-0000-0000-0000-000000000000",
      "VMSnapshotName": null,
      "Key": null
    },
    "ComPort2": {
      "Path": "",
      "ComputerName": "AB124322",
      "Name": "COM 2",
      "Id": "Microsoft:CD12132A-424D-4A90-9BE2-CC521C5CDF8A\\8E3A359F-559A-4B6A-98A9-1690A6100ED7\\1",
      "IsDeleted": false,
      "VMId": "cd12132a-424d-4a90-9be2-cc521c5cdf8a",
      "VMName": "3Y6S9438",
      "VMSnapshotId": "00000000-0000-0000-0000-000000000000",
      "VMSnapshotName": null,
      "Key": null
    },
    "FloppyDrive": {
      "PoolName": null,
      "Path": null,
      "Disk": null,
      "ComputerName": "AB124322",
      "Name": "Diskette Drive",
      "Id": "Microsoft:CD12132A-424D-4A90-9BE2-CC521C5CDF8A\\8F0D2762-0B00-4E04-AF4F-19010527CB93\\0\\0\\D",
      "IsDeleted": false,
      "VMId": "cd12132a-424d-4a90-9be2-cc521c5cdf8a",
      "VMName": "3Y6S9438",
      "VMSnapshotId": "00000000-0000-0000-0000-000000000000",
      "VMSnapshotName": null,
      "Key": null
    },
    "DVDDrives": [
      "Microsoft.HyperV.PowerShell.DvdDrive"
    ],
    "HardDrives": [
      "Microsoft.HyperV.PowerShell.HardDiskDrive"
    ],
    "VMIntegrationService": [
      "Microsoft.HyperV.PowerShell.VMIntegrationComponent",
      "Microsoft.HyperV.PowerShell.VMIntegrationComponent",
      "Microsoft.HyperV.PowerShell.DataExchangeComponent",
      "Microsoft.HyperV.PowerShell.ShutdownComponent",
      "Microsoft.HyperV.PowerShell.VMIntegrationComponent",
      "Microsoft.HyperV.PowerShell.GuestServiceInterfaceComponent"
    ],
    "VMName": "3Y6S9438",
    "VMId": "cd12132a-424d-4a90-9be2-cc521c5cdf8a"
  },

由于返回了多个结果(未显示),我没有获得有关NetworkAdapters数组或HardDrives的任何信息

所以,我知道我不想要所有这些属性。所以我可以用Select

缩小范围
Get-VM | Select Id, Name, NetworkAdapters, HardDrives, etc...

但如果返回多个结果,我仍会遇到同样的问题。我没有获得有关HardDrives或NetworkAdapters的数组的任何信息。

我读到了使用ExpandProperty,但我遇到了这个问题:

select : The property cannot be processed because the property "Id" already exists.

有没有人有任何建议?

另外,我也不需要来自Network对象的所有56个属性。我是否可以在网络查询上运行选择以缩小范围?

由于

编辑:

导致错误的命令示例

get-vm -ComputerName AB124322 | select name, id, state -ExpandProperty networkadapters

我收到此错误

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

1 个答案:

答案 0 :(得分:0)

老问题,但是您应该使用内联表达式来解决这个问题。

get-vm -ComputerName AB124322 | select @{n="name1";e={$_.name}}, id, state -ExpandProperty networkadapters