gygon

Monitoring the Event Log with PowerShell

Posted by gygon on Saturday, March 21st, 2009

One of my goals with learning more about PowerShell is to be able to monitor the event logs on servers and notify me via email when certain events happen. The system I’m looking to monitor are not part of a domain, are in remote locations on isolated networks. Some of the main things I’m looking [...]

continue reading

PowerBoots for PowerShell

Posted by gygon on Friday, March 20th, 2009

Came across a post today on an introduction to PowerBoots. WOW!
I’m already thinking of more stuff I can automate to make my life (and some others in the office) a lot easier. Definitely need to learn more PowerShell & Boots!

continue reading

Email a web page through PowerShell

Posted by gygon on Thursday, March 19th, 2009

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 [...]

continue reading

Email a web page through PowerShell

Posted by gygon on Thursday, March 19th, 2009

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

Posted in: PowerShell, Tech.

One Response to “Email a web page through PowerShell”

  1. mark Says:

    I want to say – thank you for this!

Leave a Reply