Select-String 不适用于使用 Out-String 的管道对象 [英] Select-String not working on piped object using Out-String

查看:55
本文介绍了Select-String 不适用于使用 Out-String 的管道对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一个返回一堆数据的 API 请求.在尝试使用 Select-String 搜索它时,它只是吐出存储在变量中的整个值.这是一个互联网服务器,我称之为 api.

I am doing an API request which returns a bunch of data. In attempted to search through it with Select-String, it just spits out the entire value stored in the variable. This is an internet server which I am calling an api.

$return = Invoke-RestMethod -Method GET -Uri $uri -Headers @{"authorization" = $token} -ContentType "application/json" 
$file = $return.data
$file | Out-String -Stream | Select-String -Pattern "word"

这将返回 $file 的整个值.打印 $file 看起来与管道输出相同.为什么这不起作用?

this returns the entire value of $file. printing $file looks like same as the pipe output. Why is this not working?

$file.Gettype 说它是一个 system.object,另一个答案说使用 Out-String,但有些东西不起作用.

$file.Gettype says it is a system.object, another answer said to use Out-String, but something is not working.

$file.Gettype
IsPublic IsSerial Name                                     BaseType                   
-------- -------- ----                                     --------                   
True     True     String                                   System.Object 

推荐答案

仔细检查后,我怀疑您的问题来自 Out-String 文档:

On closer inspection, I suspect that your issue comes from an ambiguity in the Out-String documentation:

表示 cmdlet 为每个 line 发送一个单独的字符串输入对象.默认情况下,每个对象的字符串都是累加的并作为单个字符串发送.

Indicates that the cmdlet sends a separate string for each line of an input object. By default, the strings for each object are accumulated and sent as a single string.

line 应该读作 item.

要将原始字符串拆分为单独的行,您需要使用以下命令拆分字符串:

To split you raw string into separate lines, you will need to split your string using the following command:

$Lines = $return.data -split [Environment]::NewLine

请注意,这假设您的数据使用与您使用的系统相同的字符作为新行.如果不是这种情况,您可能需要使用正则表达式拆分行,例如:

Note that this assumes that your data uses the same characters for a new line as the system you working on. If this is not the case, you might want to split the lines using an regular expression, e.g.:

$Lines = $return.data -split "`r*`n"

那么-Stream参数有什么作用?

So what does the-Stream parameter do?

它为输入对象的每个 item 发送一个单独的字符串.
在此定义中,对于 possible 复数 input objectS.

It sends a separate string for each item of an input object.
Where in this definition, it is also a known common best PowerShell practice to use a singular name for possible plural input objectS.

意思是如果你使用上面定义的 $Lines 变量(或类似 $Lines = Get-Content .\File.json),输入对象$Lines"是字符串的集合:

Meaning if you use the above defined $Lines variable (or something like $Lines = Get-Content .\File.json), the input object "$Lines" is a collection of strings:

$Lines.GetType().Name
String[]

如果您将其流式传输到 Out-String,它将(默认情况下)连接所有项目并返回单个字符串:

if you stream this to Out-String it will (by default) join all the items and return a single string:

($Lines | Out-String).GetType().Name
String

相比之下,如果您使用 -Stream 参数,它会将 $Lines 集合中的每个分隔项直接传递给下一个 cmdlet:

In comparison, if you use the -Stream parameter, it will pass each separated item from the $Lines collection directly to the next cmdlet:

($Lines | Out-String -Stream).GetType().Name
Object[]

我为此创建了一个文档问题:#7133 线"应该是项目"

I have created a document issue for this: #7133 "line" should be "item"

注意:

一般来说,直接偷看和戳序列化字符串(包括 Json)使用字符串方法和/或 cmdlet(如 Select-String).相反,你应该使用相关的解析器(例如ConvertFrom-Json)用于搜索和替换,这将导致更简单的语法并且通常会处理已知问题和陷阱.

In general, it is a bad practice to peek and poke directly into a serialized string (including Json) using string methods and/or cmdlets (like Select-String). Instead you should use the related parser (e.g. ConvertFrom-Json) for searching and replacing which will result in an easier syntax and usually takes care of known issues and pitfalls.

这篇关于Select-String 不适用于使用 Out-String 的管道对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆