我有这个命令吐出一个pdf文件,你可以看到它保存在我的C:驱动器中:
$DownloadReportResponse = Invoke-WebRequest -Method Post -Uri $DownloadReportUrl -ContentType "application/json" -Headers $headers -Body $DownloadReportRequestBody -OutFile "C:\Neoload\TestResult-($DateForTestResult).pdf" -TransferEncoding "deflate"
有没有办法编写脚本,以便它也可以将文件发送到某个电子邮件地址?
我有这个示例脚本来发送电子邮件,我将在哪里分配文件,以便将其附加到电子邮件中?
###########Define Variables########
$fromaddress = "myGmail@gmail.com"
$toaddress = "myGmail@gmail.com"
#$CCaddress = "myGmail@gmail.com"
$Subject = "ACtion Required"
$body = get-content .\content.htm
$attachment = $DownloadReportResponse
$smtpserver = "smtp.labtest.com"
####################################
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.CC.Add($CCaddress)
$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
答案 0 :(得分:1)
您可以使用Send-MailMessage
命令构建。只需确保附件是文件的路径
$EmailSettings=@{
SMTPServer="smtp.labtest.com"
From="myGmail@gmail.com"
To="myGmail@gmail.com"
Subject="ACtion Required"
Body=(get-content .\content.htm )
Attachments="C:\Neoload\TestResult-($DateForTestResult).pdf"
BodyAsHtml=$true
}
Send-MailMessage @EmailSettings