email - Powershell-从循环中获取输出文件,使用7zip,电子邮件存档,然后终止原始输出

标签 email powershell 7zip

我有一个整合在一起的脚本,在大多数情况下,该脚本可以实现我想要的功能,它会打入服务器列表,查找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
$7Zip = 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
& $7Zip 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。

关于email - Powershell-从循环中获取输出文件,使用7zip,电子邮件存档,然后终止原始输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33661629/

相关文章:

Powershell - Azure Data Lake Store 中的递归

azure - Azcopy 支持管道吗?

python - 无法通过 ftp 在 python 中提取 zip

c# - 如何处理 system.net.mail.smtpfailedrecipientsexception 失败的收件人

php - 使用 AJAX 在 PHP 中发送电子邮件

java - 如何使用java发送电子邮件

windows - 获取帮助和/?之间的区别在 powershell 中

.net - 在 Powershell v3 中无需调用外部 EXE 即可确定 .NET 程序集是否为混合模式

batch-file - 使用 7 zip 命令行解压缩时重命名文件

php - Centos 7、Citadel 电子邮件、服务器、phpmailer 和 S/MIME