powershell,foreach,获取行数

Sto*_*ffi 1 powershell foreach

有没有可能得到 $_. foreach管道中的变量?

例子:

$a = 1..9
$a | foreach {if ($_ -eq 5) { "show the line number of $_"}}
Run Code Online (Sandbox Code Playgroud)

我希望你知道我的意思。

谢谢!

Jos*_*efZ 5

Array.IndexOf 方法.NET框架)

搜索指定的对象并返回其在一维数组或数组元素范围中第一次出现的索引。

例子

PS D:\PShell> $a = "aa","bb","cc","dd","ee"

PS D:\PShell> $a.IndexOf('cc')
2

PS D:\PShell> $a=101..109

PS D:\PShell> $a.IndexOf(105)
4

PS D:\PShell> $a |foreach {if ($_ -eq 105) {"$($a.IndexOf($_)) is the line number of $_"}}
4 is the line number of 105

PS D:\PShell> 
Run Code Online (Sandbox Code Playgroud)