{"id":72771,"date":"2015-10-13T14:40:00","date_gmt":"2015-10-13T14:40:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/10\/13\/use-powershell-to-parse-network-log\/"},"modified":"2019-02-18T09:34:55","modified_gmt":"2019-02-18T16:34:55","slug":"use-powershell-to-parse-network-log","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-parse-network-log\/","title":{"rendered":"Use PowerShell to Parse Network Log"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about using Windows PowerShell to parse a network trace log.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today I want to talk a little bit about using Windows PowerShell to parse a network trace log. In yesterday&rsquo;s blog post, <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/packet-sniffing-with-powershell-getting-started\/\" target=\"_blank\">Packet Sniffing with PowerShell: Getting Started<\/a>, I talked using Windows PowerShell to do a network trace.<\/p>\n<p>Yesterday, I created a network trace log. I can use that log, or I can create a new log.<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>Note&nbsp;<\/b> These commands require that Windows PowerShell is elevated.<\/p>\n<p>When I create a new <b>NetEvent<\/b> session with the <b>New-NetEventSession<\/b> cmdlet, it returns a <b>NetEvent<\/b> session object:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; New-NetEventSession -Name &#8220;Session1&#8221;<\/p>\n<p style=\"margin-left:30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Session1<\/p>\n<p style=\"margin-left:30px\">CaptureMode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : SaveToFile<\/p>\n<p style=\"margin-left:30px\">LocalFilePath&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : C:\\Windows\\system32\\config\\systemprofile\\AppData\\Local\\NetEvent<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Trace.etl<\/p>\n<p style=\"margin-left:30px\">MaxFileSize&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 250 MB<\/p>\n<p style=\"margin-left:30px\">TraceBufferSize&nbsp;&nbsp;&nbsp; : 0 KB<\/p>\n<p style=\"margin-left:30px\">MaxNumberOfBuffers : 0<\/p>\n<p style=\"margin-left:30px\">SessionStatus&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotRunning<\/p>\n<p>This object contains the path to the log file. I like to store the results in a variable so that I can easily access the log file without having to do a lot of typing. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $session = New-NetEventSession -Name &#8220;Session1&#8221;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $session.LocalFilePath<\/p>\n<p style=\"margin-left:30px\">C:\\Windows\\system32\\config\\systemprofile\\AppData\\Local\\NetEventTrace.etl<\/p>\n<p>After I add my event provider and start the session, I can begin the logging, as shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Add-NetEventProvider -Name &#8220;Microsoft-Windows-TCPIP&#8221; -SessionName &#8220;Session1&#8221;<\/p>\n<p style=\"margin-left:30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Microsoft-Windows-TCPIP<\/p>\n<p style=\"margin-left:30px\">SessionName&nbsp;&nbsp;&nbsp;&nbsp; : Session1<\/p>\n<p style=\"margin-left:30px\">Level&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 4<\/p>\n<p style=\"margin-left:30px\">MatchAnyKeyword : 0xFFFFFFFFFFFFFFFF<\/p>\n<p style=\"margin-left:30px\">MatchAllKeyword : 0x0<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Start-NetEventSession -Name &#8220;Session1&#8221;<\/p>\n<p>After doing the trace, I stop the session:<\/p>\n<p style=\"margin-left:30px\">Stop-NetEventSession -Name session1<\/p>\n<h2>Examine the trace log<\/h2>\n<p>Now I use the <b>Get-WinEvent<\/b> cmdlet to examine the trace log. To do this, I like to read the contents into a variable so I can parse it. This is where storing the path to the log comes in handy. Here is the command:<\/p>\n<p style=\"margin-left:30px\">$log = Get-WinEvent -Path $session.LocalFilePath &ndash;Oldest<\/p>\n<p><b>&nbsp; &nbsp;Note<\/b>&nbsp; The trace log must be read in reverse order, so the <b>&ndash;Oldest<\/b> switch is required. Otherwise, an error occurs.<\/p>\n<p>I can inspect the first record by indexing into the collection:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $log[0]<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; ProviderName: Microsoft-Windows-TCPIP<\/p>\n<p style=\"margin-left:30px\">TimeCreated&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Id LevelDisplayName Message<\/p>\n<p style=\"margin-left:30px\">&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; &#8212;&#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;-<\/p>\n<p style=\"margin-left:30px\">10\/12\/2015 3:22:06 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1300 Information&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCP: connection 0xffffe001cc3&#8230;<\/p>\n<p>But it becomes more interesting to look at event IDs or to try to parse the message block. For example, I can look at the message block by accessing the <b>Message<\/b><i> <\/i>property:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $log[0].Message<\/p>\n<p style=\"margin-left:30px\">TCP: connection 0xffffe001cc33cd10 (local=192.168.0.7:52259 remote=127.0.0.1:443) exists. State = CloseWaitState. PID = 2640.<\/p>\n<p>Here, I look at a specific ID:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $log.Where({$_.id -eq 1348})<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; ProviderName: Microsoft-Windows-TCPIP<\/p>\n<p style=\"margin-left:30px\">TimeCreated&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Id LevelDisplayName Message<\/p>\n<p style=\"margin-left:30px\">&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; &#8212;&#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;-<\/p>\n<p style=\"margin-left:30px\">10\/12\/2015 3:23:00 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1348 Information&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCP: CTCP DataTransferTimeout&#8230;<\/p>\n<p style=\"margin-left:30px\">10\/12\/2015 3:23:00 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1348 Information&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCP: CTCP DataTransferTimeout&#8230;<\/p>\n<p style=\"margin-left:30px\">10\/12\/2015 3:23:00 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1348 Information&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCP: CTCP DataTransferTimeout&#8230;<\/p>\n<p style=\"margin-left:30px\">10\/12\/2015 3:23:00 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1348 Information&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCP: CTCP DataTransferTimeout&#8230;<\/p>\n<p style=\"margin-left:30px\">10\/12\/2015 3:23:01 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1348 Information&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCP: CTCP DataTransferTimeout&#8230;<\/p>\n<p style=\"margin-left:30px\">&lt;output truncated&gt;<\/p>\n<p>How many of those events were there? I can find that out by the count:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $log.Where({$_.id -eq 1348}).count<\/p>\n<p style=\"margin-left:30px\">72<\/p>\n<p>As shown here, I can sort by ID and do a count:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $log | group id -NoElement | sort count -Descending<\/p>\n<p style=\"margin-left:30px\">Count Name<\/p>\n<p style=\"margin-left:30px\">&#8212;&#8211; &#8212;-<\/p>\n<p style=\"margin-left:30px\">&nbsp;1188 1074<\/p>\n<p style=\"margin-left:30px\">&nbsp; 649 1332<\/p>\n<p style=\"margin-left:30px\">&nbsp; 628 1157<\/p>\n<p style=\"margin-left:30px\">&nbsp; 364 1156<\/p>\n<p style=\"margin-left:30px\">&nbsp; 359 1158<\/p>\n<p style=\"margin-left:30px\">&nbsp; 196 1159<\/p>\n<p style=\"margin-left:30px\">&nbsp; 189 1229<\/p>\n<p style=\"margin-left:30px\">&nbsp; 189 1331<\/p>\n<p style=\"margin-left:30px\">&nbsp; 137 1051<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; 72 1187<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; 72 1351<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; 72 1079<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; 72 1348<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; 68 1193<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; 52 1086<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; 40 1300<\/p>\n<p style=\"margin-left:30px\">&lt;output truncated&gt;<\/p>\n<p>Well, is an ID 1074 a good thing or a bad thing? I can easily find out by looking at a sample event, and then examining the message string:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $log.Where({$_.id -eq 1074})[0]<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; ProviderName: Microsoft-Windows-TCPIP<\/p>\n<p style=\"margin-left:30px\">TimeCreated&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;nbsp\n;&nbsp;&nbsp;&nbsp;&nbsp; Id LevelDisplayName Message<\/p>\n<p style=\"margin-left:30px\">&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; &#8212;&#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;-<\/p>\n<p style=\"margin-left:30px\">10\/12\/2015 3:22:08 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1074 Information&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCP: connection 0xffffe001d35&#8230;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $log.Where({$_.id -eq 1074})[0].message<\/p>\n<p style=\"margin-left:30px\">TCP: connection 0xffffe001d3537c00: Received data with number of bytes = 186. ThSeq<\/p>\n<p style=\"margin-left:30px\">= 2458887771.<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p>If I am not sure as to what time frame I am working with, I can look at the first and last events in my log:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $log | select -Last 1<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; ProviderName: Microsoft-Windows-TCPIP<\/p>\n<p style=\"margin-left:30px\">TimeCreated&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id LevelDisplayName Message<\/p>\n<p style=\"margin-left:30px\">&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; &#8212;&#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;-<\/p>\n<p style=\"margin-left:30px\">10\/12\/2015 3:24:15 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1193 Information&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCP: endpoint\/connection 0xff&#8230;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $log | select -First 1<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; ProviderName: Microsoft-Windows-TCPIP<\/p>\n<p style=\"margin-left:30px\">TimeCreated&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Id LevelDisplayName Message<\/p>\n<p style=\"margin-left:30px\">&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; &#8212;&#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;-<\/p>\n<p style=\"margin-left:30px\">10\/12\/2015 3:22:06 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1300 Information&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCP: connection 0xffffe001cc3&#8230;<\/p>\n<p>So, it looks like only a couple minutes. To know for sure, I can create a new timespan that represents the amount of log time:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; New-TimeSpan -end ($log | select -Last 1).timecreated -start ($log | select -first 1).Timecreated<\/p>\n<p style=\"margin-left:30px\">Days&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"margin-left:30px\">Hours&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: 0<\/p>\n<p style=\"margin-left:30px\">Minutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2<\/p>\n<p style=\"margin-left:30px\">Seconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 9<\/p>\n<p style=\"margin-left:30px\">Milliseconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 628<\/p>\n<p style=\"margin-left:30px\">Ticks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1296282580<\/p>\n<p style=\"margin-left:30px\">TotalDays&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0.00150032706018519<\/p>\n<p style=\"margin-left:30px\">TotalHours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0.0360078494444444<\/p>\n<p style=\"margin-left:30px\">TotalMinutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2.16047096666667<\/p>\n<p style=\"margin-left:30px\">TotalSeconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 129.628258<\/p>\n<p style=\"margin-left:30px\">TotalMilliseconds : 129628.258<\/p>\n<p>That is all there is to using Windows PowerShell to parse a network log. Join me tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about using Windows PowerShell to parse a network trace log. Microsoft Scripting Guy, Ed Wilson, is here. Today I want to talk a little bit about using Windows PowerShell to parse a network trace log. In yesterday&rsquo;s blog post, Packet Sniffing with PowerShell: Getting Started, I talked using [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[37,609,3,45],"class_list":["post-72771","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-networking","tag-powershell-5","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about using Windows PowerShell to parse a network trace log. Microsoft Scripting Guy, Ed Wilson, is here. Today I want to talk a little bit about using Windows PowerShell to parse a network trace log. In yesterday&rsquo;s blog post, Packet Sniffing with PowerShell: Getting Started, I talked using [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/72771","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=72771"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/72771\/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=72771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=72771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=72771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}