我有以下代码按照我的意愿工作,但我希望通过添加逻辑来推进它,仅报告不到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')
答案 0 :(得分:3)
简单地说,假设您有一个包含以下文件的文件夹:
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定义)到处都是,只有少数不到一周。
如果我只想获取上周访问过的文件,我会改为运行。
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
}
答案 1 :(得分:0)
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()