发送PowerShell输出的电子邮件

时间:2017-07-10 13:24:26

标签: powershell email

我是PowerShell的新手,我正在运行Windows Server 2012 R2。下面的脚本给了我输出我想要的但是在消息中输出没有出现,它说Body不能是" null"或"空"。

$fileToLocate = "*"
$Directories = @(
"C:\CtvFTPSite\AE1\ASN\Undelivered\",
"C:\CtvFTPSite\AE1\Invoices\Undelivered\",
"C:\CtvFTPSite\AE1\SO\Undelivered\",
"C:\CtvFTPSite\AE2\ASN\Undelivered\",
"C:\CtvFTPSite\AE2\Invoices\Undelivered\",
"C:\CtvFTPSite\AE2\SO\Undelivered\",
"C:\CtvFTPSite\BH1\ASN\Undelivered\",
"C:\CtvFTPSite\BH1\Invoices\Undelivered\",
"C:\CtvFTPSite\BH1\SO\Undelivered\",
"C:\CtvFTPSite\KW1\ASN\Undelivered\",
"C:\CtvFTPSite\KW1\Invoices\Undelivered\",
"C:\CtvFTPSite\KW1\SO\Undelivered\",
"C:\CtvFTPSite\OM1\ASN\Undelivered\",
"C:\CtvFTPSite\OM1\Invoices\Undelivered\",
"C:\CtvFTPSite\OM1\SO\Undelivered\",
"C:\CtvFTPSite\SA1\ASN\Undelivered\",
"C:\CtvFTPSite\SA1\Invoices\Undelivered\",
"C:\CtvFTPSite\SA1\SO\Undelivered\"
)

$filesUndelivered = Join-Path -Path $Directories -ChildPath $fileToLocate | Where-Object{Test-Path $_} | ForEach-Object{
    Get-ChildItem $Directories -Recurse | % { Write-Host $_.FullName }
 }

$Max_mins = "-5"
$Curr_date = get-date
$username = "myusername@myemail.com"
$password = Get-Content C:\security\string.txt | ConvertTo-SecureString
$cred = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $password

 $localIpAddress = $(ipconfig | where {$_ -match 'IPv4.+\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' } | out-null; $Matches[1])
 [string]$messagebody ="$filesUndelivered"
 [string]$titlefailed ="Urgent You Have Files in Undelivered Folder in $localIpAddress in $env:computername"
 $portno = "25"
 $smtpsrv = "mail.server.com"
 $smtpto = "myusername@myemail.com"
 $smtpfrom ="myusername@myemail.com"
 if ($filesfailed.Count)
 {     
       foreach ($file in $filesUndelivered) {[string]$messagebody += $file.FullName + "`r`n"}
       Send-MailMessage -To $smtpto -From $smtpfrom -port $portno -SmtpServer $smtpsrv  -Credential $cred -Subject $titlefailed -Body $messagebody    
    }

1 个答案:

答案 0 :(得分:0)

固定

$fileToLocate = "*"
$failedpath = @(
"C:\CtvFTPSite\AE1\ASN\Undelivered\",
"C:\CtvFTPSite\AE1\Invoices\Undelivered\",
"C:\CtvFTPSite\AE1\SO\Undelivered\",
"C:\CtvFTPSite\AE2\ASN\Undelivered\",
"C:\CtvFTPSite\AE2\Invoices\Undelivered\",
"C:\CtvFTPSite\AE2\SO\Undelivered\",
"C:\CtvFTPSite\BH1\ASN\Undelivered\",
"C:\CtvFTPSite\BH1\Invoices\Undelivered\",
"C:\CtvFTPSite\BH1\SO\Undelivered\",
"C:\CtvFTPSite\KW1\ASN\Undelivered\",
"C:\CtvFTPSite\KW1\Invoices\Undelivered\",
"C:\CtvFTPSite\KW1\SO\Undelivered\",
"C:\CtvFTPSite\OM1\ASN\Undelivered\",
"C:\CtvFTPSite\OM1\Invoices\Undelivered\",
"C:\CtvFTPSite\OM1\SO\Undelivered\",
"C:\CtvFTPSite\SA1\ASN\Undelivered\",
"C:\CtvFTPSite\SA1\Invoices\Undelivered\",
"C:\CtvFTPSite\SA1\SO\Undelivered\"
)

$Max_mins = "-5"
$Curr_date = get-date
$username = "username@email.com"
$password = Get-Content C:\security\string.txt | ConvertTo-SecureString
$cred = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $password
 $filesfailed = Get-ChildItem -Path $failedpath | Where{$_.CreationTime -lt ($Curr_date).addminutes($Max_mins)}
 $localIpAddress = $(ipconfig | where {$_ -match 'IPv4.+\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' } | out-null; $Matches[1])
 [string]$messagebody =""
 [string]$titlefailed ="Urgent You Have Files in Undelivered Folder in $localIpAddress in $env:computername"
 $portno = "25"
 $smtpsrv = "mail.server.net"
 $smtpto = "username@email.com"
 $smtpfrom ="username@email.com"
 if ($filesfailed.Count)
 {     
       foreach ($file in $filesfailed) {[string]$messagebody += $file.FullName + "`r`n"}
       Send-MailMessage -To $smtpto -From $smtpfrom -port $portno -SmtpServer $smtpsrv  -Credential $cred -Subject $titlefailed -Body $messagebody    
    }