{"id":3240,"date":"2013-07-14T00:01:00","date_gmt":"2013-07-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/07\/14\/weekend-scripter-use-powershell-to-get-boot-up-trace-events\/"},"modified":"2013-07-14T00:01:00","modified_gmt":"2013-07-14T00:01:00","slug":"weekend-scripter-use-powershell-to-get-boot-up-trace-events","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-get-boot-up-trace-events\/","title":{"rendered":"Weekend Scripter: Use PowerShell to Get Boot-Up Trace Events"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to get boot-up trace events.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping the last of my English Breakfast tea with a little lemon grass and a crushed cinnamon stick. Not sure how I ran out of English Breakfast tea, but I did. Oh well. I still have Darjeeling tea&mdash;I just normally do not drink it first thing in the morning. I guess it is time to place an order to my favorite tea broker.<\/p>\n<p>Anyway&hellip;<\/p>\n<p>Anyone who knows anything about Windows knows that the three traditional event logs (System, Application, and Security) are not the be-all and end-all of logging and event gathering. For over six years, we have also had <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms751538.aspx\" target=\"_blank\">Event Tracing for Windows (ETW)<\/a>. Luckily, Windows PowerShell&nbsp;2.0 introduced the <strong>Get-WinEvent<\/strong> cmdlet, which permits access to these trace logs.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;If you actually have a boot performance issue, you may want to read Jeff Stokes&#8217; blog post <a href=\"http:\/\/blogs.technet.com\/b\/jeff_stokes\/archive\/2012\/09\/17\/how-to-collect-a-good-boot-trace-on-windows-7.aspx\" target=\"_blank\">How to collect a good boot trace on Windows 7<\/a>.<\/p>\n<h2>Picking up boot up trace events<\/h2>\n<p>One thing to keep in mind, is that some of the trace event logs require Admin rights. Windows PowerShell does not bypass security, rather it honors it. Therefore, if I need to access trace logs that require Admin rights, the easiest way to do this is to launch Windows PowerShell with Admin rights. If I do not, I see errors, such as these:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3022.hsg-7-14-13-01.png\"><img decoding=\"async\" title=\"Image of error messages\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3022.hsg-7-14-13-01.png\" alt=\"Image of error messages\" \/><\/a><\/p>\n<p>I right-click the Windows PowerShell ISE icon, and select <strong>Run as administrator<\/strong>:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1321.hsg-7-14-13-02.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1321.hsg-7-14-13-02.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>Now that I have the Windows PowerShell ISE running with Admin rights, it is time to write the script.<\/p>\n<p>I first obtain the boot-up time. This code is shown here:<\/p>\n<p style=\"padding-left: 30px\">$bootTime = (Get-CimInstance win32_Operatingsystem).lastbootuptime<\/p>\n<p style=\"padding-left: 30px\">&#8220;Boot time is $($bootTime)&#8221;<\/p>\n<p>Now I use the <strong>Get-WinEvent<\/strong> cmdlet to obtain a list of all of the event trace logs. I then use the <strong>Foreach<\/strong> command to walk through the collection as shown here:<\/p>\n<p style=\"padding-left: 30px\">Foreach($log in Get-WinEvent -ListLog *)<\/p>\n<p>In my script block, I first display the name of each log:<\/p>\n<p style=\"padding-left: 30px\">&#8220;Events from $($log.Logname) event log&#8221;<\/p>\n<p>Now I use the <strong>Get-WinEvent<\/strong> cmdlet to get all the log entries. I use the <strong>&ndash;ea 0<\/strong> parameter because an error arises if the event log does not have entries. The command is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-WinEvent -LogName $log.Logname -ea 0&nbsp;<\/p>\n<p>In the following script, I pipe the entries to a <strong>Where-Object<\/strong> command and filter out five minutes after boot time:<\/p>\n<p style=\"padding-left: 30px\">where {$_.timecreated -gt $bootTime -and $_.timecreated -lt $bootTime.AddMinutes(5)}<\/p>\n<p>Here is the completed script:<\/p>\n<p style=\"padding-left: 30px\">$bootTime = (Get-CimInstance win32_Operatingsystem).lastbootuptime<\/p>\n<p style=\"padding-left: 30px\">&#8220;Boot time is $($bootTime)&#8221;<\/p>\n<p style=\"padding-left: 30px\">Foreach($log in Get-WinEvent -ListLog *)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;{<\/p>\n<p style=\"padding-left: 30px\">&nbsp; &#8220;Events from $($log.Logname) event log&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp; Get-WinEvent -LogName $log.Logname -ea 0 |<\/p>\n<p style=\"padding-left: 30px\">&nbsp; where {$_.timecreated -gt $bootTime -and $_.timecreated -lt $bootTime.AddMinutes(5)}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; }<\/p>\n<p>The script is a bit slow in running, and I could certainly improve the efficiency by moving the time filter to the left of the pipeline (inside the <strong>Get-WinEvent<\/strong> cmdlet). As it is written, it gets every entry from every log, and then it filters it. This is inefficient. What keeps it from being a real performance dog is the pipeline.<\/p>\n<p>If this was a script that I wanted to run more than once, I would most certainly &ldquo;fix&rdquo; it and improve the performance. But for a quick script that took less than five minutes to write, it is fine. The results from running the script are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4571.hsg-7-14-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4571.hsg-7-14-13-03.png\" alt=\"Image of command output\" \/><\/a><\/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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to get boot-up trace events. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping the last of my English Breakfast tea with a little lemon grass and a crushed cinnamon stick. Not sure how I ran out of English Breakfast tea, [&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":[97,98,3,61,45],"class_list":["post-3240","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-event-logs","tag-logs-and-monitoring","tag-scripting-guy","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to get boot-up trace events. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping the last of my English Breakfast tea with a little lemon grass and a crushed cinnamon stick. Not sure how I ran out of English Breakfast tea, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3240","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=3240"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3240\/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=3240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}