我需要从filezilla日志中提取日期,时间和文件名,以便将其插入数据库。
这是我到目前为止所拥有的:
Select-String "C:\Scripts\testing\SSIS\testfiles\fzs-2014-04-15.log" -Pattern "STOR" -AllMatches
Foreach($line in $lines){
$line = $line -replace "C:\Path-to-logs\filezilla.log.*\) ",""
$line = $line -replace "[AM|PM].* STOR ",""
$line -split " "
}
我得到以下结果:
C:\path-to-logs\filezilla.log:114:(003173) 4/15/2014 3:04:20 AM - cwwsfge (192.168.250)> STOR NewFileupload.TXT
C:\path-to-logs\filezilla.log.log:210:(000182) 4/15/2014 6:21:21 AM - refect(192.168.250)> STOR Testfile_20140415
C:\path-to-logs\filezilla.log.log:662:(000179) 4/15/2014 6:27:13 AM - refect (192.168.2)> STOR FreeMoney.txt
所以一旦在foreach中获取该信息,如何获得该信息?
谢谢!
进行编辑以匹配上面的更改。我认为您仍然可以通过一个正则表达式拆分,然后挑选所需的元素。试试这个:
Foreach($line in $lines){
$parts = $line -split '\) | - |\> STOR |:\('
$logdate = [DateTime]::Parse($parts[2])
$filename = $parts[4]
write-host "Filename: $filename"
write-host "Filedate: $($logdate.Date.ToShortDateString())"
write-host "Filetime: $($logdate.TimeOfDay)`n"
}
基本上,这是匹配“)”,“-”,“> STOR”或“ :(”,然后将它们分开。如果格式一致,则每个遍次中应该有5个元素。我向其中添加了一些代码选取文件名并解析日期,并生成上面示例中的值:
Filename: NewFileupload.TXT
Filedate: 4/15/2014
Filetime: 03:04:20
Filename: Testfile_20140415
Filedate: 4/15/2014
Filetime: 06:21:21
Filename: FreeMoney.txt
Filedate: 4/15/2014
Filetime: 06:27:13
使用先前的答案,我构建了一个脚本,以制表符分隔的格式输出。
$logpath = "C:\Program Files (x86)\FileZilla Server\Logs"
# combine all the logs into one file
gci "$logpath\*.log" | sort LastWriteTime | % {$(Get-Content $_)} | Set-Content "$logpath\combo.txt"
# scan lines only containing login,file upload, and download
$lines = Select-String "$logpath\combo.txt" -Pattern "> RETR|> STOR|230 Logged on" -AllMatches
Foreach($line in $lines){
$parts = $line -split '\) | - |\> |:\('
$logdate = [DateTime]::Parse($parts[2])
$filename = $parts[4].Replace("230 Logged","Logged")
$user = $parts[3]
$user,$filename,$logdate -join "`t" | Out-File -FilePath "$logpath\output.txt" -Append -Width 500
}