使用PowerShell我想监视应用程序错误日志的文件大小,并将每24分钟的大小写入.csv文件。我不确定我的代码中的问题在哪里。任何帮助将不胜感激。谢谢。
$a = dir c:\users\rob\desktop\testharness\TestFile.txt
$a | Add-Member -MemberType AliasProperty -Name FileLength -Value Length
$b = $a.FileLength
$MaxSamples = 1440
$SampleInterval = 1
write-host "starting job"
Start-Job {
Param($jobs, $sample, $interval)
$jobs
$sample
$interval
Get-Counter $jobs -MaxSamples $sample -SampleInterval $interval |
Export-Counter -path $home\FileSize.csv -FileFormat CSV -Force
} -Name test -ArgumentList @($b, $MaxSamples, $SampleInterval)
Wait-Job test | Remove-Job
write-host "job done"
答案 0 :(得分:1)
这样的事情会起作用吗?
$LogFile = 'c:\Temp\log.txt'
$Csv = 'c:\Temp\LogFileSize.csv'
$MinsIn24Hours = 1440
"Time,Size" | Out-File $Csv
for ($i = 0; $i -lt $MinsIn24Hours; $i++) {
$now = Get-Date -format "dd-MMM-yyyy HH:mm"
# Get file size and confirt to minutes
$Size = (Get-Item $LogFile).Length / 1KB
"$Now,$Size KB" | Out-File $Csv -Append
Start-Sleep -Seconds 60
}
注意:因为每次迭代都需要一些时间来执行,所以在24小时内可能会丢失几秒钟:
Measure-Command{
$now = Get-Date -format "dd-MMM-yyyy HH:mm"
$Size = (Get-Item $LogFile).Length / 1KB
"$Now,$Size KB" | Out-File $Csv -Append
}
11毫秒* 1440 = 15.84秒