0

I am calling restAPIs and some of them return multiple results, I am trying use Select-String to get the correct line in the Array, but it returns a matchinfo object with a value starting with @{... I can't get this value into a hashtable or object so I can extract a member from the string.

I tried converting the MatchInfo object to a string with out-string and then putting that result in a hashtable. Get following error:

Cannot convert the "
@{id=352475; href=/api/v1/exports/458234/export_files/352475; export_id=458234; status=Available}
" value of type "System.String" to type "System.Collections.Hashtable".

Have a PSCustomObject with the following contents:

PS C:> $_expFilesRet
href                                                        export_files
----                                                        ------------
/api/v1/user_identities/289362/export_files                 {@{id=352475; 
href=/api/v1/exports/458234/export_files/3...

The export files method of the above is

PS C:\> $_ExpFilesRet.export_files.getType();
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

with the following value:

PS C:\> $_expFilesRet.export_files

                           id href                                              export_id status
                           -- ----                                              --------- ------
                       352475 /api/v1/exports/458234/exp...                        458234 Available
                       278697 /api/v1/exports/357459/exp...                        357459 Available

Attempted the following

PS C:\> $_temp=$_ExpFilesRet.export_files | select-string -pattern $_postret.export_files.export_id
PS C:\> $_temp

@{id=352475; href=/api/v1/exports/458234/export_files/352475; export_id=458234; status=Available}
PS C:\> $_temp=$_ExpFilesRet.export_files | select-string -pattern $_postret.export_files.export_id | out-str
ing -width 1000
PS C:\> $_temp

@{id=352475; href=/api/v1/exports/458234/export_files/352475; export_id=458234; status=Available}


PS C:\> [hashtable]$_temp=$_temp
Cannot convert the "
@{id=352475; href=/api/v1/exports/458234/export_files/352475; export_id=458234; status=Available}
" value of type "System.String" to type "System.Collections.Hashtable".
At line:1 char:1
+ [hashtable]$_temp=$_temp
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

I am trying to get the value of ID (352475) from the resulting string in $_temp.

1 Answer 1

1

Use Where-Object rather than Select-String to filter objects:

$_temp = $_ExpFilesRet.export_files |Where-Object export_id -eq $_postret.export_files.export_id |Select -Expand id
Sign up to request clarification or add additional context in comments.

Comments

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.