3

I am trying to implement a ReST request in PowerShell. Below is my $response in PowerShell.

@type            : bundleObject
id               : ZZZZZZ160000000000RU
orgId            : 000007
name             : xxxxxxxxxx
description      : Bundle being used for QA.
createTime       : 2015-04-24T15:13:24.000Z
updateTime       : 2015-04-24T15:13:24.000Z
createdBy        : [email protected]
updatedBy        : [email protected]
lastVersion      : 1.0
paid             : False
accessType       : PUBLIC
objects          : {@{@type=bundleRefObject; objectTypeCode=17; 
                   objectId=ZZZZZZ17000000000003; 
                   objectName=Mapping_01; objectDescription=; 
                   objectUpdateTime=2015-04-24T15:05:41.000Z}, 
                   @{@type=bundleRefObject; objectTypeCode=17; 
                   objectId=ZZZZZZ17000000000004; 
                   objectName=Mapping_02; objectDescription=; 
                   objectUpdateTime=2015-04-24T15:09:28.000Z}, 
                   @{@type=bundleRefObject; objectTypeCode=17; 
                   objectId=ZZZZZZ17000000000005; 
                   objectName=Mapping_03; objectDescription=; 
                   objectUpdateTime=2015-04-24T15:11:59.000Z}}
externalId       : CYIhYNVCSdC6J17N-LyA6A

In the objects section, there are 3 object ids. I need to copy these ids & names in a list, for later use. I have successfully fetched the objects part using

$responseobject = $response.objects

However, I am unsure how I can fetch the object name & object ids and save in a list. Should I use a PSCustomObject here?

--------------------------Updated

One more query here. I added the values to a hashmap with

$response_connection_hashmap = $response_connection|foreach {
    @{  $_.name = $_.id }
    }

However, while fetching values with key

$response_connection_hashmap.Item('Key_1')

I am getting error as

Exception getting "Item": "Cannot convert argument "index", with value:  "Key_1", for "get_Item" to type "System.Int32": "Cannot convert value "Key_1" to type "System.Int32". Error: "Input string was not in a correctformat.

am I missing something else?

0

1 Answer 1

2

The answer depends on how you intend to use the values.

If the only thing you need is an array of IDs, you can use

($response.objects).objectIds

or, in longer form (necessary for older versions of PS)

$response.objects | Select-Object -ExpandProperty objectIds

If you need to pull out the values to operate on them one by one, you can use foreach to iterate over the objects.

$response.objects | foreach {
  Do-SomethingWith -id $_.objectID -updateTime $_.objectUpdateTime
}

If you need (for further processing) a variable containing the ID and UpdateTime for all of the objects, then you can build an array of hashmaps:

$responseObjectList = $response.objects | foreach {
  @{ id = $_.objectID; updateTime = $_.objectUpdateTime }
}

Or PSCustomObjects, if you want them to print nicely.

$responseObjectList = $response.objects | foreach {
  [PSCustomObject]@{ id = $_.objectID; updateTime = $_.objectUpdateTime }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response! It was helpful!! I intend to use the id value in further ReST calls. Hash map normally contains a key-value pairs. But in my case I need an array containing a list of single value, such as id or name, so that it would be easier to parsing the array and for each element call a rest request using id as variable
If all you want is an array of ID values, it's as simple as ($response.objects).objectId
Oh ok! That was pretty straight forward!! Thanks for helping out!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.