Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell,foreach,获取行数_Powershell_Foreach - Fatal编程技术网

powershell,foreach,获取行数

powershell,foreach,获取行数,powershell,foreach,Powershell,Foreach,有没有可能得到一美元的号码。foreach管道中的变量 例如: $a = 1..9 $a | foreach {if ($_ -eq 5) { "show the line number of $_"}} 我希望你明白我的意思 谢谢 (.NET框架) 搜索指定的对象并返回其第一个对象的索引 出现在一维数组或数组中的一系列元素中 数组 示例: PS D:\PShell> $a = "aa","bb","cc","dd","ee" PS D:\PShell> $a.IndexOf('

有没有可能得到一美元的号码。foreach管道中的变量

例如:

$a = 1..9
$a | foreach {if ($_ -eq 5) { "show the line number of $_"}}
我希望你明白我的意思

谢谢

.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> 
.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> 

如果试图获取脚本文件名和行号,可以在函数调用中使用$MyInvocation变量来获取调用函数的脚本文件中的行号

将以下内容保存在ps1脚本文件中:

function Get-CurrentLineNumber {
  return $MyInvocation.ScriptLineNumber
}

function Get-CurrentFileName {
  return $MyInvocation.ScriptName
}

1..9 | % {
  if ($_ -eq 5) {
    "{0}:{1}  value is {2}" -f (Get-CurrentFileName), (Get-CurrentLineNumber), $_
  }
}
运行脚本后,您应该会得到如下输出:

//c:\Users\bob\Desktop\Test.ps1:11值为5

无耻地借用了poshoholic的博客:

如果试图获取脚本文件名和行号,可以在函数调用中使用$MyInvocation变量来获取调用函数的脚本文件中的行号

将以下内容保存在ps1脚本文件中:

function Get-CurrentLineNumber {
  return $MyInvocation.ScriptLineNumber
}

function Get-CurrentFileName {
  return $MyInvocation.ScriptName
}

1..9 | % {
  if ($_ -eq 5) {
    "{0}:{1}  value is {2}" -f (Get-CurrentFileName), (Get-CurrentLineNumber), $_
  }
}
运行脚本后,您应该会得到如下输出:

//c:\Users\bob\Desktop\Test.ps1:11值为5

无耻地借用了poshoholic的博客:

您是在尝试获取
$\uu
的值,还是在尝试获取
$\u
的数组索引?示例代码中的值显示得很好。您是在尝试获取
$\uu
的值,还是在尝试获取
$\u
的数组索引?示例代码中的值显示得很好。谢谢,IndexOf就是我所需要的!也许我的英语应该更清楚地描述我的问题谢谢,索引是我需要的!也许我的英语应该更清楚地描述我的问题