Powershell 文件创建 - 创建日期奇怪

Powershell 文件创建 - 创建日期奇怪

为工作创建一个脚本,他们希望日志滚动 7 天。例如,今天的日志是 xxxxx.log,明天将重命名为 xxxxx.01,第二天重命名为 xxxxx.02。

但是我的日期发生了一些奇怪的事情。为了测试脚本是否正常工作,我创建了一个名为 xxxxx.log 的文件,并将创建日期(通过 powershell)更改为几年前。然后脚本检查文件以查看创建日期是否是今天,如果不是,则滚动。

它将文件重命名为 xxxxx.01(这很好),然后为今天创建了一个新的日志文件,但是这个新的日志文件具有旧文件的创建日期,而重命名的文件的日期更改为完全不同的日期。

我尝试过复制文件然后删除,尝试过重命名,尝试过移动,但它们都有相同的症状!

代码如下:

Function RollLogs {
#If the file exists, get the creation date of the current log
if($FileDate = (Get-ChildItem -Path "$LogDirectory\$LogFile.$LogExt").CreationTime) {
    #Format the creation date of the file, and compare it to todays date
    #If the date is not equal to today, then roll the logs
    if($FileDate.ToString('yyyyMMdd') -ne $Date.ToString('yyyyMMdd')) {
        #Delete the .07 file, and increment each file by a number
        #Then rename the last file to 0.1
        if(Test-Path "$LogDirectory\$LogFile.07") {Remove-Item -Path "$LogDirectory\$LogFile.07" -Force}
        if(Test-Path "$LogDirectory\$LogFile.06") {Copy-Item -Path "$LogDirectory\$LogFile.06" -Destination "$LogDirectory\$LogFile.07"; Remove-Item -Path "$LogDirectory\$LogFile.06" -Force}
        if(Test-Path "$LogDirectory\$LogFile.05") {Copy-Item -Path "$LogDirectory\$LogFile.05" -Destination "$LogDirectory\$LogFile.06"; Remove-Item -Path "$LogDirectory\$LogFile.05" -Force}
        if(Test-Path "$LogDirectory\$LogFile.04") {Copy-Item -Path "$LogDirectory\$LogFile.04" -Destination "$LogDirectory\$LogFile.05"; Remove-Item -Path "$LogDirectory\$LogFile.04" -Force}
        if(Test-Path "$LogDirectory\$LogFile.03") {Copy-Item -Path "$LogDirectory\$LogFile.03" -Destination "$LogDirectory\$LogFile.04"; Remove-Item -Path "$LogDirectory\$LogFile.03" -Force}
        if(Test-Path "$LogDirectory\$LogFile.02") {Copy-Item -Path "$LogDirectory\$LogFile.02" -Destination "$LogDirectory\$LogFile.03"; Remove-Item -Path "$LogDirectory\$LogFile.02" -Force}
        if(Test-Path "$LogDirectory\$LogFile.01") {Copy-Item -Path "$LogDirectory\$LogFile.01" -Destination "$LogDirectory\$LogFile.02"; Remove-Item -Path "$LogDirectory\$LogFile.01" -Force}
        if(Test-Path "$LogDirectory\$LogFile.$LogExt") {Copy-Item -Path "$LogDirectory\$LogFile.$LogExt" -Destination "$LogDirectory\$LogFile.01"; Remove-Item -Path "$LogDirectory\$LogFile.$LogExt" -Force}
        Log "------------------------------------------------------------------------" "Information"
        Log "Log Roll Complete" "Information"
        Log "------------------------------------------------------------------------" "Information"
    }
}

}

答案1

这是一个老问题,但我认为我已经找到了问题的根源,这应该是有帮助的:

这似乎是一个时间问题。旧文件名被重复使用得太快,并且保留了旧版本的创建日期。我使用 start-sleep -seconds 15 解决了这个问题。14 秒或更短的时间不起作用。我不知道为什么会出现这种行为(一些残留位需要时间从内存或文件表中清除?)。

我也看到了同样的情况,但我的做法略有不同。我创建了 temp.txt,然后将 production.txt 重命名为 $date-Production.txt,以保留 15 个以前的版本。然后将 temp.txt 重命名为 production.txt。然后我得到超过 15 天的 *.txt,并删除它们。最终,我重命名的所有文件和 production 文件都是同一日期,超过 15 天了,所有内容都被删除了!

我查看的日期是来自 get-childitem 的创建日期(与创建日期相同:在文件资源管理器中右键单击属性)。

$date = get-date format yyy-MM-dd
"Production (existing) created on: " #logging
gci Production.txt | Select CreationTime #logging
"create this" | out-file temp.txt
"Temp file created on: " #logging
gci Temp.txt | Select CreationTime #logging
Rename-item production.txt "$date-production.txt"
"Renamed $Date-Production created on: " #logging
gci "$Date-Production.txt" | Select CreationTime #logging
Start-Sleep -Seconds 15 #Adding this fixed it!
Rename-item temp.txt production.txt
"Renamed Production file (was Temp) created on: " #logging 
gci Production.txt | Select CreationTime #logging
#File Cleanup section is below, to delete all .txt older than 15 days
$files2delete = gci *.txt | Where {$_.CreationTime -lt (Get-Date).AddDays(-15)}
$files2delete | Remove-Item -Force

上述脚本日志的输出(稍微清理了一下):

Production (existing) created on: 4/16/2020 9:29:11 AM
Temp file created on:  4/16/2020 9:32:19 AM 
Renamed 2020-04-16_093310-Production created on:  4/16/2020 9:29:11 AM
Sleeping *14* seconds
Renamed Production file (was Temp) created on:  4/16/2020 9:29:11 AM


Production (existing) created on:  4/16/2020 9:29:11 AM
Temp file created on:  4/16/2020 9:33:42 AM
Renamed 2020-04-16_093342-Production created on:  4/16/2020 9:29:11 AM
Sleeping *15* seconds
Renamed Production file (was Temp) created on:  4/16/2020 9:33:42 AM

相关内容