Reputation: 43
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.
Upvotes: 0
Views: 156
Reputation: 174465
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
Upvotes: 1