ForEach只阅读第一行文字

问题描述

我有一个文本文件,其中有多个文本,它们在不同的行上是唯一的。我有类似的东西:

$submit = Get-Content submit.txt 

$submit| ForEach {
function Query 


{ ....}
return 0

但是,它仅读取第一行并将其解析为函数。它解析它与文本文件上的行数相同。我怎样才能让它一步一步地遍历每一行并将文本传递给函数

解决方法

您只想在进入循环之前 定义一次函数:

# This will define the function so its available later,in the loop
function Query {
  param($Line)
  # ...
}

Get-Content submit.txt |ForEach-Object {
  # this actually executes the Query function
  Query -Line $_
}