Search code examples
powershellrestmcafee

Invoke-RestMethod returning a result but I can't seem to be able to work with the JSON Data and select objects


I have worked out how to retrieve data from our API using the following:

Invoke-RestMethod -Uri "https://mcafee-epo.domain.local:8448/remote/system.find?searchText=$(94422)&searchNameOnly=true&:output=json" -Credential $Cred

I can see that I get data back immediately in Powershell and if I Out-File this I get a result similar to this (shows up all as one line in VS CODE):

OK:
[ { "EPOComputerProperties.ParentID" : 13988, "EPOComputerProperties.ComputerName" : "94422", "EPOComputerProperties.Description" : null, "EPOComputerProperties.ComputerDescription" : "N\u002fA", "EPOComputerProperties.TimeZone" : "AUS Eastern Standard Time", "EPOComputerProperties.DefaultLangID" : "0c09", "EPOComputerProperties.UserName" : "jamesk", "EPOComputerProperties.DomainName" : "DOMAIN.local", "EPOComputerProperties.IPHostName" : "94422.DOMAIN.local", "EPOComputerProperties.IPV6" : "0:0:0:0:0:FFFF:A3F:C40", "EPOComputerProperties.IPAddress" : "10.42.1.80", "EPOComputerProperties.IPSubnet" : "0:0:0:0:0:FFFF:A4D:900", "EPOComputerProperties.IPSubnetMask" : "0:0:0:0:0:FFFF:FFFF:F800", "EPOComputerProperties.IPV4x" : -1975710640, "EPOComputerProperties.IPXAddress" : "N\u002fA", "EPOComputerProperties.SubnetAddress" : "10.42.3.44", "EPOComputerProperties.SubnetMask" : "255.255.248.0", "EPOComputerProperties.NetAddress" : "00057A00", "EPOComputerProperties.OSType" : "Windows 10", "EPOComputerProperties.OSVersion" : "10.0", "EPOComputerProperties.OSCsdVersion" : "", "EPOComputerProperties.OSBuildNum" : 19045, "EPOComputerProperties.OSPlatform" : "Workstation", "EPOComputerProperties.OSOEMID" : "00329-00000-00003-A4459", "EPOComputerProperties.CPUType" : "AMD Ryzen 5 PRO 5650U with Radeon Graphics     ", "EPOComputerProperties.CPUSpeed" : 2296, "EPOComputerProperties.NumOfCPU" : 12, "EPOComputerProperties.CPUSerialNumber" : "N\u002fA", "EPOComputerProperties.TotalPhysicalMemory" : 16442781696, "EPOComputerProperties.FreeMemory" : 5206036480, "EPOComputerProperties.FreeDiskSpace" : 221685, "EPOComputerProperties.TotalDiskSpace" : 482875, "EPOComputerProperties.IsPortable" : 1, "EPOComputerProperties.Vdi" : 0, "EPOComputerProperties.OSBitMode" : 1, "EPOComputerProperties.LastAgentHandler" : 1, "EPOComputerProperties.UserProperty1" : "James Kiss", "EPOComputerProperties.UserProperty2" : "Test", "EPOComputerProperties.UserProperty3" : "Melbourne", "EPOComputerProperties.UserProperty4" : "In use", "EPOComputerProperties.UserProperty5" : "Unspecified", "EPOComputerProperties.UserProperty6" : null, "EPOComputerProperties.UserProperty7" : "Profile: unspecified", "EPOComputerProperties.UserProperty8" : null, "EPOComputerProperties.Free_Space_of_Drive_C" : 221685, "EPOComputerProperties.Total_Space_of_Drive_C" : 482875, "EPOLeafNode.Tags" : "Workstation", "EPOLeafNode.ExcludedTags" : "", "EPOLeafNode.LastUpdate" : "2023-08-14T12:18:07+10:00", "EPOLeafNode.ManagedState" : 1, "EPOLeafNode.AgentGUID" : "634545454-300E-11ED-04417-5C60435345435", "EPOLeafNode.AgentVersion" : "5.7.6.251", "EPOBranchNode.AutoID" : 21 } ]

If I try and select-object one of the properties

$result = Invoke-RestMethod -Uri "https://mcafee-epo.domain.local:8448/remote/system.find?searchText=$(94422)&searchNameOnly=true&:output=json" -Credential $Cred
$result | Select-Object EPOComputerProperties.ParentID

I just get a blank table.

What am I doing wrong? It feels like Powershell is maybe not parsing the result or I'm not understanding something here.

Any guidance would be really appreciated.


Solution

  • The website you are calling may not be returning back a proper json encoding.

    To troubleshoot this sort of thing use Invoke-WebRequest instead of Invoke-RestMethod. Invoke-WebRequest will not attempt to convert the result and will let you see the exact return value.

    $result = Invoke-WebRequest -Uri "https://mcafee-epo.domain.local:8448/remote/system.find?searchText=$(94422)&searchNameOnly=true&:output=json" -Credential $Cred
    $result.Content
    

    The above will let you see the raw value returned.

    You can then try to convert the value to json yourself:

    $result = Invoke-WebRequest -Uri "https://mcafee-epo.domain.local:8448/remote/system.find?searchText=$(94422)&searchNameOnly=true&:output=json" -Credential $Cred
    $result.Content | ConvertFrom-Json | Select-Object EPOComputerProperties.ParentID
    

    If that doesn't work, maybe they are sending the text "OK:" as a part of the result body which would throw off the conversion to json, so try removing it:

    $result = Invoke-WebRequest -Uri "https://mcafee-epo.domain.local:8448/remote/system.find?searchText=$(94422)&searchNameOnly=true&:output=json" -Credential $Cred
    $result.Content -replace 'OK:', '' | ConvertFrom-Json | Select-Object EPOComputerProperties.ParentID