【问题标题】:How do I modify this powershell script so it creates a log of the date time and names of all files that were deleted or previously deleted?如何修改此 powershell 脚本,以便创建日期时间和所有已删除或以前删除的文件名称的日志?
【发布时间】:2013-05-04 05:15:12
【问题描述】:

我想制作一个 PowerShell 脚本,在某个文件夹中删除 7 天前的任何类型的文件。我遇到的问题是创建一个日志文件,其中包含在脚本运行时已删除或之前删除的所有文件的日期、时间和名称。

我想知道是否有办法修改此页面上找到的答案:https://stackoverflow.com/questions/12326171/powershell-script-to-delete-sub-folders-and-files-if-creation-date-is-7-days-bu?lq=1

我希望它不会发送包含报告的电子邮件,而是会创建一个日志文件,其中包含我存储脚本的文件夹中的名称、时间和日期已删除的所有文件(请注意,我我希望它每次都附加日志文件而不是覆盖它)。很抱歉,因为我不知道如何编码,所以很长一段时间以来我一直在尝试自己做这件事,我一直在问问题等等,但似乎仍然无法让它发挥作用。所以是的,如果有人可以修改该脚本,将不胜感激!谢谢!

这是我对脚本所做的(如果有帮助的话),但它不起作用(我再次不知道如何编码):

$report_items = @()
# set folder path
$dump_path = "C:FileDeleter\AutoDeleteFilesInThisFolder"
# set min age of files
$max_days = "-7"
# get the current date
$curr_date = Get-Date
# determine how far back we go based on current date
$del_date = $curr_date.AddDays($max_days)
# get the sub directories
$sub_dirs = Get-ChildItem $dump_path | Where-Object {!$_.PSIsContainer }
$sub_dirs | % {
    if (Get-ChildItem $_.FullName -Recurse | Where-Object { $_.LastWriteTime -gt     $del_date } ) {
        $report_items += New-Object -Type PSObject -Property @{
            Time = Get-Date -f "hh:mm:ss"
            Message = "Skipping " + $_.FullName + " because it contains items newer     than " + $del_date
        }
    }
    else {
        Remove-Item $_.FullName -Recurse
        $report_items += New-Object -Type PSObject -Property @{
            Time = Get-Date -f "hh:mm:ss"
            Message = "Deleting " + $_.FullName + " because it contains no items newer     than " + $del_date
        }
    }
}
$report_items | out-file "C:\FileDeleter\PruningReport.txt"

【问题讨论】:

  • 您应该告诉我们您所看到的行为以及与您的预期有何不同。我看不到$report_items 是如何设置的,我还注意到您的out-file 调用每次都会覆盖文件-您可能想尝试-Append
  • 您的标题中不必添加[UNSOLVED][SOLVED]。如果它已经解决,您应该已经接受了提供解决方案的答案,而您没有这样做的事实表明它尚未解决。请不要用不需要的额外噪音来混淆您的问题标题。谢谢。

标签: powershell


【解决方案1】:

应该这样做:

Get-ChildItem -Path "C:\FileDeleter\AutoDeleteFilesInThisFolder" -Filter *.torrent -Recurse | Where { $_.CreationTime -lt (Get-Date).AddDays(-7) } | %{Remove-Item -Force; if ($?) {"$(Get-Date -Format 'MM-dd-yy HH:mm:ss.ff')  $_.Name" | Add-Content 'C:\FileDeleter\PruningReport.txt'}}

if ($?) 块的原因是仅在文件删除成功时才写入日志条目 - 你不想假设它是。)

【讨论】:

  • 实际上,这让它更多感到困惑,因为我需要阅读原始脚本并弄清楚它在做什么,然后阅读你的脚本并尝试找到你做了什么改变,并弄清楚你想用这些改变来完成什么。这些都不是很清楚。相反,你能告诉我错误是什么吗?
  • 我不想回答这个答案的问题...我需要帮助回答我对另一个答案的问题...谢谢。请在下面阅读!
  • 我不明白你想说什么。无论如何,我发布的内容根据我的测试解决了您最初的问题。如果您有新问题,您应该接受这个问题的答案(如最初提出的那样)并发布一个新问题。在人们已经回答了您最初提出的问题后,您不应该编辑问题以提出不同的问题。编辑应该是为了清楚起见并添加相关信息,而不是说“别管我问什么和我收到的答案,我决定问一些不同的东西。”
  • 它实际上并没有让我支持 cmets 或接受答案... Idk...但是呃您的答案也不完全正确...这是完整的正确答案:Get-ChildItem -Path "C:\FileDeleter\AutoDeleteFilesInThisFolder" -Recurse | Where { $_.CreationTime -lt (Get-Date).AddDays(-7) } | Tee-Object -FilePath "C:\FileDeleter\ARGH - ME BOOTY (File Deletion Log).txt" -Append | Remove-Item -Force现在就像我说的我会投票给你,但我不能......
  • 好的,您确实需要至少 50 个代表才能支持/反对。但是您不需要任何代表来接受答案(实际上,每次接受您都会获得 +2 代表)。您可以通过单击答案左侧的复选标记来接受答案。
【解决方案2】:

没有那么简洁,但我喜欢不要在 PowerShell 中过度流水线化。

[String]                   $strBaseDir       = "c:\temp";
[String]                   $strFileFilter    = "*.txt";
[Int]                      $intDayThreshold  = 7;
[System.IO.FileSystemInfo] $objFile          = $null;
[Int]                      $intLastWriteDays = 0;
[String]                   $strLogFileName   = "d:\data\yourlogfile.log";

Get-ChildItem -LiteralPath $strBaseDir -Filter $strFileFilter -Recurse | Where-Object{ ! $_.PSIsContainer } |
    Foreach-Object {
        $objFile = $_;

        $intLastWriteDays = [Math]::Round((New-TimeSpan -Start $objFile.LastWriteTime -End (Get-Date)).TotalDays);

        if ( $intLastWriteDays -ge $intDayThreshold ) {
            Write-Output -InputObject ( "{0:G} : Deleting file [{1}] with last write time of [{2:G}], which is [{3}] day(s) ago." -f (Get-Date), $objFile.FullName, $objFile.LastWriteTime, $intLastWriteDays ) | Add-Content -Path $strLogFileName -PassThru;
            $objFile | Remove-Item -Force -ErrorAction silentlyContinue;
            if ( ! $? ) {
                Write-Output -InputObject "ERROR : Failed to remove file." | Add-Content -Path $strLogFileName -PassThru;
                } #if
        } else {
            Write-Output -InputObject ( "{0:G} : Skipping file [{1}] with last write time of [{2:G}], which is [{3}] day(s) ago." -f (Get-Date), $objFile.FullName, $objFile.LastWriteTime, $intLastWriteDays ) | Add-Content -Path $strLogFileName -PassThru;
        } #else-if

        } #Foreach-Object

【讨论】:

  • 好的,我了解要更改的内容,但是 [String] $strFileFilter = "*.txt"; 是否可以修改为任何文件类型?我只是想知道,因为我想使用此脚本自动删除任何类型的任何文件。那么我应该为脚本的.txt 部分添加什么?另外,如果我运行脚本,我会收到一个错误(抱歉),我会将修改后的脚本发布在图片中,并将错误发布在图片中(请注意它上面写着.torrent 我希望它实际上是任何文件类型)。错误图片:bit.ly/15gsKcO 修改后的脚本图片:bit.ly/10z6v9x
猜你喜欢
  • 1970-01-01
  • 2022-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多