简体   繁体   中英

ForEach only reading first line of text

I have a text file where I have multiple text, on different lines which are unique. I have something like:

$submit = Get-Content submit.txt 

$submit| ForEach {
function Query 


{ ....}
return 0

However, it only reads the first line and parses it to the function. It parses it the same number of lines that are on the text file. How can I get it to iterate through each line one by one and pass the text to the function?

You'll want to define your function just once, before entering the loop:

# 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 $_
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM