Select-String - 管道输入时的模式输出

Select-String -Pattern Output when piping into

我试图让 PowerShell 吐出一个语义版本控制变量,但卡在其中,只显示我输入的命令(在 ISE 中执行)或 2 个错误('missing argument' 或 'doesn' t accept piped input'), 如果我尝试解决它们,将以简单地再次显示的命令结束。

我如何得到这个:

(Invoke-WebRequest -Uri http://someplace).Links.href | Out-String -Stream |
    Select-String -Pattern [regex]$someGoodRegex -OutVariable $NodeVersion_target

假设正则表达式和 Web 请求指向可靠的东西以简单地将搜索词粘贴在 -OutVariable 定义的范围内?


更笼统地说,有没有一种显示管道中对象属性的好方法?经过大量挖掘,我偶然发现了 {$_},但如果命令变得比简单的 cmdlet 稍微复杂一点,它就无法再次显示命令以外的任何内容。

删除 [regex]Select-String 已经将参数 -Pattern 的参数视为正则表达式。

从变量名中删除 $。您需要它来直接使用变量,但是 -OutVariable 参数需要没有前导 $.

的裸变量名

您还可以删除 Out-String -Stream

像这样的东西应该可以工作:

$uri = 'http://www.example.com/'
$re  = 'v\d+\.\d+\.\d+/s'
(Invoke-WebRequest -Uri $uri).Links.href |
    Select-String -Pattern $re -OutVariable NodeVersion_target

或者,您可以将管道的输出分配给一个变量,而不是使用 -OutVariable:

$uri = 'http://www.example.com/'
$re  = 'v\d+\.\d+\.\d+/s'
$NodeVersion_target = (Invoke-WebRequest -Uri $uri).Links.href |
                      Select-String -Pattern $re

后者其实更PoSh


关于检查管道中的当前对象:通过管道输入 Get-Member to get a list of the properties/methods of the pipelined objects, and pipe into Format-List * 以获取管道对象的值列表。