<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>vmweaver.com &#187; EventLog</title>
	<atom:link href="http://vmweaver.com/index.php/tag/eventlog/feed/" rel="self" type="application/rss+xml" />
	<link>http://vmweaver.com</link>
	<description>Mindless ramblings of a geek...</description>
	<lastBuildDate>Thu, 06 Oct 2011 20:42:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Binary Search and Powershell</title>
		<link>http://vmweaver.com/index.php/2009/07/binary-search-and-powershell/</link>
		<comments>http://vmweaver.com/index.php/2009/07/binary-search-and-powershell/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 03:53:52 +0000</pubDate>
		<dc:creator>Mark A. Weaver</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[EventLog]]></category>

		<guid isPermaLink="false">http://vmweaver.com/?p=142</guid>
		<description><![CDATA[Time for another Powershell Post. So, what is a binary search and why should you care about it? Well, it provides us a mechanism to very quickly search through an ordered list of &#8220;things&#8221; (like event log entries). It is especially useful when looking through LARGE amounts of data. I know, I know.. There are [...]]]></description>
			<content:encoded><![CDATA[<p>Time for another Powershell Post.<br />
So, what is a binary search and why should you care about it?</p>
<p>Well, it provides us a mechanism to very quickly search through an ordered list of &#8220;things&#8221; (like event log entries). It is especially useful when looking through LARGE amounts of data.</p>
<p>I know, I know.. There are all sorts of ways to look through/filter/search event logs and everyone has probably written about this already, but I guess I feel compelled to put in my 2 cents.</p>
<p>We ran into the need to be able to pull event log entries from a specific period of time on several servers.<br />
There aren&#8217;t really any &#8220;native&#8221; Powershell methods to query Eventlogs from REMOTE servers (unless you are on CTP and hitting Vista/2K8 systems).</p>
<p>So a little info about a binary search:</p>
<p>Lets say we have a list of things as follows:</p>
<table border="1" cellspacing="0" cellpadding="4" width="198" bordercolor="#000000">
<colgroup span="1">
<col span="1" width="48"></col>
<col span="1" width="47"></col>
<col span="1" width="77"></col>
</colgroup>
<tbody>
<tr valign="top">
<td width="48" bgcolor="#000000">
<p align="center"><span style="color: #ffffff;"><strong>Index</strong></span></p>
</td>
<td width="47" bgcolor="#000000">
<p align="center"><span style="color: #ffffff;"><strong>Name</strong></span></p>
</td>
<td width="77" bgcolor="#000000">
<p align="center"><span style="color: #ffffff;"><strong>DOB</strong></span></p>
</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">0</p>
</td>
<td width="47">Adam</td>
<td width="77">01/20/80</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">1</p>
</td>
<td width="47">Cheryl</td>
<td width="77">09/16/77</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">2</p>
</td>
<td width="47">Frank</td>
<td width="77">04/01/82</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">3</p>
</td>
<td width="47">Ivan</td>
<td width="77">10/26/70</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">4</p>
</td>
<td width="47">Suzy</td>
<td width="77">04/10/90</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">5</p>
</td>
<td width="47">Tim</td>
<td width="77">11/21/62</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">6</p>
</td>
<td width="47">Wendy</td>
<td width="77">02/02/71</td>
</tr>
</tbody>
</table>
<p>And we are wanting to find which index “Suzy” is in the list.</p>
<p>One way would be to start at the top of the list and see if the Name matches. On a short list this may make sense since it is really only 5 compares before we find her.</p>
<p>But if I was looking for “Suzy” in a list the size of a phone book this can take a long time and a lot of horsepower to find her and may take 300,000 compares or more.</p>
<p>A binary search algorithm helps us by breaking the list down by using something we already know about the list: that it is sorted by name.</p>
<p>With a single compare, though, we can cut the list of viable options in half.</p>
<p>All we need to do is take the index of our smallest value [0] and we add it to the index of our largest element [6] and divide by 2, we get the index of our middle element [3]. Now lets compare what we are looking for (“Suzy”) to the value of element [3].</p>
<p>LowerBound Index = 0<br />
UpperBound Index = 6<br />
Mid Index = (6+0)\2 = 3</p>
<p>Lets take a look at this:</p>
<table border="1" cellspacing="0" cellpadding="4" width="198" bordercolor="#000000">
<colgroup span="1">
<col span="1" width="48"></col>
<col span="1" width="47"></col>
<col span="1" width="77"></col>
</colgroup>
<tbody>
<tr valign="top">
<td width="48" bgcolor="#000000">
<p align="center"><span style="color: #ffffff;"><strong>Index</strong></span></p>
</td>
<td width="47" bgcolor="#000000">
<p align="center"><span style="color: #ffffff;"><strong>Name</strong></span></p>
</td>
<td width="77" bgcolor="#000000">
<p align="center"><span style="color: #ffffff;"><strong>DOB</strong></span></p>
</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">0</p>
</td>
<td width="47">Adam</td>
<td width="77">01/20/80</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">1</p>
</td>
<td width="47">Cheryl</td>
<td width="77">09/16/77</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">2</p>
</td>
<td width="47">Frank</td>
<td width="77">04/01/82</td>
</tr>
<tr valign="top">
<td width="48" bgcolor="#e6e6ff">
<p align="center">3</p>
</td>
<td width="47" bgcolor="#e6e6ff">Ivan</td>
<td width="77" bgcolor="#e6e6ff">10/26/70</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">4</p>
</td>
<td width="47">Suzy</td>
<td width="77">04/10/90</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">5</p>
</td>
<td width="47">Tim</td>
<td width="77">11/21/62</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">6</p>
</td>
<td width="47">Wendy</td>
<td width="77">02/02/71</td>
</tr>
</tbody>
</table>
<p>Obviously “Suzy” is greater than our value at index [3] “Ivan”. Since we know this, we can throw out all indexes that are [3] and lower.</p>
<p>Our list of viable options now becomes this:</p>
<table border="1" cellspacing="0" cellpadding="4" width="198" bordercolor="#000000">
<colgroup span="1">
<col span="1" width="48"></col>
<col span="1" width="47"></col>
<col span="1" width="77"></col>
</colgroup>
<tbody>
<tr valign="top">
<td width="48">
<p align="center">4</p>
</td>
<td width="47">Suzy</td>
<td width="77">04/10/90</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">5</p>
</td>
<td width="47">Tim</td>
<td width="77">11/21/62</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">6</p>
</td>
<td width="47">Wendy</td>
<td width="77">02/02/71</td>
</tr>
</tbody>
</table>
<p style="MARGIN-BOTTOM: 0in">Let&#8217;s repeat this exercise on our new list:</p>
<p style="MARGIN-BOTTOM: 0in">Lowest index = 4</p>
<p style="MARGIN-BOTTOM: 0in">Largest index = 6</p>
<p style="MARGIN-BOTTOM: 0in">Middle index = (6+4) /2 = 5</p>
<p style="MARGIN-BOTTOM: 0in"> </p>
<table border="1" cellspacing="0" cellpadding="4" width="198" bordercolor="#000000">
<colgroup span="1">
<col span="1" width="48"></col>
<col span="1" width="47"></col>
<col span="1" width="77"></col>
</colgroup>
<tbody>
<tr valign="top">
<td width="48">
<p align="center">4</p>
</td>
<td width="47">Suzy</td>
<td width="77">04/10/90</td>
</tr>
<tr valign="top">
<td width="48" bgcolor="#e6e6e6">
<p align="center">5</p>
</td>
<td width="47" bgcolor="#e6e6e6">Tim</td>
<td width="77" bgcolor="#e6e6e6">11/21/62</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">6</p>
</td>
<td width="47">Wendy</td>
<td width="77">02/02/71</td>
</tr>
</tbody>
</table>
<p style="MARGIN-BOTTOM: 0in"> So now we compare what we are looking for “Suzy” to our element [5] (“Tim”). Well, “Suzy is less than “Tim”, so that means we can throw out indexes that are [5] and higher.</p>
<p style="MARGIN-BOTTOM: 0in"> </p>
<table border="1" cellspacing="0" cellpadding="4" width="198" bordercolor="#000000">
<colgroup span="1">
<col span="1" width="48"></col>
<col span="1" width="47"></col>
<col span="1" width="77"></col>
</colgroup>
<tbody>
<tr valign="top">
<td width="48" bgcolor="#e6e6e6">
<p align="center">4</p>
</td>
<td width="47" bgcolor="#e6e6e6">Suzy</td>
<td width="77" bgcolor="#e6e6e6">04/10/90</td>
</tr>
</tbody>
</table>
<p style="MARGIN-BOTTOM: 0in"> Let&#8217;s go one more time&#8230;. oh wait.. .there is only one thing left&#8230;.it&#8217;s “Suzy”. We have found her.</p>
<p style="MARGIN-BOTTOM: 0in"> </p>
<p style="MARGIN-BOTTOM: 0in">There ARE .NET methods (of [System.Array]) that enable us to do binary searches of Arrays for Strings. The only problem is that it will be looking for EXACT matches for the string.</p>
<p style="MARGIN-BOTTOM: 0in"> </p>
<p style="MARGIN-BOTTOM: 0in">This poses a problem for us if we are looking for something that is a little less exact.</p>
<p style="MARGIN-BOTTOM: 0in"> </p>
<p style="MARGIN-BOTTOM: 0in"><span style="font-size: large;"><em>Mark&#8217;s Modified Binary Search</em></span></p>
<p>Okay, leave it to me to mess with a good thing.</p>
<p>My problem is that I want to grab all events from the System event log that occurred between 8pm and 9pm last night.</p>
<p>This is a little more “fuzzy” than exact, so let me show you what I did&#8230;.heh</p>
<p> I know you all are getting a little antsy for some code, but we will get there soon.. Stay with me.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> Get<span style="color: pink;">-</span>DatedLogEntries<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$ServerName</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$EventLogName</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span>datetime<span style="color: #000000;">&#93;</span><span style="color: #800080;">$OldestTime</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span>datetime<span style="color: #000000;">&#93;</span><span style="color: #800080;">$NewestTime</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #008000;">#Grabbing my Eventlog Entries</span>
	<span style="color: #800080;">$EventLog</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Diagnostics.EventLog<span style="color: #000000;">&#40;</span><span style="color: #800080;">$EventlogName</span><span style="color: #000000;">&#41;</span>
	<span style="color: #800080;">$EventLog</span>.MachineName <span style="color: pink;">=</span> <span style="color: #800080;">$ServerName</span>
	<span style="color: #800080;">$Entries</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Eventlog</span>.Entries</pre></div></div>

<p>There are some cmdlets designed to return eventlog entries, but they are only really effective if you are querying the local server or if you are leveraging the “new” Powershell Remoting, but you have to be running against Vista or Server 2008.</p>
<p>Fortunately, there is a way to grab remote event log entries via .NET (as shown in the code above)</p>
<p>When done, $Entries will contain all of the events in the logfile sorted by Time.</p>
<p>Next, we will setup our “bound”ing values for our array of entries and the times we are looking for.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">#Defining my starting boundaries of my array</span>
<span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Entries</span>.count <span style="color: pink;">-</span> <span style="color: #804000;">1</span>
<span style="color: #800080;">$Lbound</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>
<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>
&nbsp;
<span style="color: #008000;">#Setting up my dates</span>
<span style="color: #800080;">$StartTime</span> <span style="color: pink;">=</span> <span style="color: #800080;">$OldestTime</span>
<span style="color: #800080;">$EndTime</span> <span style="color: pink;">=</span> $NewestTime</pre></div></div>

<p>Now we are ready for the real work. Because I want to grab a range of events, I will run through the binary search two times. The first time will tell me what the UpperBound of my list is, and the second will tell me what the LowerBound of my list is as array indexes.</p>
<p>Before we start checking anything, we can make sure our search isn&#8217;t for naught.</p>
<p>Many systems have event logs that can roll pretty quickly, so we can just compare the oldest event to our start time. If the oldest event is newer than my start time, then my logs have rolled and I won&#8217;t have any data.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">-</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-gt</span> <span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #804000;">2</span> <span style="color: #000000;">&#41;</span> <span style="color: #008000;">#Calculate my midpoint</span>
  <span style="color: #008000;">#Compare my midpoint to my StartTime</span>
  <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
  <span style="color: #008000;">#If my midpoint is less than my Start time, then throw out all events</span>
  <span style="color: #008000;">#below and including my Midpoint</span>
        <span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span> <span style="color: pink;">+</span> <span style="color: #804000;">1</span>
     <span style="color: #000000;">&#125;</span>
   <span style="color: #0000FF;">elseif</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
     <span style="color: #008000;">#If my midpoint is greater than my Start time, then throw out all events</span>
      <span style="color: #008000;">#above and including my Midpoint</span>
      <span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span><span style="color: pink;">-</span><span style="color: #804000;">1</span>
     <span style="color: #000000;">&#125;</span>
    <span style="color: #0000FF;">else</span>
     <span style="color: #000000;">&#123;</span>
     <span style="color: #008000;">#If my midpoint is equal to my Start time, then I got lucky and found my time.</span>
     <span style="color: #008000;">#I just realized that I may need to do something else with this. May tackle that</span>
     <span style="color: #008000;"># later though...</span>
      <span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
      <span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
     <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
 <span style="color: #008000;"># Now I know that the array index of our oldest item is here</span>
 <span style="color: #800080;">$Oldest</span> <span style="color: pink;">=</span> $LBound</pre></div></div>

<p>Now we will repeat the process to find out LowerBound. I won&#8217;t document it too much since it is nearly identical code to above. If you have questions, please let me know.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Entries</span>.count <span style="color: pink;">-</span> <span style="color: #804000;">1</span>
<span style="color: #800080;">$Lbound</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>
<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>
&nbsp;
<span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">-</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-gt</span> <span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
   <span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #804000;">2</span> <span style="color: #000000;">&#41;</span>
   <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$EndTime</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
      <span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span> <span style="color: pink;">+</span> <span style="color: #804000;">1</span>
     <span style="color: #000000;">&#125;</span>
    <span style="color: #0000FF;">elseif</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$EndTime</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
      <span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span><span style="color: pink;">-</span><span style="color: #804000;">1</span>
     <span style="color: #000000;">&#125;</span>
    <span style="color: #0000FF;">else</span>
    <span style="color: #000000;">&#123;</span>
     <span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
     <span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
    <span style="color: #000000;">&#125;</span>
 <span style="color: #000000;">&#125;</span>
<span style="color: #800080;">$Newest</span> <span style="color: pink;">=</span> $LBound</pre></div></div>

<p>So now we have the LowerBound index for the range I am looking for.<br />
Now to finish it up&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Sometimes we can end up with endpoints that don't meet our time range</span>
<span style="color: #008000;"># We fix that by going through each side and adjusting them until they</span>
<span style="color: #008000;"># are correct</span>
 <span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Newest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$Endtime</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
   <span style="color: #800080;">$Newest</span><span style="color: pink;">--</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
 <span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Oldest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
   <span style="color: #800080;">$Oldest</span><span style="color: pink;">++</span>
  <span style="color: #000000;">&#125;</span>	
&nbsp;
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Newest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-and</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Oldest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$EndTime</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
    <span style="color: #008000;">#Insert Code here if you want to do something when</span>
    <span style="color: #008000;">#no events found during the period requested..</span>
    <span style="color: #800080;">$EntriesByDate</span> <span style="color: pink;">=</span> <span style="color: #800080;">$NULL</span>
 <span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">else</span>
 <span style="color: #000000;">&#123;</span>
    <span style="color: #008000;">#Create a new array and assign it the $Entries indexes ranging from our 'oldest' to our 'newest'</span>
    <span style="color: #800080;">$EntriesByDate</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Oldest</span>..<span style="color: #800080;">$Newest</span><span style="color: #000000;">&#93;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">else</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;">#Insert Code here if you want to do something when</span>
	<span style="color: #008000;"># Logs have rolled... and none were in the specified range.</span>
	<span style="color: #800080;">$EntriesByDate</span> <span style="color: pink;">=</span> <span style="color: #800080;">$NULL</span>
 <span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">return</span> <span style="color: #800080;">$EntriesByDate</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>I hope I was able to articulate this effectively. As always, let me know if you have questions, comments, or suggestions.<br />
Thanks for Reading and happy shelling.<br />
So, that is basically it. Here is the code in one big block:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">## Get-DatedLogEntries Function</span>
<span style="color: #008000;">## Written by: Mark A. Weaver</span>
<span style="color: #008000;">## Website: www.vmweaver.com</span>
<span style="color: #008000;">## Version: 1.0</span>
<span style="color: #008000;">## Date: 7/23/2009</span>
<span style="color: #008000;">## Purpose: This Function will get event log entries from the specified server using currently logged in</span>
<span style="color: #008000;">##          credentials and return an array of Events that occurred between the 2 times.</span>
<span style="color: #008000;">##          Not much error checking or validation is done, so you please edit to your liking.</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">##        Input:</span>
<span style="color: #008000;">##				-ServerName &quot;ServerName&quot;</span>
<span style="color: #008000;">##				-EventLogName &quot;EventLogName&quot;</span>
<span style="color: #008000;">##          -OldestTime [DateTime]OldestTime</span>
<span style="color: #008000;">##				-NewestTime [Datetime]NewestTime</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">##        Output:</span>
<span style="color: #008000;">##				Array of Event log entries or Null if none found</span>
<span style="color: #008000;">#############################</span>
<span style="color: #008000;">## Updates:</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">######################################################################</span>
<span style="color: #008000;">######################################################################</span>
&nbsp;
<span style="color: #0000FF;">function</span> Get<span style="color: pink;">-</span>DatedLogEntries<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$ServerName</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$EventLogName</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span>datetime<span style="color: #000000;">&#93;</span><span style="color: #800080;">$OldestTime</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span>datetime<span style="color: #000000;">&#93;</span><span style="color: #800080;">$NewestTime</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #008000;">#Grabbing my Eventlog Entries</span>
	<span style="color: #800080;">$EventLog</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Diagnostics.EventLog<span style="color: #000000;">&#40;</span><span style="color: #800080;">$EventlogName</span><span style="color: #000000;">&#41;</span>
	<span style="color: #800080;">$EventLog</span>.MachineName <span style="color: pink;">=</span> <span style="color: #800080;">$ServerName</span>
	<span style="color: #800080;">$Entries</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Eventlog</span>.Entries
&nbsp;
	<span style="color: #008000;">#Defining my starting boundaries of my array</span>
	<span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Entries</span>.count <span style="color: pink;">-</span> <span style="color: #804000;">1</span>
	<span style="color: #800080;">$Lbound</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>
	<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>
&nbsp;
	<span style="color: #008000;">#Setting up my dates</span>
	<span style="color: #800080;">$StartTime</span> <span style="color: pink;">=</span> <span style="color: #800080;">$OldestTime</span>
	<span style="color: #800080;">$EndTime</span> <span style="color: pink;">=</span> <span style="color: #800080;">$NewestTime</span> 
&nbsp;
	<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">-</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-gt</span> <span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #804000;">2</span> <span style="color: #000000;">&#41;</span> <span style="color: #008000;">#Calculate my midpoint</span>
			<span style="color: #008000;">#Compare my midpoint to my StartTime</span>
			<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #008000;">#If my midpoint is less than my Start time, then throw out all events</span>
				<span style="color: #008000;">#below and including my Midpoint</span>
				<span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span> <span style="color: pink;">+</span> <span style="color: #804000;">1</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0000FF;">elseif</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #008000;">#If my midpoint is greater than my Start time, then throw out all events</span>
				<span style="color: #008000;">#above and including my Midpoint</span>
				<span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span><span style="color: pink;">-</span><span style="color: #804000;">1</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0000FF;">else</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #008000;">#If my midpoint is equal to my Start time, then I got lucky and found my time.</span>
				<span style="color: #008000;">#I just realized that I may need to do something else with this. May tackle that</span>
				<span style="color: #008000;"># later though...</span>
				<span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
				<span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #008000;"># Now I know that the array index of our oldest item is here</span>
		<span style="color: #800080;">$Oldest</span> <span style="color: pink;">=</span> <span style="color: #800080;">$LBound</span> 
&nbsp;
		<span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Entries</span>.count <span style="color: pink;">-</span> <span style="color: #804000;">1</span>
		<span style="color: #800080;">$Lbound</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>
		<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>
&nbsp;
		<span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">-</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-gt</span> <span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #804000;">2</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$EndTime</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span> <span style="color: pink;">+</span> <span style="color: #804000;">1</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0000FF;">elseif</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$EndTime</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span><span style="color: pink;">-</span><span style="color: #804000;">1</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0000FF;">else</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
				<span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #800080;">$Newest</span> <span style="color: pink;">=</span> <span style="color: #800080;">$LBound</span>		
&nbsp;
		<span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Newest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$Endtime</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$Newest</span><span style="color: pink;">--</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Oldest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$Oldest</span><span style="color: pink;">++</span>
		<span style="color: #000000;">&#125;</span>	
&nbsp;
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Newest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-and</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Oldest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$EndTime</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008000;">#No events found during the period requested..</span>
			<span style="color: #800080;">$EntriesByDate</span> <span style="color: pink;">=</span> <span style="color: #800080;">$NULL</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">else</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008000;">#Create a new array and assign it the $Entries indexes ranging from our 'oldest' to our 'newest'</span>
			<span style="color: #800080;">$EntriesByDate</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Oldest</span>..<span style="color: #800080;">$Newest</span><span style="color: #000000;">&#93;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">else</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008000;"># Logs have rolled... and none were in the specified range.</span>
		<span style="color: #800080;">$EntriesByDate</span> <span style="color: pink;">=</span> <span style="color: #800080;">$NULL</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">return</span> <span style="color: #800080;">$EntriesByDate</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/07/binary-search-and-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

