Is it possible to terminate or stop a PowerShell pipeline from within a filter

后端 未结 8 2159
無奈伤痛
無奈伤痛 2020-12-06 06:27

I have written a simple PowerShell filter that pushes the current object down the pipeline if its date is between the specified begin and end date. The objects coming down

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 06:44

    Another option would be to use the -file parameter on a switch statement. Using -file will read the file one line at a time, and you can use break to exit immediately without reading the rest of the file.

    switch -file $someFile {
      # Parse current line for later matches.
      { $script:line = [DateTime]$_ } { }
      # If less than min date, keep looking.
      { $line -lt $minDate } { Write-Host "skipping: $line"; continue }
      # If greater than max date, stop checking.
      { $line -gt $maxDate } { Write-Host "stopping: $line"; break }
      # Otherwise, date is between min and max.
      default { Write-Host "match: $line" }
    }
    

提交回复
热议问题