Powershell - 从循环中获取输出文件,使用 7zip 存档,通过电子邮件发送,然后终止原始输出
Powershell - Take output file from loop, archive with 7zip, email, and then kill the original output
我有一个我放在一起的脚本,在大多数情况下,该脚本执行我想要它执行的操作,它命中了一个服务器列表,寻找超过 25 小时的日志文件(表明另一个脚本没有完成它的工作),这在测试(1 到 5 台服务器)中完美运行,但是一旦我在 150 多台服务器上松开它我想在这个环境中检查,文件大小增加,并且电子邮件进程由于文件大小超过 10mb 而失败。
所以现在我需要一种压缩结果的方法,我想使用 7zip,但出于某种原因,我无法全神贯注地思考如何完成我正在尝试做的事情。
如有任何帮助,我们将不胜感激。
这是我目前的脚本。
# Specify where the list of servers is located.
$SL = get-content C:\Scripts\lists\AgingLogsServers.txt
# Define logfile age to report on.
$limit = (Get-Date).AddHours(-25)
# Define the current date & time.
$filedate = get-date -f "MM.dd.yy_hh.mm.ss"
$emldate = get-date -f "MM.dd.yy"
# Variable to add current date & time to saved filename.
$filename = "AgingReport_$($filedate).log"
# Files or patterns to exclude from the scan.
$excluded = @(".exe")
# Specify SMTP server
$smtpserver = "mail.yourserver.com"
# Loop to process each server in the pool.
Foreach ($Server in $SL){
$c1++
Write-Progress -Activity 'Looking for Logfiles in excess of 25 hours old' -Status "Processing $($c1) of $($SL.count)" -CurrentOperation $Server -PercentComplete (($c1/$SL.count) * 100)
If (Test-Path "\$Server\logs") {$SP1 = "\$Server\Logs"}
Else {$SP1 = "\$Server\D-Logs"}
Get-ChildItem -ErrorAction SilentlyContinue -Path $SP1 -Exclude $excluded -Include *.zip, *.7z, *.log -Recurse | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Foreach-Object {write-output $_.CreationTime $server $_.fullname} | Out-file C:\Scripts\data$filename -Append -Width 160
}
# Zip the $filename and remove the original
# And this is where I believe a 7zip process would go to compress the result file, then I can reference that file and path in the Send-MailMessage line.
# Email the results.
Send-MailMessage -From "Aging.Logs@yourhost.com" -To "user@yourhost.com" -Subject "Aging Logs report for $emldate" -Body "Attached is the listing of aging logs for the environment for $emldate" -SmtpServer $smtpserver -Attachments C:\Scripts\data$filename
# Give the logfile time to release from the email process.
Start-Sleep -s 120
# Clean up the results file.
#Remove-Item C:\Scripts\data\AgingReport*
运行 7-Zip 非常简单。语法是
7z.exe a <archive path> <file to zip path>
这很简单,我们只需要知道 7z.exe 在哪里。因此,我们将使 PowerShell 找到它,然后使用调用运算符 &
和这些参数执行它(顺便说一句,'a' 意味着我们a将文件添加到存档)。然后我们清理源文件,并通过电子邮件发送存档。
# Zip the $filename and remove the original
# Find 7-Zip executable
Zip = gci c:\Program* -include '7z.exe' -recurse -ea 4|select -first 1 -expand fullname
# Define archive name and path
$ZipFile = "C:\Scripts\data$filename" -replace "...$","zip"
# Perform Zip
& Zip a "$ZipFile" "C:\Scripts\data$filename" | Out-Null
# Remove source file
Remove-Item -Path "C:\Scripts\data$filename"
# Email the results.
Send-MailMessage -From "Aging.Logs@yourhost.com" -To "user@yourhost.com" -Subject "Aging Logs report for $emldate" -Body "Attached is the listing of aging logs for the environment for $emldate" -SmtpServer $smtpserver -Attachments $ZipFile
顺便说一下,您的存档将与您的日志文件相同,但文件扩展名为 .zip 而不是 .log。
@TheMadTechnician,非常感谢你的帮助post,我尝试整合你给我的东西,但是无论我怎么去做我都无法得到想要的动作,我按照方向你提供并最终能够让它工作,这里是完成我想要它做的所有事情的代码,以防其他人想要完成同样的事情。
<#
Script: AgingLogQuery.ps1
Author: Xander J.
Date: 11/12/2015
Aging log query checks the logs and d-logs shares contained within a text file to see if there are any logfiles older than 25
hours old,if it finds a logfile that is older than 25 hours old it passes the server name, the full path and filename and the
files age to the AgingReport log file.
After checking all of the servers in the list, the script archives the logfile, removes the original logfile, emails the
archive as an attachment, then waits a specified amount of time to remove the archive file.
#>
# Specify where the list of servers is located.
$SL = get-content C:\Scripts\lists\AgingLogsServers.txt
# Define logfile age to report on.
$limit = (Get-Date).AddHours(-25)
# Define the current date & time.
$filedate = get-date -f "MM.dd.yy_hh.mm.ss"
$emldate = get-date -f "MM.dd.yy"
# Variable to add current date & time to saved filename.
$filename = "AgingReport_$($filedate).log"
# Files or patterns to exclude from the scan.
$excluded = @("*.exe*")
# Specify SMTP server
$smtpserver = "mail.email.com"
#Get script path
$ScriptPath = Split-Path -Path $($MyInvocation.MyCommand.Definition)
#Get the path for 7za.exe
$zipexe = $ScriptPath + "za.exe"
set-alias sz $zipexe
$archive = "AgingReport_$($filedate).zip"
# Loop to process each server on the list.
Foreach ($Server in $SL){
$c1++
Write-Progress -Activity 'Looking for Logfiles in excess of 25 hours old' -Status "Processing $($c1) of $($SL.count)" -CurrentOperation $Server -PercentComplete (($c1/$SL.count) * 100)
If (Test-Path "\$Server\logs") {$SP1 = "\$Server\Logs"}
Else {$SP1 = "\$Server\D-Logs"}
Get-ChildItem -ErrorAction SilentlyContinue -Path $SP1 -Exclude $excluded -Include *.zip, *.7z, *.log -Recurse | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Foreach-Object {write-output $_.CreationTime $server $_.fullname} | Out-file C:\Scripts\data$filename -Append -Width 160
}
# Zip the $filename
& sz a -mmt -tzip c:\Scripts\Data$archive C:\Scripts\data\AgingReport*.log -stl
# Clean up the results file.
Remove-Item -Force C:\Scripts\data$filename
# Email the results.
Send-MailMessage -From "Aging.Logs@echopass.com" -To "user@email.com" -Subject "Aging Logs report for $emldate" -Body "Attached is the listing of aging logs for the environment for $emldate" -SmtpServer $smtpserver -Attachments C:\Scripts\data$archive
# Give the logfile time to release from the email process.
Start-Sleep -s 15
# Clean up the results file.
Remove-Item -Force C:\Scripts\data$archive
我有一个我放在一起的脚本,在大多数情况下,该脚本执行我想要它执行的操作,它命中了一个服务器列表,寻找超过 25 小时的日志文件(表明另一个脚本没有完成它的工作),这在测试(1 到 5 台服务器)中完美运行,但是一旦我在 150 多台服务器上松开它我想在这个环境中检查,文件大小增加,并且电子邮件进程由于文件大小超过 10mb 而失败。
所以现在我需要一种压缩结果的方法,我想使用 7zip,但出于某种原因,我无法全神贯注地思考如何完成我正在尝试做的事情。
如有任何帮助,我们将不胜感激。
这是我目前的脚本。
# Specify where the list of servers is located.
$SL = get-content C:\Scripts\lists\AgingLogsServers.txt
# Define logfile age to report on.
$limit = (Get-Date).AddHours(-25)
# Define the current date & time.
$filedate = get-date -f "MM.dd.yy_hh.mm.ss"
$emldate = get-date -f "MM.dd.yy"
# Variable to add current date & time to saved filename.
$filename = "AgingReport_$($filedate).log"
# Files or patterns to exclude from the scan.
$excluded = @(".exe")
# Specify SMTP server
$smtpserver = "mail.yourserver.com"
# Loop to process each server in the pool.
Foreach ($Server in $SL){
$c1++
Write-Progress -Activity 'Looking for Logfiles in excess of 25 hours old' -Status "Processing $($c1) of $($SL.count)" -CurrentOperation $Server -PercentComplete (($c1/$SL.count) * 100)
If (Test-Path "\$Server\logs") {$SP1 = "\$Server\Logs"}
Else {$SP1 = "\$Server\D-Logs"}
Get-ChildItem -ErrorAction SilentlyContinue -Path $SP1 -Exclude $excluded -Include *.zip, *.7z, *.log -Recurse | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Foreach-Object {write-output $_.CreationTime $server $_.fullname} | Out-file C:\Scripts\data$filename -Append -Width 160
}
# Zip the $filename and remove the original
# And this is where I believe a 7zip process would go to compress the result file, then I can reference that file and path in the Send-MailMessage line.
# Email the results.
Send-MailMessage -From "Aging.Logs@yourhost.com" -To "user@yourhost.com" -Subject "Aging Logs report for $emldate" -Body "Attached is the listing of aging logs for the environment for $emldate" -SmtpServer $smtpserver -Attachments C:\Scripts\data$filename
# Give the logfile time to release from the email process.
Start-Sleep -s 120
# Clean up the results file.
#Remove-Item C:\Scripts\data\AgingReport*
运行 7-Zip 非常简单。语法是
7z.exe a <archive path> <file to zip path>
这很简单,我们只需要知道 7z.exe 在哪里。因此,我们将使 PowerShell 找到它,然后使用调用运算符 &
和这些参数执行它(顺便说一句,'a' 意味着我们a将文件添加到存档)。然后我们清理源文件,并通过电子邮件发送存档。
# Zip the $filename and remove the original
# Find 7-Zip executable
Zip = gci c:\Program* -include '7z.exe' -recurse -ea 4|select -first 1 -expand fullname
# Define archive name and path
$ZipFile = "C:\Scripts\data$filename" -replace "...$","zip"
# Perform Zip
& Zip a "$ZipFile" "C:\Scripts\data$filename" | Out-Null
# Remove source file
Remove-Item -Path "C:\Scripts\data$filename"
# Email the results.
Send-MailMessage -From "Aging.Logs@yourhost.com" -To "user@yourhost.com" -Subject "Aging Logs report for $emldate" -Body "Attached is the listing of aging logs for the environment for $emldate" -SmtpServer $smtpserver -Attachments $ZipFile
顺便说一下,您的存档将与您的日志文件相同,但文件扩展名为 .zip 而不是 .log。
@TheMadTechnician,非常感谢你的帮助post,我尝试整合你给我的东西,但是无论我怎么去做我都无法得到想要的动作,我按照方向你提供并最终能够让它工作,这里是完成我想要它做的所有事情的代码,以防其他人想要完成同样的事情。
<#
Script: AgingLogQuery.ps1
Author: Xander J.
Date: 11/12/2015
Aging log query checks the logs and d-logs shares contained within a text file to see if there are any logfiles older than 25
hours old,if it finds a logfile that is older than 25 hours old it passes the server name, the full path and filename and the
files age to the AgingReport log file.
After checking all of the servers in the list, the script archives the logfile, removes the original logfile, emails the
archive as an attachment, then waits a specified amount of time to remove the archive file.
#>
# Specify where the list of servers is located.
$SL = get-content C:\Scripts\lists\AgingLogsServers.txt
# Define logfile age to report on.
$limit = (Get-Date).AddHours(-25)
# Define the current date & time.
$filedate = get-date -f "MM.dd.yy_hh.mm.ss"
$emldate = get-date -f "MM.dd.yy"
# Variable to add current date & time to saved filename.
$filename = "AgingReport_$($filedate).log"
# Files or patterns to exclude from the scan.
$excluded = @("*.exe*")
# Specify SMTP server
$smtpserver = "mail.email.com"
#Get script path
$ScriptPath = Split-Path -Path $($MyInvocation.MyCommand.Definition)
#Get the path for 7za.exe
$zipexe = $ScriptPath + "za.exe"
set-alias sz $zipexe
$archive = "AgingReport_$($filedate).zip"
# Loop to process each server on the list.
Foreach ($Server in $SL){
$c1++
Write-Progress -Activity 'Looking for Logfiles in excess of 25 hours old' -Status "Processing $($c1) of $($SL.count)" -CurrentOperation $Server -PercentComplete (($c1/$SL.count) * 100)
If (Test-Path "\$Server\logs") {$SP1 = "\$Server\Logs"}
Else {$SP1 = "\$Server\D-Logs"}
Get-ChildItem -ErrorAction SilentlyContinue -Path $SP1 -Exclude $excluded -Include *.zip, *.7z, *.log -Recurse | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Foreach-Object {write-output $_.CreationTime $server $_.fullname} | Out-file C:\Scripts\data$filename -Append -Width 160
}
# Zip the $filename
& sz a -mmt -tzip c:\Scripts\Data$archive C:\Scripts\data\AgingReport*.log -stl
# Clean up the results file.
Remove-Item -Force C:\Scripts\data$filename
# Email the results.
Send-MailMessage -From "Aging.Logs@echopass.com" -To "user@email.com" -Subject "Aging Logs report for $emldate" -Body "Attached is the listing of aging logs for the environment for $emldate" -SmtpServer $smtpserver -Attachments C:\Scripts\data$archive
# Give the logfile time to release from the email process.
Start-Sleep -s 15
# Clean up the results file.
Remove-Item -Force C:\Scripts\data$archive