在PowerShell中读取文本文件的控件中断逻辑

在PowerShell中读取文本文件的控件中断逻辑,powershell,Powershell,我想读一个文件;当一个值改变时,抓住那一行 $log = Import-Csv .\logfile.txt -Header Date, OS 以下是文本文件中的数据示例: 11/19/2019,Windows 10 Enterprise Version 1809 11/19/2019,Windows 10 Enterprise Version 1809 11/19/2019,Windows 10 Enterprise Version 1809 11/19/2019,Windows 10 Ent

我想读一个文件;当一个值改变时,抓住那一行

$log = Import-Csv .\logfile.txt -Header Date, OS
以下是文本文件中的数据示例:

11/19/2019,Windows 10 Enterprise Version 1809
11/19/2019,Windows 10 Enterprise Version 1809
11/19/2019,Windows 10 Enterprise Version 1809
11/19/2019,Windows 10 Enterprise Version 1903
11/19/2019,Windows 10 Enterprise Version 1903
5/5/2020,Windows 10 Enterprise Version 1909
5/6/2020,Windows 10 Enterprise Version 1909
5/6/2020,Windows 10 Enterprise Version 1909
当操作系统更改为新版本时,抓取该行以显示操作系统更改的日期和操作系统。我希望输出像这样:

11/19/2019,Windows 10 Enterprise Version 1809
11/19/2019,Windows 10 Enterprise Version 1903
5/5/2020,Windows 10 Enterprise Version 1909

非常感谢。

使用循环中的变量跟踪上一个值:

$lastOS = ''
Import-Csv .\logfile.txt -Header Date, OS |ForEach-Object {
  if($lastOS -ne $_.OS){
    # new OS, output current record 
    $_

    # and update our tracking variable
    $lastOS = $_.OS
  }
}
根据提供的样本数据,我们得出:

Date       OS
----       --
11/19/2019 Windows 10 Enterprise Version 1809
11/19/2019 Windows 10 Enterprise Version 1903
5/5/2020   Windows 10 Enterprise Version 1909

这很有效。非常感谢。