我正在编写一个Powershell脚本,用于搜索网络位置,如果该文件是在2011年或2012年创建的,则将文件名写入日志,以及创建的所有2011/12文件的总和。
我在尝试转换文件创建日期和时间并将其与我的日期范围进行比较时遇到异常。
<#Checks one network location for files from 2011.
gets the name of that file and adds to the count for 2011, then writes it to a log.
Repeats for 2012.#>
New-Item c:\users\logs\yearLog.txt -type file -force
$path = "\\path"
$log = "c:\users\log"
$date2011 = "2011"
$date2012 = "2012"
write-progress -activity "Compiling Data" -status "Progress:"
$x = 0
"$date2011 files" | add-content $log
Get-Childitem -Path $path -Recurse | Where-Object {$_.LastWriteTime -gt (12/31/2010) -AND $_LastWriteTime -lt (01/01/2012) |
ForEach {
$filename = $_.fullname
$x++
"$filename" | add-content $movelog
}
"$date2011 total files = $x" | add-content $log
$x = 0
"$date2012 files" | add-content $log
Get-Childitem -Path $path -Recurse | Where-Object {$_.LastWriteTime -gt (12/31/2011) -AND $_LastWriteTime -lt (01/01/2013) |
ForEach {
$filename = $_.fullname
$x++
"$filename" | add-content $log
}
"$date2012 total files = $x" | add-content $log
}
}
答案 0 :(得分:1)
关键问题:Where子句中的括号是不平衡的,管道已损坏。
其他修正:
无论如何,这是一个固定版本,转换为一个函数,所以你可以选择任何路径,年份,并选择日志输出文件夹
function YearLog {
param(
[Parameter(Mandatory=$true)][String]$Path,
[Parameter(Mandatory=$true)][String]$LogFolder,
[Parameter(Mandatory=$true)][Int]$Year
)
$log = '{0}\FileLog-{1}.txt' -f $LogFolder, $Year
if(Test-Path -Path:$log) {
Remove-Item -Force -Path:$log
}
'Files Found for {0}:' -f $Year | add-content $log
Get-Childitem -Path $Path -Recurse |
Where-Object { ($_.LastWriteTime.Year -gt ($Year-1)) -AND ($_.LastWriteTime.Year -lt ($Year+1)) } |
ForEach -Begin { $x = 0 } -Process {
$x++ | Out-Null
$_.FullName | add-content $log
}
'Total Found for {0}: {1}' -f $year, $x | add-content $log
'Log written for items in {0} for {1}: {2}' -f $Path, $Year, $log | Write-Host
}
<# Usage:
YearLog -Path:$ENV:ProgramFiles -LogFolder:$ENV:TEMP -Year:2012
#>