当输入为 system.array 时,无法访问 select-string 输出的成员
Trouble accessing members of select-string output when input is a system.array
我正在调用 restAPI,其中一些 return 多个结果,我正在尝试使用 Select-String 来获取数组中的正确行,但它 return 是一个匹配信息具有以@{开头的值的对象...我无法将此值放入哈希表或对象中,因此我可以从字符串中提取一个成员。
我尝试将 MatchInfo 对象转换为带有 out-string 的字符串,然后将结果放入哈希表中。出现以下错误:
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".
拥有一个包含以下内容的 PSCustomObject:
PS C:> $_expFilesRet
href export_files
---- ------------
/api/v1/user_identities/289362/export_files {@{id=352475;
href=/api/v1/exports/458234/export_files/3...
以上的导出文件方法为
PS C:\> $_ExpFilesRet.export_files.getType();
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
具有以下值:
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
尝试了以下
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
我正在尝试从 $_temp 中的结果字符串中获取 ID (352475) 的值。
使用Where-Object
而不是Select-String
来过滤对象:
$_temp = $_ExpFilesRet.export_files |Where-Object export_id -eq $_postret.export_files.export_id |Select -Expand id
我正在调用 restAPI,其中一些 return 多个结果,我正在尝试使用 Select-String 来获取数组中的正确行,但它 return 是一个匹配信息具有以@{开头的值的对象...我无法将此值放入哈希表或对象中,因此我可以从字符串中提取一个成员。
我尝试将 MatchInfo 对象转换为带有 out-string 的字符串,然后将结果放入哈希表中。出现以下错误:
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".
拥有一个包含以下内容的 PSCustomObject:
PS C:> $_expFilesRet
href export_files
---- ------------
/api/v1/user_identities/289362/export_files {@{id=352475;
href=/api/v1/exports/458234/export_files/3...
以上的导出文件方法为
PS C:\> $_ExpFilesRet.export_files.getType();
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
具有以下值:
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
尝试了以下
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
我正在尝试从 $_temp 中的结果字符串中获取 ID (352475) 的值。
使用Where-Object
而不是Select-String
来过滤对象:
$_temp = $_ExpFilesRet.export_files |Where-Object export_id -eq $_postret.export_files.export_id |Select -Expand id