Email a web page through PowerShell
On a hosted web based app, I needed to go to a page every day to kick off a process. Nothing needed to be done on the page other than to open it and it would do it’s daily stuff. Easy, but a pain to have to do every day. And even more so on the weekend since the page is only accessible from the office so it had the extra steps to remotely connect and then open the page.
So I found this great little PowerShell function on the MSDN site that let’s me use PowerShell to open the web page and get the results. Combining this with a function I already had in use to send html emails, I’m able to have a script scheduled to run and email the results page to me on a daily basis. Now the daily manual task has been replaced by a small script that is completely automated.
function Get-Web($url, [switch]$self, $credential, $toFile, [switch]$bytes) { #.Synopsis # Downloads a file from the web #.Description # Uses System.Net.Webclient (not the browser) to download data # from the web. #.Parameter self # Uses the default credentials when downloading that page (for downloading intranet pages) #.Parameter credential # The credentials to use to download the web data #.Parameter url # The page to download (e.g. www.msn.com) #.Parameter toFile # The file to save the web data to #.Parameter bytes # Download the data as bytes #.Example # # Downloads www.live.com and outputs it as a string # Get-Web http://www.live.com/ #.Example # # Downloads www.live.com and saves it to a file # Get-Web http://wwww.msn.com/ -toFile www.msn.com.html $webclient = New-Object Net.Webclient if ($credential) { $webClient.Credential = $credential } if ($self) { $webClient.UseDefaultCredentials = $true } if ($toFile) { if (-not "$toFile".Contains(":")) { $toFile = Join-Path $pwd $toFile } $webClient.DownloadFile($url, $toFile) } else { if ($bytes) { $webClient.DownloadData($url) } else { $webClient.DownloadString($url) } } } function SendEmail($SendTo,$SendSubject,$SendMessage) { $SmtpClient = new-object system.net.mail.smtpClient $MailMessage = New-Object system.net.mail.mailmessage $SmtpClient.Host = "mail.plazahotelsuites.com" $FromAddress = new-object System.Net.Mail.MailAddress("sender@domain.com", "Sender Name") $mailmessage.sender = $FromAddress $mailmessage.from = $FromAddress $mailmessage.To.add($SendTo) $mailmessage.Subject = $SendSubject $mailmessage.IsBodyHtml = 1 # The line below will pull the message body from the specified file # This is currently commented out to include the file here so the Folio ID can be embedded # $mailmessage.Body = Get-Content .\emailbody.htm $mailmessage.Body = $SendMessage $Credentials = new-object System.Net.networkCredential $Credentials.UserName = "sender@domain.com" $Credentials.Password = "password" $SMTPClient.Credentials = $Credentials $SMTPClient.Port = 25 $smtpclient.Send($mailmessage) } # Set who the email goes to and what the subject will be. $sendtoemail = 'user@domain.com' $SendSubject = 'Message subject here' $webpagecall = Get-Web 'http://www.somewebsite.com' # Uncomment the line below to output the retrieved web page to the screen # write $webpagecall SendEmail $sendtoemail $SendSubject $webpagecall
April 15th, 2009 at 12:39 am
I want to say – thank you for this!
November 14th, 2011 at 9:18 pm
[...] couple years ago I posted about a PowerShell script that would read in a web page. I used that to setup a basic web site test to go through a list of [...]