Html Powershell脚本,用于获取最近2天的IIS日志文件并检查文件的大小差异或增长

Html Powershell脚本,用于获取最近2天的IIS日志文件并检查文件的大小差异或增长,html,powershell,iis,logging,Html,Powershell,Iis,Logging,使用Powershell脚本获取最近2天的IIS日志文件,检查文件和电子邮件的大小差异或增长,或生成带有值的html电子邮件 我只做了几个步骤,但在获取两个日志文件和html部分之间的差异时遇到了问题 这是我的密码 # Set your backup path $BackupPath = "D:\log files\" # Get the log file created today $BackupToday = Get-ChildItem $BackupPath -Filter "*.log

使用Powershell脚本获取最近2天的IIS日志文件,检查文件和电子邮件的大小差异或增长,或生成带有值的html电子邮件

我只做了几个步骤,但在获取两个日志文件和html部分之间的差异时遇到了问题

这是我的密码

# Set your backup path
$BackupPath = "D:\log files\"

# Get the log file created today
$BackupToday = Get-ChildItem $BackupPath -Filter "*.log" | Where-Object {$_.CreationTime.Date -eq (Get-Date).Date} | %{[int]($_.length/1KB)} 

# Get the log file created yesterday
$BackupYDay = Get-ChildItem $BackupPath -Filter "*.log" |  Where-Object {$_.CreationTime.Date -eq ((Get-Date).AddDays(-1)).Date} | %{[int]($_.length/1KB)}

# Compare the two files based on the size
$compare = ($BackupYDay - $BackupToday)

Write-Host($BackupToday)
Write-Host($BackupYDay)
Write-Host($compare)

不太清楚您所说的“和电子邮件或生成带有值的html电子邮件”是什么意思。 为此,请查看cmdlet

如果要发送电子邮件的内容非常简单,只需显示两个文件大小的差异(如果有),可以执行以下操作:

# Set your backup path
$BackupPath = "D:\log files"

# get the logfiles and select only the latest two
$logNewest, $logBefore = Get-ChildItem -Path $BackupPath -Filter "*.log" -File | Sort-Object CreationTime -Descending | Select-Object -First 2
$sizeDiff = $logNewest.Length - $logBefore.Length

$difference = switch ([math]::Sign($sizeDiff)) {
     1 { 'File {0} is {1:N2}KB larger than file {2}'  -f $logNewest.Name, [math]::Abs($sizeDiff / 1KB ), $logBefore.Name }
    -1 { 'File {0} is {1:N2}KB smaller than file {2}' -f $logNewest.Name, [math]::Abs($sizeDiff / 1KB ), $logBefore.Name }
     0 { 'File {0} and file {1} are of equal size'    -f $logNewest.Name, $logBefore.Name }
}

Write-Host $difference
结果:

文件Today.log比文件dayed.log大0,01KB


删除
bash
标记,因为您的问题与此无关。如果合适,您可能需要添加批处理。您需要什么帮助还不太清楚。你能发布一个你尝试过的和不起作用的最低限度的描述吗?是否保证您每天只有一个文件?我们没有收到您的来信。。我的回答解决了你的问题吗?如果是,请点击✓ 在左边。这将帮助其他有类似问题的人更容易地找到它。如果没有,请编辑您的问题并解释什么不适合您。