作为PowerShell正则表达式模式中的变量扩展方面的一个新手,在正则表达式模式下扩展变量不起作用
我想要做的是:
- 扫描日志文件已经过两次时限
- 对于每个日志文件之间的变化,我得到这表明它引用的日期名称的一部分。
该日期存储在变量$filedate
中。 然后走线槽每一行日志文件 每当我找到一条线,看起来像:
14:00:15 blablabla
在一档名为blabla20130620.log
我想的是,数据线变得
2013-06-20 14:00:15 blablabla
应该写输出追加模式到一个文本文件(连接不同的日志文件)
这是我到现在为止(我现在在沙箱中测试,所以没有评论等...)
$Logpath = "o:\Log"
$prevcheck="2013-06-24 19:27:14"
$currenttd="{0:yyyy-MM-dd HH:mm:ss}" -f (get-date)
$batch = 1000
[regex]$match_regex = '^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)'
If (Test-Path "$Logpath\test.txt"){
Remove-Item "$Logpath\test.txt"
}
$files=Get-ChildItem $LogPath\*.log | Where-Object { $_.LastWriteTime -ge "$prevcheck" - and $_.LastWriteTime -le "$currenttd" -and !$_.PSIsContainer }
foreach ($file in $files)
{
$filedate=$file.Name.Substring(6,4) + "-" + $file.Name.Substring(10,2) + "-" + $file.Name.Substring(12,2)
## This doesn't seem to work fine
## results look like:
## "$filedate" 14:00:15 blablabla
$replace_regex = '"$filedate" $_'
## I tried this too, but without success
## The time seems to dissappear now
## results look like:
## 2013-06-20 blablabla
#$replace_regex = iex('$filedate' + $_)
(Get-Content $file.PSPath -ReadCount $batch) |
foreach-object {if ($_ -match $match_regex) { $_ -replace $match_regex, $replace_regex} else { $_ }}|
out-file -Append "o:\log\test.txt"
哇...谢谢一堆。现在我会试着更好地理解它。 – davidthijs