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.
PSCustomObjectinstead. To iterate though them, you can use.GetEnumerator()method. Dot notation will also work when referencing a property:$Scriptblock.ID. Just like you mentioned, you can use a hash table. Depends on what fits your needs.{ … }declares a script block in PowerShell, whereas@{ … }declares a hashtable. I’m not sure how your Invoke-restMethod is returning an array of script blocks, but try adding a leading@both to the{in your$array = …sample code like you’ve got in thedetails = @{ … }.$array. This seems very weird format if it is returned fromInvoke-RestMethodInvoke-RestMethod, very curious to know what kind of API you're querying. You can evaluate your scriptblocks as hashtables withInvoke-Expressionby prepending a@:$array |Invoke-Expression -Command {'@'+$_.Ast.ToString()}(this is dangerous and you should never use this in production)