{"id":134,"date":"2016-03-29T02:00:10","date_gmt":"2016-03-29T02:00:10","guid":{"rendered":"http:\/\/eventlogxp.com\/blog\/?p=134"},"modified":"2016-03-30T02:09:07","modified_gmt":"2016-03-30T02:09:07","slug":"exporting-event-logs-with-windows-powershell","status":"publish","type":"post","link":"https:\/\/eventlogxp.com\/blog\/exporting-event-logs-with-windows-powershell\/","title":{"rendered":"Exporting event logs with Windows PowerShell"},"content":{"rendered":"<p><em>Do you need to automate error reporting based on recent events and don&#8217;t want to use third-party tools? This article describes how to collect events from different sources and unite them in one document using standard Windows instruments only.<\/em><\/p>\n<p>Recently I described <a href=\"http:\/\/eventlogxp.com\/blog\/case-study-generating-regular-reports-about-the-problems-in-your-windows-network\/\">how to export events into Excel format using our Event Log Explorer software<\/a>. However, in some cases, using third-party software can be impossible. This may happen if your company doesn&#8217;t have budget to purchase event log utilities, or such utilities are restricted by the company&#8217;s rules. In any case, the task of regular exporting the recent events from different machines into one legible file is still crucial. That&#8217;s why I will show how you can get the events from different Windows machines and export them into one file for further investigation.<\/p>\n<h3>Task<\/h3>\n<p>Let&#8217;s take the same task we solved previously. We have 4 Windows servers and we need to generate weekly reports of Error and Warning events in Application and System event logs. We should utilize only standard Windows instruments.<\/p>\n<h3>Instruments<\/h3>\n<p>Microsoft features Windows PowerShell as a framework to automate different administrative tasks and perform configuration management in Windows. My scripts require at least PowerShell version 3.0. If your PowerShell is outdated, you can update it by downloading Windows Management Framework from Microsoft&#8217;s site. To check PowerShell version simply type in PowerShell console:<\/p>\n<blockquote><p>$PSVersionTable.PSVersion<\/p><\/blockquote>\n<p><a href=\"http:\/\/eventlogxp.com\/blog\/wp-content\/uploads\/2016\/03\/powershell-getversion.png\" rel=\"attachment wp-att-135\" data-rel=\"lightbox-gallery-4x7JCRkb\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-135\" src=\"http:\/\/eventlogxp.com\/blog\/wp-content\/uploads\/2016\/03\/powershell-getversion-300x43.png\" alt=\"Getting Powershell version\" width=\"300\" height=\"43\" srcset=\"https:\/\/eventlogxp.com\/blog\/wp-content\/uploads\/2016\/03\/powershell-getversion-300x43.png 300w, https:\/\/eventlogxp.com\/blog\/wp-content\/uploads\/2016\/03\/powershell-getversion.png 487w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>In my case, PowerShell version = 3 which is OK.<\/p>\n<h3>Research<\/h3>\n<p>To access event logs, Windows PowerShell comes with Get-EventLog cmdlet:<\/p>\n<pre>Parameter Set: LogName\r\nGet-EventLog [-LogName] &lt;String&gt; [[-InstanceId] &lt;Int64[]&gt; ] \r\n[-After &lt;DateTime&gt; ] [-AsBaseObject] [-Before &lt;DateTime&gt; ] \r\n[-ComputerName &lt;String[]&gt; ] [-EntryType &lt;String[]&gt; ] \r\n[-Index &lt;Int32[]&gt; ] [-Message &lt;String&gt; ] [-Newest &lt;Int32&gt; ] \r\n[-Source &lt;String[]&gt; ] [-UserName &lt;String[]&gt; ] [&lt;CommonParameters&gt;]\r\n<\/pre>\n<p>First we need to define the start date (the date after which we will get events). This date is calculated as today minus 7 days:<\/p>\n<blockquote><p>$now=get-date<br \/>\n$startdate=$now.adddays(-7)<\/p><\/blockquote>\n<p>Now we can read warning and error events from a log for the last week:<\/p>\n<blockquote><p>$el = get-eventlog -ComputerName Serv1 -log System -After $startdate -EntryType Error, Warning<\/p><\/blockquote>\n<p>Let&#8217;s check the result. Just type $el in the console. Yes, we can see events from the event log.<br \/>\nBut how will we export the event log? Windows PowerShell doesn&#8217;t have cmdlets to export to Excel. But it supports export to CSV file. Let&#8217;s try it now:<\/p>\n<blockquote><p>$el | export-csv eventlog.csv<\/p><\/blockquote>\n<p>Yes, it works, but multi-line descriptions ruined the output file.<br \/>\nMaybe export to XML will help?<\/p>\n<blockquote><p>$el | export-clixml eventlog.xml<\/p><\/blockquote>\n<p>But how to display it in clear way? Excel understands XML files, but I have no idea how to interpret it:<\/p>\n<p><a href=\"http:\/\/eventlogxp.com\/blog\/wp-content\/uploads\/2016\/03\/powershell-eventlog-export-xml.jpg\" rel=\"attachment wp-att-138\" data-rel=\"lightbox-gallery-4x7JCRkb\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-138\" src=\"http:\/\/eventlogxp.com\/blog\/wp-content\/uploads\/2016\/03\/powershell-eventlog-export-xml-300x87.jpg\" alt=\"PowerShell Log to XML\" width=\"300\" height=\"87\" srcset=\"https:\/\/eventlogxp.com\/blog\/wp-content\/uploads\/2016\/03\/powershell-eventlog-export-xml-300x87.jpg 300w, https:\/\/eventlogxp.com\/blog\/wp-content\/uploads\/2016\/03\/powershell-eventlog-export-xml-768x222.jpg 768w, https:\/\/eventlogxp.com\/blog\/wp-content\/uploads\/2016\/03\/powershell-eventlog-export-xml-1024x296.jpg 1024w, https:\/\/eventlogxp.com\/blog\/wp-content\/uploads\/2016\/03\/powershell-eventlog-export-xml-660x191.jpg 660w, https:\/\/eventlogxp.com\/blog\/wp-content\/uploads\/2016\/03\/powershell-eventlog-export-xml.jpg 1171w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>I guess we can make an XML transformation to convert this XML into more readable file, but I&#8217;m not an XML guru, but I have a more or less useful solution. We can solve our problem if we just export to CSV only several event properties (without event description):<\/p>\n<blockquote><p>$el |Select EntryType, TimeGenerated, Source, EventID | Export-CSV eventlog.csv -NoTypeInfo<\/p><\/blockquote>\n<p>Now we can read eventlog.csv in Excel without problems.<\/p>\n<h3>Putting all together<\/h3>\n<p>It&#8217;s time to write the PowerShell script.<br \/>\nBrief: we will read recent (7 days) error and warning events from Application and System event logs, join them, sort them by time and export to CSV format.<\/p>\n<pre><code>#\r\n#  This script exports consolidated and filtered event logs to CSV\r\n#  Author: Michael Karsyan, FSPro Labs, eventlogxp.com (c) 2016\r\n#\r\n\r\nSet-Variable -Name EventAgeDays -Value 7     #we will take events for the latest 7 days\r\nSet-Variable -Name CompArr -Value @(\"SERV1\", \"SERV2\", \"SERV3\", \"SERV4\")   # replace it with your server names\r\nSet-Variable -Name LogNames -Value @(\"Application\", \"System\")  # Checking app and system logs\r\nSet-Variable -Name EventTypes -Value @(\"Error\", \"Warning\")  # Loading only Errors and Warnings\r\nSet-Variable -Name ExportFolder -Value \"C:\\TEST\\\"\r\n\r\n\r\n$el_c = @()   #consolidated error log\r\n$now=get-date\r\n$startdate=$now.adddays(-$EventAgeDays)\r\n$ExportFile=$ExportFolder + \"el\" + $now.ToString(\"yyyy-MM-dd---hh-mm-ss\") + \".csv\"  # we cannot use standard delimiteds like \":\"\r\n\r\nforeach($comp in $CompArr)\r\n{\r\n  foreach($log in $LogNames)\r\n  {\r\n    Write-Host Processing $comp\\$log\r\n    $el = get-eventlog -ComputerName $comp -log $log -After $startdate -EntryType $EventTypes\r\n    $el_c += $el  #consolidating\r\n  }\r\n}\r\n$el_sorted = $el_c | Sort-Object TimeGenerated    #sort by time\r\nWrite-Host Exporting to $ExportFile\r\n$el_sorted|Select EntryType, TimeGenerated, Source, EventID, MachineName | Export-CSV $ExportFile -NoTypeInfo  #EXPORT\r\nWrite-Host Done!\r\n<\/code><\/pre>\n<h3>Scheduling the task<\/h3>\n<p>To run the script, we should run this command:<\/p>\n<blockquote><p>PowerShell.exe -ExecutionPolicy ByPass -File export-logs.ps1<\/p><\/blockquote>\n<p>We can open Windows scheduler GUI to make this task, or use PowerShell console:<br \/>\nMicrosoft recommends this way to schedule a PowerShell script:<\/p>\n<blockquote><p>$Trigger=New-JobTrigger -Weekly -At &#8220;7:00AM&#8221; -DaysOfWeek &#8220;Monday&#8221;<br \/>\nRegister-ScheduledJob -Name &#8220;Export Logs&#8221; -FilePath &#8220;C:\\Test\\export-logs.ps1&#8221; -Trigger $Trigger<\/p><\/blockquote>\n<p>But this may miswork, because it adds to Windows Task Scheduler the following action:<\/p>\n<blockquote><p>powershell.exe -NoLogo -NonInteractive -WindowStyle Hidden -Command &#8220;Import-Module PSScheduledJob; $jobDef = [Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition]::LoadFromStore(&#8216;Export Logs&#8217;, &#8216;C:\\Users\\Michael\\AppData\\Local\\Microsoft\\Windows\\PowerShell\\ScheduledJobs&#8217;); $jobDef.Run()&#8221;<\/p><\/blockquote>\n<p>If your policy prevents running PoweShell scripts, our export script won&#8217;t run because powershell parameters miss -ExecutionPolicy option.<br \/>\nThat&#8217;s why I will use ScriptBlock instead of FilePath. This code does the trick:<\/p>\n<pre><code>$trigger=New-JobTrigger -Weekly -At \"7:00AM\" -DaysOfWeek \"Monday\"\r\n$action=\"PowerShell.exe -ExecutionPolicy ByPass -File c:\\test\\export-logs.ps1\"\r\n$sb=[Scriptblock]::Create($action)\r\nRegister-ScheduledJob -Name \"Export Logs\" -ScriptBlock $sb -Trigger $trigger\r\n<\/code><\/pre>\n<p>Note that to run Register-ScheduledJob cmdlet, you need to start PowerShell elevated.<br \/>\nThat&#8217;s all. Now you should have a task that runs every Monday at 7:00, collects events from your servers and exports them to CSV files.<\/p>\n<h3>Conclusion<\/h3>\n<p>As you can see, the problem of exporting events to Excel can be solved without third-party tools. This method is somewhat limited, but it works.<\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Feventlogxp.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F134&#038;t=Exporting%20event%20logs%20with%20Windows%20PowerShell&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Feventlogxp.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F134&#038;p&#091;images&#093;&#091;0&#093;=https%3A%2F%2Feventlogxp.com%2Fblog%2Fwp-content%2Fuploads%2F2016%2F03%2Fpowershell-export1.jpg&#038;p&#091;title&#093;=Exporting%20event%20logs%20with%20Windows%20PowerShell\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none\" src=\"https:\/\/eventlogxp.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/facebook.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Feventlogxp.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F134&#038;text=Check%20this%20Event%20Log%20Explorer%20blog%20post\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none\" src=\"https:\/\/eventlogxp.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/twitter.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-reddit nolightbox\" data-provider=\"reddit\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Reddit\" href=\"https:\/\/www.reddit.com\/submit?url=https%3A%2F%2Feventlogxp.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F134&#038;title=Exporting%20event%20logs%20with%20Windows%20PowerShell\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img alt=\"reddit\" title=\"Share on Reddit\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none\" src=\"https:\/\/eventlogxp.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/reddit.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-pinterest nolightbox\" data-provider=\"pinterest\" target=\"_blank\" rel=\"nofollow\" title=\"Pin it with Pinterest\" href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Feventlogxp.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F134&#038;media=https%3A%2F%2Feventlogxp.com%2Fblog%2Fwp-content%2Fuploads%2F2016%2F03%2Fpowershell-export1.jpg&#038;description=Exporting%20event%20logs%20with%20Windows%20PowerShell\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img alt=\"pinterest\" title=\"Pin it with Pinterest\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none\" src=\"https:\/\/eventlogxp.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/pinterest.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&#038;url=https%3A%2F%2Feventlogxp.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F134&#038;title=Exporting%20event%20logs%20with%20Windows%20PowerShell\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none\" src=\"https:\/\/eventlogxp.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/linkedin.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=Exporting%20event%20logs%20with%20Windows%20PowerShell&#038;body=Check%20this%20Event%20Log%20Explorer%20blog%20post:%20https%3A%2F%2Feventlogxp.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F134\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px\"><img alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none\" src=\"https:\/\/eventlogxp.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/mail.png\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>Do you need to automate error reporting based on recent events and don&#8217;t want to use third-party tools? This article describes how to collect events from different sources and unite them in one document using standard Windows instruments only. Recently I described how to export events into Excel format using our Event Log Explorer software. However, in some cases, using third-party software can be impossible.\u2026 <span class=\"read-more\"><a href=\"https:\/\/eventlogxp.com\/blog\/exporting-event-logs-with-windows-powershell\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":142,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[39],"tags":[38,54,56,53,50,55],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/eventlogxp.com\/blog\/wp-json\/wp\/v2\/posts\/134"}],"collection":[{"href":"https:\/\/eventlogxp.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eventlogxp.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eventlogxp.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/eventlogxp.com\/blog\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":6,"href":"https:\/\/eventlogxp.com\/blog\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/eventlogxp.com\/blog\/wp-json\/wp\/v2\/posts\/134\/revisions\/143"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eventlogxp.com\/blog\/wp-json\/wp\/v2\/media\/142"}],"wp:attachment":[{"href":"https:\/\/eventlogxp.com\/blog\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eventlogxp.com\/blog\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eventlogxp.com\/blog\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}