Skip to content
Archive of entries posted on December 2009

Get-EventLog and Get-WMIObject

Recently, we had an occasion to write a process to read event logs on several sql servers to try to determine login times for different sql and Windows logins.  Since we have begun using PowerShell v2.0, and since get-eventlog now has a -computername parameter, it seemed like an obvious solution.

The event message we were interested in looked something like “Login succeeeded for uesr ‘UserName’ ….”.  The code we were trying to use was:

get-eventlog -computername $servername -logname Application -message "Login succeeded for user*" -after ((get-date).AddDays(-1))

I expected that, given a date parameter and a leading string to match wouldn’t be too bad, but this ended up taking several minutes per server.  As there are over a hundred servers to scan, that didn’t work well for us.

We ended up falling back to get-wmiobject.

$BeginDate=[System.Management.ManagementDateTimeConverter]::ToDMTFDateTime((get-date).AddDays(-1))
get-wmiobject -class win32_ntlogevent -computerName $servername -filter "(EventCode=18453)  and (LogFile='Application') and (TimeGenerated >'$BeginDate')"

Cons:

  • We have to encode the date parameter (instead of using a nice datetime parameter like get-eventlog has)
  • We have to write a WQL where-clause to match the parameters

Pros:

  • We get to use the event code (rather than a string match)
  • The code is orders of magnitude faster (39 servers in 13 minutes as a test case)

I think that you might have a positive experience using get-eventlog if you need to scan a range of time (for instance if you’re reporting on what happened on the server), but if you need to look for a specific event (or set of events) you’re probably going to want to use get-wmiobject.

-Mike

  • Digg
  • Slashdot
  • Reddit
  • Tumblr
  • Delicious
  • Twitter
  • Google Bookmarks
  • StumbleUpon
  • Technorati Favorites
  • Google Reader
  • Share/Bookmark

Writing your own PowerShell Hosting App (the epilog)

As I mentioned before, I have created a CodePlex project to track the development of a WPF PowerShell host using AvalonDock and AvalonEdit.

It’s still in the very beginning stages, but it’s comparable to the code I used in this tutorial series (except that it’s using different technologies, all of which I’m new to).

PowerShellWorkBench will eventually include:

  • Treeview controls
  • Node/Edge Graphs (using the GraphXL library)
  • Context-menus based on powershell ETS
  • Whatever you think of and submit

If you’re interested in contributing to PowerShellWorkBench, drop me a line (mike).

-Mike

[EDIT]: The windows forms-based powershell workbench project can be downloaded here.

  • Digg
  • Slashdot
  • Reddit
  • Tumblr
  • Delicious
  • Twitter
  • Google Bookmarks
  • StumbleUpon
  • Technorati Favorites
  • Google Reader
  • Share/Bookmark