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

Portable Ubuntu

Monday, April 6th, 2009 - Tech, Windows

There was a post on Lifehacker the other day about Portable Ubuntu for Windows which lets you run Ubuntu within Windows! So you can still be in Windows and have access to apps within Ubuntu….without even needing to install. It runs self-contained and can even be run from a thumb drive.

SecondRun.tv for Windows Media Center

Friday, March 27th, 2009 - Tech, Windows

Just came across this extension for Windows Media Center called SecondRun.tv that looks really cool. We’ve been considering cutting out the cable service in the house and just going with the over the air tv and online shows and this makes that change one step closer. It allows you to select and watch content from numerous online sites through MCE. Check out the site for more info and to download…still in the beta stage but it’s a start.

Custom Logon Screen in Windows 7

Wednesday, March 25th, 2009 - Tech, Windows

Looks like Windows 7 is going to make it a little easier to modify the logon screen. I’ve always used one of the numerous tools out there to do this in the past but after reading a post on WithinWindows it looks like it’s going to be an easy change to do in Windows 7.

Google for Wallpaper

Sunday, March 22nd, 2009 - Tech, Tips

Cool tip from Tekzilla the other day on searching for new desktop wallpaper. In Google Image search, enter: imagesize:1920×1200 <keyword> and you’ll get back images sized just for you.

Monitoring the Event Log with PowerShell

Saturday, March 21st, 2009 - PowerShell, Tech

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 to monitor is the RAID status and drive alerts. While the servers do have the Dell Open Manage Server Administrator (OMSA) on them, it doesn’t support emails so my options seemed to either rely on OMSA to trigger something to run or monitor the event log for the event.

I may setup a trigger as well once I do some more testing but for starters I’m going to monitor the event log for the eventIDs between 2048 and 2368 which should be all of the alerts from OMSA. This will be adjusted along the way I’m sure but it’s a starting point…plus the more events that will trigger it the more testing I can get done.

As I’ve mentioned, I don’t consider myself a programmer by any means…this was put together from the samples and info found on other sites, the two biggest contributors being Windows PowerShell Blog & Dell OMSA Users Guide.

Here’s the script that I’m using, from the Windows PowerShell blog, to parse through the event log and look for the events I’m interested in. The next step will be to take the results and send them, when there’s an event, via email to me.

# Eventlog monitoring script 2006/03/28 JonN
#
# Event logs can contain hundreds of thousands of items, so
# "get-eventlog System | where {<condition>}" can take excessively long
# on production systems.  Plus, the same failures will be reported
# over and over.
# This script will remember your last Index position in the log,
# and only report events which occurred since then.
# It also remembers the TimeGenerated of the oldest log entry,
# so that it can detect when the log has been cleared.
# Example:
# eventlog.msh1 -LogName System -Filter {$_.EventID -eq 4226} -PositionFile "c:\logs\syslogpos.txt"

param (
    [string]$LogName = "System",
    [ScriptBlock]$Filter = {$true},
    [string]$PositionFile = $home + "\" + $LogName + "_position.txt",
    [switch]$Force,
    [switch]$Restart
    )

write-debug "`$LogName = $LogName"
write-debug "`$Filter = '$Filter'"
write-debug "`$PositionFile = '$PositionFile'"
write-debug "`$Force = $Force"
write-debug "`$Restart = $Restart"

[int]$lastCheckedIndex = -1
[DateTime]$lastCheckedTime = [DateTime]::MaxValue

if (!$Restart)
{
    $filecontent = get-content $PositionFile -ea SilentlyContinue
    if ($null -eq $filecontent)
    {
        if (!$Force)
        {
      throw "Position file '$PositionFile' does not exist or could not be opened. Use -Force or -Restart to create a new position file."
        }
        write-verbose "Position file '$PositionFile' does not exist or could not be opened.  Rebuilding position file because -Force was specified."
    }
    else
    {
        write-debug "Position file contains '$filecontent'"
        trap {
            if ($Force)
            {
                write-warning "Content of position file '$PositionFile' is invalid.  Rebuilding position file because -Force was specified."
                $lastCheckedIndex = -1
                $lastCheckedTime = [DateTime]::MaxValue
                continue
            }
      throw "Content of position file '$PositionFile' is invalid.  Terminating operation.  Use -Force or -Restart to rebuild the position file."
        }
        # These lines will throw if the cast fails
        $lastCheckedIndex = $filecontent[0]
        $lastCheckedTime = $filecontent[1]
    }
}

write-debug "`$lastCheckedIndex = $lastCheckedIndex"
write-debug "`$lastCheckedTime = $lastCheckedTime"

# I don't simply call get-eventlog $LogName because I don't want to
# build an array with all the hundreds of thousands of event log entries.
# Instead, I make sure the EventLogEntryCollection is not unrolled.
$log = get-eventlog -List | where {$_.Log -ieq $LogName}
if ($null -eq $log)
{
    throw "Log not found: $LogName"
}

$entries = $log.Entries
if (0 -eq $entries.Count)
{
    write-debug "Log empty: $LogName"
    return
}

$oldestEntryTime = $entries[0].TimeGenerated
if ($oldestEntryTime -gt $lastCheckedTime)
{
    write-verbose "The log appears to have been cleared since it was last checked: $LogName."
    $lastCheckedIndex = -1
}

# The index can diverge from the number of entries
# when the log reaches its maximum size and is configured
# to "Overwrite entries as needed"
$newestEntryIndex = $entries[$entries.Count - 1].Index
$newestEntryTime = $entries[$entries.Count - 1].TimeGenerated

write-debug "Newest entry in log has index $newestEntryIndex"
write-debug "Newest entry in log has TimeGenerated $newestEntryTime"

# $entries.Count could be more than the maximum range "50000 .. 0"
foreach ($i in ($entries.Count - 1) .. 0)
{
    $entry = $entries[$i]
    if ($entry.Index -le $lastCheckedIndex) {
        break
    }
    # This line actually generates the output
    $entry | where $Filter
}

write-debug "Writing index $newestEntryIndex to position file '$PositionFile'"
write-debug "Writing time $newestEntryTime to position file '$PositionFile'"
$newestEntryIndex,$newestEntryTime | set-content $PositionFile

PowerBoots for PowerShell

Friday, March 20th, 2009 - PowerShell, Tech

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!

Email a web page through PowerShell

Thursday, March 19th, 2009 - PowerShell, Tech

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

Samsung SSD System

Wednesday, March 18th, 2009 - Tech

This is definitely better than the typical marketing ads for a computer hard drive. Makes me wonder how the SSD drives work in the new netbooks…looking forward to getting one to try it out.

Grid Junction 1.4

Tuesday, March 17th, 2009 - Tech, Windows Home Server

Grid Junction

Grid Junction

If you have a Windows Home Server connected to a UPS, this is one great little app to have. Grid Junction recently released version 1.4 with some new features and little tweaks. I have to give credit to the creators, this is a solid app that does what it needs and does it well!

ecofont – Less is more

Monday, March 16th, 2009 - Featured, Tech

Thought this was a pretty cool way to conserve a little. Granted most printers have an eco mode that you can use this is another option. ecofont is a font that prints with tiny little circles in the letters of no ink to help cut down on toner. Not sure if it actually lives up to the “up to 20%” claim but it’s a neat idea.