{"id":72801,"date":"2015-10-12T00:01:00","date_gmt":"2015-10-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/10\/12\/packet-sniffing-with-powershell-getting-started\/"},"modified":"2019-02-18T09:34:56","modified_gmt":"2019-02-18T16:34:56","slug":"packet-sniffing-with-powershell-getting-started","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/packet-sniffing-with-powershell-getting-started\/","title":{"rendered":"Packet Sniffing with PowerShell: Getting Started"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about getting started with packet sniffing in Windows PowerShell.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. One of the way cool things that happened with Windows&nbsp;8.1 and Windows Server&nbsp;2012&nbsp;R2 was the ability to do network traces with Windows PowerShell. I have found network tracing extremely useful and helpful in troubleshooting and diagnostics ever since I wrote my book, <a href=\"http:\/\/www.amazon.com\/Network-Monitoring-Analysis-Protocol-Troubleshooting\/dp\/0130264954\" target=\"_blank\">Network Monitoring and Analysis: A Protocol Approach to Troubleshooting<\/a>.<\/p>\n<p>In the past, I have used batch files, automated the NetMon API, and done all kinds of crazy things to try to automate capturing network traces and analyzing the data. Although the <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/dn268520(v=wps.630).aspx\" target=\"_blank\">Network Event Packet Capture cmdlets<\/a> have been around for at least a year, I have not written very much about them. The thing is that even though it is basic Windows PowerShell, it still takes a bit of time to figure out how to get started. This is because there are 27 cmdlets in the NetEventPacketCapture module:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; (gcm -Module NetEventPacketCapture | measure).count<\/p>\n<p style=\"margin-left:30px\">27<\/p>\n<p>Here are the 27 cmdlets (functions):<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; gcm -Module NetEventPacketCapture | select name<\/p>\n<p style=\"margin-left:30px\">Name<\/p>\n<p style=\"margin-left:30px\">&#8212;-<\/p>\n<p style=\"margin-left:30px\">Add-NetEventNetworkAdapter<\/p>\n<p style=\"margin-left:30px\">Add-NetEventPacketCaptureProvider<\/p>\n<p style=\"margin-left:30px\">Add-NetEventProvider<\/p>\n<p style=\"margin-left:30px\">Add-NetEventVmNetworkAdapter<\/p>\n<p style=\"margin-left:30px\">Add-NetEventVmSwitch<\/p>\n<p style=\"margin-left:30px\">Add-NetEventWFPCaptureProvider<\/p>\n<p style=\"margin-left:30px\">Get-NetEventNetworkAdapter<\/p>\n<p style=\"margin-left:30px\">Get-NetEventPacketCaptureProvider<\/p>\n<p style=\"margin-left:30px\">Get-NetEventProvider<\/p>\n<p style=\"margin-left:30px\">Get-NetEventSession<\/p>\n<p style=\"margin-left:30px\">Get-NetEventVmNetworkAdapter<\/p>\n<p style=\"margin-left:30px\">Get-NetEventVmSwitch<\/p>\n<p style=\"margin-left:30px\">Get-NetEventWFPCaptureProvider<\/p>\n<p style=\"margin-left:30px\">New-NetEventSession<\/p>\n<p style=\"margin-left:30px\">Remove-NetEventNetworkAdapter<\/p>\n<p style=\"margin-left:30px\">Remove-NetEventPacketCaptureProvider<\/p>\n<p style=\"margin-left:30px\">Remove-NetEventProvider<\/p>\n<p style=\"margin-left:30px\">Remove-NetEventSession<\/p>\n<p style=\"margin-left:30px\">Remove-NetEventVmNetworkAdapter<\/p>\n<p style=\"margin-left:30px\">Remove-NetEventVmSwitch<\/p>\n<p style=\"margin-left:30px\">Remove-NetEventWFPCaptureProvider<\/p>\n<p style=\"margin-left:30px\">Set-NetEventPacketCaptureProvider<\/p>\n<p style=\"margin-left:30px\">Set-NetEventProvider<\/p>\n<p style=\"margin-left:30px\">Set-NetEventSession<\/p>\n<p style=\"margin-left:30px\">Set-NetEventWFPCaptureProvider<\/p>\n<p style=\"margin-left:30px\">Start-NetEventSession<\/p>\n<p style=\"margin-left:30px\">Stop-NetEventSession<\/p>\n<p>TechNet does a good job at describing the cmdlets, but there is also a pretty good chance that it will be rather cumbersome to figure out how to get started. I mean, how do I do a basic network trace? How is that trace viewed? How do I filter that trace to find useful information? These are the sorts of things that I would need if I were going to do a network trace using Windows PowerShell. So, let&#8217;s get started.<\/p>\n<h2>Using an ETL log<\/h2>\n<p>This makes sense. With a gigabyte Ethernet (or greater), there are lots of packets flying by on the wire. Many of them are encrypted, and I can learn nearly nothing by watching network packets fly past. Well, nearly nothing. I can, of course, tell if my laptop is seeing anything on the wire&mdash;but that is basically the same as looking to see if the light blinks on my network card.<\/p>\n<p>As I have mentioned before, ETL logging is an extremely high performance logging interface that is capable of writing hundreds of events a second&mdash; just the thing if I want to do a network trace. And guess what? Windows PowerShell already has a cmdlet that will read ETL logs&mdash;the <b>Get-WinEvent<\/b> cmdlet. So I don&rsquo;t need anything else to be able to read my traces.<\/p>\n<h2>Six basic steps to perform a network trace<\/h2>\n<p>There are six basic steps required to perform a network trace:<\/p>\n<ol>\n<li>Add a new network event session with <b>New-NetEventSession<\/b>.<\/li>\n<li>Add a network event provider to the session with <b>New-NetEventProvider<\/b>.<\/li>\n<li>Start the session with <b>Start-NetEventSession<\/b>.<\/li>\n<li>Get information about the session with <b>Get-NetEventSession<\/b>.<\/li>\n<li>Stop the network event session with <b>Stop-NetEventSession<\/b>.<\/li>\n<li>Remove the network event session with <b>Remove-NetEventSession<\/b>.<\/li>\n<\/ol>\n<h2>Step-by-step walkthrough<\/h2>\n<p>Now I will go through the six steps that are used to create a new network event tracing session.<\/p>\n<h3>Create a new session<\/h3>\n<p>The first thing I need to do is to create a new network event session. To do this, I use the <b>New-NetEventSession<\/b> cmdlet and specify a name for the session. Here is an example of this command:<\/p>\n<p style=\"margin-left:30px\">New-NetEventSession -Name &#8220;Session1&#8221;<\/p>\n<p>When I run this command, I receive information such as where the log file will be and the size of file:<\/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<h3>Add a provider<\/h3>\n<p>The second thing I need to do is to add a provider to the network event session. To do this, I need to know two things:<\/p>\n<ul>\n<li>The name of the session (in my case, session1)<\/li>\n<li>The name of a provider<\/li>\n<\/ul>\n<p>I can use the <b>Get-EtwTraceProvider<\/b> cmdlet; but unfortunately, it only lists GUIDs and not much more information. So I can use the Logman.exe to query for providers:<\/p>\n<p style=\"margin-left:30px\">logman query providers<\/p>\n<p>This command brings back pages of providers, so I can either scroll through it or use <b>Select-String<\/b> to help me find what I need. That is right. I can pipe the output from the executable directly to <b>Select-String<\/b>. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\Windows\\system32&gt; logman query providers | select-string tcp<\/p>\n<p style=\"margin-left:30px\">Microsoft-Windows-TCPIP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {2F07E2EE-15DB-40F1-90EF-9D7BA282188A}<\/p>\n<p style=\"margin-left:30px\">Microsoft-Windows-Tcpip-SQM-Provider&nbsp;&nbsp;&nbsp;&nbsp; {C8F7689F-3692-4D66-B0C0-9536D21082C9}<\/p>\n<p style=\"margin-left:30px\">TCPIP Service Trace&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {EB004A05-9B1A-11D4-9123-0050047759BC}<\/p>\n<p>I want to use the Microsoft-Windows-TCPIP provider, and my command is 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<h3>Start the session<\/h3>\n<p>Now I need to start the network trace session. I use the <b>Start-NetEventSession<\/b> cmdlet and specify my session name. Note that nothing returns from the following command:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Start-NetEventSession -Name &#8220;Session1&#8221;<\/p>\n<h3>Get the session<\/h3>\n<p>I want to get information about my session. To do this, I use the <b>Get-NetEventSession<\/b> cmdlet:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-NetEventSession<\/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; : 64 KB<\/p>\n<p style=\"margin-left:30px\">MaxNumberOfBuffers : 38<\/p>\n<p style=\"margin-left:30px\">SessionStatus&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Running<\/p>\n<p>The <b>Get-NetEventSession<\/b> cmdlet tells me the location of the log file, so I probably want to store that and avoid a bit of typing. This is shown here:<\/p>\n<p style=\"margin-left:30px\">$s = Get-NetEventSession<\/p>\n<h3>Stop the session<\/h3>\n<p>Now it is time to stop the network trace session. To do this, I use <b>Stop-NetEventSession<\/b> and specify my session number. This command does not return any information:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Stop-NetEventSession -Name session1<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<h3>Remove the session<\/h3>\n<p>The last thing I need to do is to remove the session that I stopped. To do this, I use the <b>Remove-NetEventSession<\/b> cmdlet. It does not return any information either, so I use the <b>Get-NetEventSession<\/b> cmdlet to ensure that it did remove the session:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Remove-NetEventSession<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-NetEventSession<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p>Now you know how to use Windows PowerShell to get started making network traces.&nbsp; Join me tomorrow when I will talk about parsing the captured ETL log data.<\/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 getting started with packet sniffing in Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. One of the way cool things that happened with Windows&nbsp;8.1 and Windows Server&nbsp;2012&nbsp;R2 was the ability to do network traces with Windows PowerShell. I have found network tracing extremely useful and helpful [&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,3,626,45],"class_list":["post-72801","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-networking","tag-scripting-guy","tag-traces","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about getting started with packet sniffing in Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. One of the way cool things that happened with Windows&nbsp;8.1 and Windows Server&nbsp;2012&nbsp;R2 was the ability to do network traces with Windows PowerShell. I have found network tracing extremely useful and helpful [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/72801","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=72801"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/72801\/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=72801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=72801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=72801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}