PowerShell – 上次保存的文件 – 要缩小到最后7天

我有下面的代码工作,因为我想要它,但我想推进它通过添加逻辑来只报告less于7天的文件。 我不确定我是否可以用这个逻辑做到这一点..

Remove-Item ("c:\temp\output.txt") $Date = Get-Date Write-Output $Date | Out-file 'c:\temp\output.txt' -Append $word = New-Object -Com Excel.Application $word.Visible = $false #to prevent the document you open to show $filename = "Test User" $filelocation = "\\server\Storage\Applications\Employee Sheets\Test.xlsm" $doc = $word.Workbooks.Open($filelocation, $false, $true) # open in read only mode Write-Host 'Currently processing' $filename '/ 1 out of 45' $binding = "System.Reflection.BindingFlags" -as [type] Foreach($property in $doc.BuiltInDocumentProperties) { try { $pn = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null) if ($pn -eq "Last author") { $lastSavedBy = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null) } if ($pn -eq "Last Save Time") { $lastSavedTime = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null) } } catch { } } $doc.Close($false) $word.Quit() $1 = "Last Saved By: " + $lastSavedBy $2 = "Last Saved Date: " + $lastSavedTime Write-Output "$filename $1 $2" | Out-file 'c:\temp\output.txt' -Append Invoke-Item ('c:\temp\output.txt') 

简单地说,假设你有一个这样的文件的文件夹:

  Directory: C:\users\Stephen\Downloads Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 6/22/2016 11:05 AM 85638 GOINGSTATELESS (1).jpg -a---- 6/22/2016 11:03 AM 75529 GOINGSTATELESS.jpg -a---- 6/17/2016 9:45 PM 738880 JavaSetup8u91.exe -a---- 6/15/2016 2:08 PM 8390 CheckIfSCOM2012r2WasUpgradedToUR3.ps1 -a---- 6/15/2016 12:01 PM 7811072 LWAPlugin64BitInstaller32.msi -a---- 6/15/2016 10:59 AM 45648 36491_SCOM_AppMonitoringPhase3_Change Order.docx d----- 6/6/2016 3:27 PM ffmpeg-20160531-git-a1953d4-win64-static -a---- 6/6/2016 3:24 PM 15201547 ffmpeg-20160531-git-a1953d4-win64-static.7z d----- 6/6/2016 3:17 PM dedication.tar -a---- 6/6/2016 2:18 PM 14344935 20160606_135011.mp4 

正如你所看到的,他们的年龄(由lastWriteTime定义)已经遍布全球,只有less数人不到一周的时间。

如果我只想得到上周访问过的文件,我会运行它。

 dir | Where LastAccessTime -ge ((Get-Date).AddDays(-7)) Directory: C:\users\Stephen\Downloads Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 6/22/2016 11:05 AM 85638 GOINGSTATELESS (1).jpg -a---- 6/22/2016 11:03 AM 75529 GOINGSTATELESS.jpg -a---- 6/17/2016 9:45 PM 738880 JavaSetup8u91.exe 

您只需在您的代码块运行之前,将相同的逻辑应用于您的代码。 缩小这样的文件列表,我想你会顺利的。

  $LastWeekFiles = dir | Where LastAccessTime -ge ((Get-Date).AddDays(-7)) ForEach ($file in $lastWeekFiles){ #Insert your code here } 
  Remove-Item ("c:\temp\output.txt") $Date = Get-Date Write-Output $Date | Out-file 'c:\temp\output.txt' -Append $LastWeekFiles = Get-ChildItem "\\server\Storage\Applications\"| Where LastWriteTime -ge ((Get-Date).AddHours(-58)) $word = New-Object -Com Excel.Application $word.Visible = $false #to prevent the document you open to show $binding = "System.Reflection.BindingFlags" -as [type] $doc = $word.Workbooks.Open("\\server\Storage\Applications\" + $file, $false, $true) # open in read only mode ForEach ($file in $lastWeekFiles){ ForEach($property in $doc.BuiltInDocumentProperties){ try{ $pn = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null) if ($pn -eq "Last author") { $lastSavedBy = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null) } if ($pn -eq "Last Save Time") { $lastSavedTime = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null) } } catch{} } } $doc.Close($false) $word.Quit()