We're playing with Inovke-RestMethod response. As a part of response we have an array:
$array = @(
{
id = "9916"
title = "title9916"
}
{
id = "9917"
title = "title9917"
}
)
We've noticed that every item in an array is treated as ScriptBlock:
PS C:\> $array | % { $_.GetType() }
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True ScriptBlock System.Object
True True ScriptBlock System.Object
How to make every item iterable?
PS. When ScriptBlock is given as:
$scriptblock = {
id = "9916"
title = "title9916"
}
we could transform to HashTable with:
PS C:\> $hash = ConvertFrom-StringData -StringData $scriptblock.ToString()
What if ScriptBlock is given as:
$scriptblock = {
id = "9916"
title = "title9916"
details = @{
name = "name9916"
count = 128
}
}
In this case ConvertFrom-StringData won't work.
PS. For those who are curious: we're playing with ManageEngine ServiceDesk Plus v3
https://www.manageengine.com/products/service-desk/sdpod-v3-api/SDPOD-V3-API.html#get-request
Response is given as a PSCustomObject and we only need a certain subset of it. This subset could change in time and it's defined as a list in XML file. Based on that list we should process our response. We think that the best way to achieve this goal is to transform PSCustomObject to XML and use functions:
- SelectNodes
- SelectSingleNode
- GetElementsByTagName
To transform PSCustomObject to XML we're using modified version of the script:
It works perfectly until it comes to an array.