{"id":15801,"date":"2011-01-25T00:01:00","date_gmt":"2011-01-25T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/01\/25\/use-powershell-to-parse-saved-event-logs-for-errors\/"},"modified":"2011-01-25T00:01:00","modified_gmt":"2011-01-25T00:01:00","slug":"use-powershell-to-parse-saved-event-logs-for-errors","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-parse-saved-event-logs-for-errors\/","title":{"rendered":"Use PowerShell to Parse Saved Event Logs for Errors"},"content":{"rendered":"<p>&#160;<\/p>\n<p><b>Summary<\/b>: Simplify Windows auditing and monitoring by using Windows PowerShell to parse archived event logs for errors.<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/p>\n<p> Hey, Scripting Guy! I have been using a scheduled job and a Windows PowerShell script to archive our event logs to .evt files. When I need to check something, I need to import the .evtx file in to Event Viewer so that I can search the file. This is a bit cumbersome, and I would like to find a better way to do this. What I really wish is that I could query the .evtx file in the same way that I query a live event log. I think I can use <b>LogParser<\/b> to query the .evtx file, but I do not know what is up with that. It is more than six years old, and I hate to be dependent on something that is not in the operating system.<\/p>\n<p>&#8212; AH<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/> Hello AH, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. I have written Hey, Scripting Guy! Blog posts and a <i>TechNet Magazine<\/i> article about backing up event logs. However, until now, I have not written about parsing those event log files. By using the <b>Get-WinEvent<\/b> cmdlet, it is as easy to parse an archived event log file as it is to parse an online log. <\/p>\n<p>To view the contents of an archived event log (it can be a .etl, .evt, or .evtx file), use the <b>path<\/b> parameter to point to the archived file. This is illustrated here:<\/p>\n<blockquote>\n<p>PS C:\\&gt; Get-WinEvent -Path C:\\fso\\SavedAppLog.evtx<\/p>\n<p>TimeCreated&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ProviderName&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Id&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Message<\/p>\n<p>&#8212;&#8212;&#8212;&#8211;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;&#8212;&#8212;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;-<\/p>\n<p>1\/15\/2011 9:09:11 AM&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 26&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Connection to Microso&#8230;<\/p>\n<p>1\/15\/2011 9:08:54 AM&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 26&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Connection to Microso&#8230;<\/p>\n<p>1\/15\/2011 9:05:46 AM&#160;&#160;&#160;&#160;&#160;&#160; Office Software Prote&#8230;&#160;&#160;&#160; 1003&#160;&#160;&#160;&#160;&#160;&#160; The Software Protecti&#8230;<\/p>\n<p>1\/15\/2011 9:04:51 AM&#160;&#160;&#160;&#160;&#160;&#160; Office Software Prote&#8230;&#160;&#160;&#160; 1003&#160;&#160;&#160;&#160;&#160;&#160;&#160; The Software Protecti&#8230;<\/p>\n<p>1\/15\/2011 8:37:59 AM&#160;&#160;&#160;&#160;&#160;&#160; SceCli&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 1704&#160;&#160;&#160;&#160;&#160;&#160; Security policy in th&#8230;<\/p>\n<p>&lt;Truncated Output&gt;<\/p>\n<\/blockquote>\n<p>If I need to filter out the content of the saved file, I will need to use the <b>FilterHashTable<\/b> parameter. The <b>FilterHashTable<\/b> parameter was <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/01\/24\/use-powershell-cmdlet-to-filter-event-log-for-easy-parsing.aspx\">discussed yesterday<\/a>. <\/p>\n<p>This can be a bit confusing. The use of the <b>Path<\/b> and <b>FilterHashTable<\/b> are exclusive. This is shown in the two command sets:<\/p>\n<blockquote>\n<p>Get-WinEvent [-Path] &lt;string[]&gt; [-ComputerName &lt;string&gt;] [-Credential &lt;PSCredential&gt;] [-FilterX<\/p>\n<p>Path &lt;string&gt;] [-Force &lt;switch&gt;] [-MaxEvents &lt;int64&gt;] [-Oldest] [&lt;CommonParameters&gt;]<\/p>\n<p>Get-WinEvent -FilterHashTable &lt;Hashtable[]&gt; [-ComputerName &lt;string&gt;] [-Credential &lt;PSCredential<\/p>\n<p>&gt;] [-Force &lt;switch&gt;] [-MaxEvents &lt;int64&gt;] [-Oldest] [&lt;CommonParameters&gt;]<\/p>\n<\/blockquote>\n<p>When I use the <b>FilterHashTable<\/b> parameter, I need to specify the provider, log, and other information via the hash table itself. To retrieve only Outlook event log entries from my saved application log, I specify the path to the log and the <b>ProviderName<\/b> key values in the hash table. Because the saved log is an application event log, I do not need to specify a value for the <b>LogName<\/b> key. The path to the saved log is the location (including the file name) of the stored log. The <b>ProviderName<\/b> key is the source of the events. The following command lists all events from the Outlook provider on my computer. <\/p>\n<blockquote>\n<p>PS C:\\&gt; Get-WinEvent -FilterHashtable @{Path=&quot;C:\\fso\\SavedAppLog.evtx&quot;;ProviderName=&quot;outlook&quot;}<\/p>\n<p>TimeCreated&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ProviderName&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Id&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Message<\/p>\n<p>&#8212;&#8212;&#8212;&#8211;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;&#8212;&#8212;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;<\/p>\n<p>1\/15\/2011 9:09:11 AM&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 26&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Connection to Microso&#8230;<\/p>\n<p>1\/15\/2011 9:08:54 AM&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 26&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Connection to Microso&#8230;<\/p>\n<p>1\/15\/2011 8:28:00 AM&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 45&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Outlook loaded the fo&#8230;<\/p>\n<p>1\/14\/2011 11:28:43 PM&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 45&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Outlook loaded the fo&#8230;<\/p>\n<p>1\/14\/2011 10:49:24 PM&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&amp;#160\n;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 54&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; An appointment has be&#8230;<\/p>\n<p>&lt;Output Truncated&gt;<\/p>\n<\/blockquote>\n<p>In the event log entry shown in the following image, it quickly becomes obvious that the key values required for the <b>FilterHashTable<\/b><i> <\/i>parameter and the values that show up in the graphical user interface do not match up. <\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/8015.HSG-1-25-11-1_2D2BAAED.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" title=\"Image of event log entry\" border=\"0\" alt=\"Image of event log entry\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/2555.HSG-1-25-11-1_thumb_146C5DDD.jpg\" width=\"604\" height=\"422\" \/><\/a><\/p>\n<p>When working with the <b>Get-WinEvent<\/b> Windows PowerShell cmdlet, I often find it helpful to consult the XML view of the event details. This view appears to have a bit more relevance. For example, I can tell that the <b>Level: Information<\/b> of the event log entry (seen in the figure above) is the same as the level 4 that is displayed in the XML view shown in the following image.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/4237.HSG-1-25-11-2_208DD804.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" title=\"Image of XML view of event details\" border=\"0\" alt=\"Image of XML view of event details\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/3225.HSG-1-25-11-2_thumb_17BDCFB8.jpg\" width=\"604\" height=\"422\" \/><\/a><\/p>\n<p>By consulting and matching the information, I compiled the table that follows to aid me in using the <b>Get-WinEvent<\/b> Windows PowerShell cmdlet <b>FilterHashTable<\/b> key values.    <\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p><strong>Event log viewer name<\/strong><\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p><strong>FilterHashTable parameter key name<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p>Log Name<\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p>LogName<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p>Source<\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p>ProviderName<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p>Event ID<\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p>ID<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p>Level<\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p>Level<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p>User<\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p>UserID<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p>Op Code<\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p>*<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p>Logged<\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p>*<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p>Task Category<\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p>*<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p>Keywords<\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p>*<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p>Computer<\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p>N\/A use \u2013<i>ComputerName<\/i> parameter<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p>Details<\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p>Data<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If I want to search for data that shows up in the Details portion of the Event properties, I must include the entire string, typed exactly as it appears. This is because the Data portion of the <b>FilterHashTable<\/b> does not accept a wildcard character. Therefore, the command shown here will retrieve all of the Outlook events that have the message that states that \u201cConnection to Microsoft Exchange has been lost. Outlook will restore the connection when possible.\u201d<\/p>\n<blockquote>\n<p>PS C:\\&gt; Get-WinEvent -FilterHashtable @{Path=&quot;C:\\fso\\SavedAppLog.evtx&quot;;ProviderName=&quot;outlook&quot;;Data=&quot;<\/p>\n<p>Connection to Microsoft Exchange has been lost. Outlook will restore the connection when possible.&quot;}<\/p>\n<p>TimeCreated&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ProviderName&#160;&#160;&#160;&#160; Id&#160;&#160;&#160;&#160;&#160;&#160;&#160; Message<\/p>\n<p>&#8212;&#8212;&#8212;&#8211;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;&#8212;&#8212;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;-<\/p>\n<p>1\/15\/2011 9:08:54 AM&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 26&#160;&#160;&#160;&#160;&#160; Connection to Microso&#8230;<\/p>\n<p>1\/7\/2011 3:46:52 PM&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 26&#160;&#160;&#160;&#160;&#160; Connection to Microso&#8230;<\/p>\n<p>12\/21\/2010 11:36:31 AM&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 26&#160;&#160;&#160;&#160;&#160; Connection to Microso&#8230;<\/p>\n<p>I can search for only event ID 26, as shown here, but the problem is that event ID 26 includes not only the connection lost but also the connection restored message detail. <\/p>\n<p>PS C:\\&gt; Get-WinEvent -FilterHashtable @{Path=&quot;C:\\fso\\SavedAppLog.evtx&quot;;ProviderName=&quot;outlook&quot;;Id=26}<\/p>\n<p>TimeCreated&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ProviderName&#160;&#160;&#160;&#160; Id&#160;&#160;&#160;&#160;&#160;&#160; Mess\nage<\/p>\n<p>&#8212;&#8212;&#8212;&#8211; -&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;&#8212;&#8211;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8211;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;-<\/p>\n<p>1\/15\/2011 9:09:11 AM&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 26&#160;&#160;&#160;&#160;&#160; Connection to Microso&#8230;<\/p>\n<p>1\/15\/2011 9:08:54 AM&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 26&#160;&#160;&#160;&#160;&#160; Connection to Microso&#8230;<\/p>\n<p>1\/7\/2011 3:48:13 PM&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 26&#160;&#160;&#160;&#160;&#160; Connection to Microso&#8230;<\/p>\n<p>1\/7\/2011 3:46:52 PM&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 26&#160;&#160;&#160;&#160;&#160; Connection to Microso&#8230;<\/p>\n<p>12\/21\/2010 11:36:39 AM&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 26&#160;&#160;&#160;&#160;&#160; Connection to Microso&#8230;<\/p>\n<p>12\/21\/2010 11:36:31 AM&#160;&#160;&#160;&#160;&#160;&#160; Outlook&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 26&#160;&#160;&#160;&#160;&#160; Connection to Microso&#8230;<\/p>\n<p>&lt;Output Truncated&gt;<\/p>\n<\/blockquote>\n<p>This particular view into the event log is not too bad, because I can easily tell that my connection to the Microsoft Exchange Server lost connection on 1\/7\/2011 for a minute and 21 seconds (by subtracting the amount of time between connection lost and connection restored).<\/p>\n<p>If I want to determine the total number of disconnections during the course of my event log, I can pipe the results to the <b>Measure-Object<\/b> cmdlet and divide the number by two (to account for connection lost and restored\u2014not precise, but better than typing out the entire text of the detail message).<\/p>\n<blockquote>\n<p>PS C:\\&gt; $count = Get-WinEvent -FilterHashtable @{Path=&quot;C:\\fso\\SavedAppLog.evtx&quot;;ProviderName=&quot;outloo<\/p>\n<p>k&quot;;Id=26} | Measure-object<\/p>\n<p>PS C:\\&gt; $count.Count \/ 2<\/p>\n<p>131.5<\/p>\n<p>PS C:\\&gt;<\/p>\n<\/blockquote>\n<p>From looking at the above data, I can also surmise that one day, I lost connection and it was never restored. If I really cared, I could parse the data further to discover when that date occurred. <\/p>\n<p>In the ParseSavedEventLogsForErrors.ps1 script, I use the <b>Get-ChildItem<\/b> cmdlet to retrieve all the saved event logs from a central location. I then use the <b>Get-WinEvent<\/b> Windows PowerShell cmdlet to examine each saved log to look for errors in the log that occur between January 14, 2011, and January 15, 2011. This is an example of the type of script one might use to quickly peruse archived daily event logs. <\/p>\n<blockquote>\n<p><strong>ParseSavedEventLogsForErrors.ps1<\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p>Get-ChildItem -include *.evt,*.evtx -Path c:\\fso -recurse |<\/p>\n<p>ForEach-Object {<\/p>\n<p>&quot;Parsing $($_.fullname)`r`n&quot;<\/p>\n<p>Try <\/p>\n<p>{ Get-WinEvent -FilterHashtable @{<\/p>\n<p>Path=$_.fullname<\/p>\n<p>Level=2;<\/p>\n<p>StartTime=&quot;1\/14\/2011&quot; ;<\/p>\n<p>EndTime=&quot;1\/15\/2011&quot;} -EA Stop}<\/p>\n<p>Catch [System.Exception] {&quot;No errors in current log&quot;}<\/p>\n<p>}<\/p>\n<\/blockquote>\n<p>As the script currently stands, the script does not accept <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/getting+started\/retrieving+input\/\">input parameters<\/a> for the start time and end time or for the path to the stored logs, but that is an easy change to make. The <a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-march-11-2010\/\">Try\/Catch block<\/a> is required because an error is generated when an event log does not return any matching records. It is a non-terminating error, however, so I needed to specify the <b>ErrorAction<\/b> (<b>EA<\/b> is the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/01\/15\/weekend-scripter-discovering-powershell-cmdlet-parameter-aliases.aspx\">parameter alias<\/a>) of <b>stop<\/b> to cause the script to move into the catch block. <\/p>\n<p>When the script runs, the output that is shown in the following image appears on my computer.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/0083.HSG-1-25-11-3_43FA569C.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" title=\"Image of script output\" border=\"0\" alt=\"Image of script output\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/metablogapi\/8831.HSG-1-25-11-3_thumb_21561821.jpg\" width=\"604\" height=\"509\" \/><\/a><\/p>\n<p>AH, that is all there is to using the Windows PowerShell <b>Get-WinEvent<\/b> cmdlet to query offline event logs. Neglected Windows PowerShell Cmdlet Week will continue tomorrow. <\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post them on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&#160;<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#160; Summary: Simplify Windows auditing and monitoring by using Windows PowerShell to parse archived event logs for errors. Hey, Scripting Guy! I have been using a scheduled job and a Windows PowerShell script to archive our event logs to .evt files. When I need to check something, I need to import the .evtx file in [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,45],"class_list":["post-15801","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&#160; Summary: Simplify Windows auditing and monitoring by using Windows PowerShell to parse archived event logs for errors. Hey, Scripting Guy! I have been using a scheduled job and a Windows PowerShell script to archive our event logs to .evt files. When I need to check something, I need to import the .evtx file in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=15801"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15801\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=15801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}