gygon

PowerShell Ping Test

Posted by gygon on Wednesday, November 16th, 2011

Used this little script the other day for a basic network test to an access point I was testing. It just pings the device every 10 seconds (or whatever you set it at) and only outputs when there is an error and the time it occurred. Made for an easy way to leave this running over […]

continue reading

Using PowerShell to monitor a web server

Posted by gygon on Monday, November 14th, 2011

A 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 web sites/servers and make sure they were up and running and email an alert if they were down or the size of […]

continue reading

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

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.

2 Responses to “Email a web page through PowerShell”

  1. mark Says:

    I want to say – thank you for this!

  2. gygon » Blog Archive » Using PowerShell to monitor a web server Says:

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