title: Powershell script to email with attachment from WebRequest Response
body: <p>I have this command which spits out a pdf file and you can see that it is saving in my C: drive: </p>
  
  <pre><code>$DownloadReportResponse = Invoke-WebRequest -Method Post -Uri $DownloadReportUrl -ContentType "application/json" -Headers $headers -Body $DownloadReportRequestBody -OutFile "C:\Neoload\TestResult-($DateForTestResult).pdf" -TransferEncoding "deflate"
  </code></pre>
  
  <p>Is there way to script this so it can send the file to certain email address as well? </p>
  
  <p>I have this example script to send email, where would I assign the file so it is attached in the email? </p>
  
  <pre><code>###########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) 
  </code></pre>