Powershell文件随未来和过去时间更新



我有一个文本文件,其中包含这样的内容。

One;Thomas;Newyork;2020-12-31 14:00:00;0
Two;David;London;2021-01-31 12:00:00;0
Three;James;Chicago;2021-01-20 15:00:00;0
Four;Edward;India;2020-12-25 15:00:00;0

根据日期和时间,这些条目中有两个是过去的条目,两个是未来的条目。字符串中的最后一个0表示Flag。对于过去的条目,该标志需要更改为1。

考虑所有条目都用数组分隔。我尝试了这段代码,但它不能解决这里的问题。

for ($item=0 ; $item -lt $entries.count ; $item++)
{
if ($entries.DateTime[$item] -lt (Get-Date -Format "yyyy-MM-dd  HH:mm:ss"))
{
$cont = Get-Content $entries -ErrorAction Stop
$string = $entries.number[$item] + ";" + $entries.name[$item] + ";" + 
$entries.city[$item]+ ";" + $entries.DateTime[$item] 
$lineNum = $cont | Select-String $string
$line = $lineNum.LineNumber + 1
$cont[$line] = $string + ";1"
Set-Content -path $entries
}
}

我在这个概念上犯了错误。

输出应为:-

一个;托马斯;纽约;2020-12-31 14:00:00;1(相对于当前日期的过去部署(

二;大卫;伦敦2021-01-31 12:00:00;0

三;詹姆斯;芝加哥2021-01-20 15:00:00;0

四;爱德华;印度2020-12-25 15:00:00;1(相对于当前日期的过去部署(

此输出需要覆盖从中提取内容的文件,即Entries.txt

param(
$exampleFileName = "d:tmpfile.txt"
)
@"
One;Thomas;Newyork;2020-12-31 14:00:00;0
Two;David;London;2021-01-31 12:00:00;0
Three;James;Chicago;2021-01-20 15:00:00;0
Four;Edward;India;2020-12-25 15:00:00;0
"@ | Out-File $exampleFileName

Remove-Variable out -ErrorAction SilentlyContinue
Get-Content $exampleFileName | ForEach-Object {
$out += ($_ -and [datetime]::Parse(($_ -split ";")[3]) -gt [datetime]::Now) ? $_.SubString(0,$_.Length-1) + "1`r`n" : $_ + "`r`n"
}
Out-File -InputObject $out  -FilePath $exampleFileName 

最新更新