Extract response from Invoke-RestMethod

Hello,

Im a new guy with powershell, and i need some help. Im doing a $update= Invoke-RestMethod -Method $Method -URI $URI -header $Header | ConvertFrom-Json

The result is:

$id : 1
_items : {@{$id=2; Index=0; Version=; Name=Atualização; Id=Atualização; Level=1; DateAndTime=2024-04-08T20:54:25.8611613-03:00; Subject=Sem atualizações disponíveis; Details=Não existem atualizações
disponíveis para serem executadas nesta base de dados.; Url=; Duration=00:00:00}}
Version : *
Start : 2024-04-08T20:54:25.8611613-03:00
End : 2024-04-08T20:54:25.0008218-03:00
Partial : False
DBInfo : @{$id=3; _greatestTables=System.Object; DBName=; DBVersion=; DbDif=; ServerName=; ServerConfiguration=; DBSize=0; Environment=; UpgradeVersion=}

I need to extract only Level information, on _items object, and if show in screen, if result is 1, the updated was successfully, if result is 2, the update has erros, if the result is 3 not updated.
If Level was on a separated object was easier, but this way I can’t.

Hi @phelype and welcome to the forums!

First, please make sure you’re formatting your code to adhere to the coding practices found here:
PSH Code Format

Assuming that the returned object is a hashtable, you could, instead of ConvertFrom-Json, you can simply just use the property field of the $update variable that you assigned it to such that:
$update._items.id

EDIT:
Typically, the return type of an Invoke-RestMethod is a hashtable of objects, hence why you can check the properties of each field. You can see this doing a $update | Get-Member.

Austin,

Thank u for help, worked.
Next time I will format it as code, thanks for the tip.

1 Like