{"id":59,"date":"2012-07-27T09:25:36","date_gmt":"2012-07-27T14:25:36","guid":{"rendered":"https:\/\/sysadminnightmare.com\/?p=59"},"modified":"2012-07-27T09:25:36","modified_gmt":"2012-07-27T14:25:36","slug":"power-shell-script-to-clear-and-save-event-logs","status":"publish","type":"post","link":"https:\/\/sysadminnightmare.com\/index.php\/2012\/07\/27\/power-shell-script-to-clear-and-save-event-logs\/","title":{"rendered":"Power Shell Script to Clear and Save Event Logs"},"content":{"rendered":"<p>This is a work in progress script that will Save and clear event logs. I got this from <a href=\"http:\/\/technet.microsoft.com\/en-us\/magazine\/2009.07.heyscriptingguy.aspx\">http:\/\/technet.microsoft.com\/en-us\/magazine\/2009.07.heyscriptingguy.aspx<\/a><\/p>\n<p>In the futre I will be adding code to upload the files to Sharepoint Document Library or List (still thinking of the best way I want to review these). I will link to that part and blog post when I complete.<\/p>\n<p>[code]<\/p>\n<p>Param(<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $LogsArchive = &#8220;c:logarchive&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $List,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $computers,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [switch]$AD,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [switch]$Localhost,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [switch]$clear,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [switch]$Help<br \/>\n\u00a0\u00a0\u00a0\u00a0 )<br \/>\nFunction Get-ADComputers<br \/>\n{<br \/>\n\u00a0$ds = New-Object DirectoryServices.DirectorySearcher<br \/>\n\u00a0$ds.Filter = &#8220;ObjectCategory=Computer&#8221;<br \/>\n\u00a0$ds.FindAll() |<br \/>\n\u00a0\u00a0\u00a0\u00a0 ForEach-Object { $_.Properties[&#8216;dnshostname&#8217;]}<br \/>\n} #end Get-AdComputers<\/p>\n<p>Function Test-ComputerConnection<br \/>\n{<br \/>\n\u00a0ForEach($Computer in $Computers)<br \/>\n\u00a0{<br \/>\n\u00a0 $Result = Get-WmiObject -Class win32_pingstatus -Filter &#8220;address=&#8217;$computer'&#8221;<br \/>\n\u00a0 If($Result.Statuscode -eq 0)<br \/>\n\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0 if($computer.length -ge 1)<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Write-Host &#8220;+ Processing $Computer&#8221;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Get-BackUpFolder<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0 } #end if<br \/>\n\u00a0\u00a0 else { &#8220;Skipping $computer .. not accessible&#8221; }<br \/>\n\u00a0} #end Foreach<br \/>\n} #end Test-ComputerConnection<\/p>\n<p>&nbsp;<\/p>\n<p>Function Get-BackUpFolder<br \/>\n{<br \/>\n\u00a0$Folder = &#8220;{1}-Logs-{0:MMddyymm}&#8221; -f [DateTime]::now,$computer<br \/>\n\u00a0 New-Item &#8220;$LogsArchive$folder&#8221; -type Directory -force\u00a0 | out-Null<br \/>\n\u00a0 If(!(Test-Path &#8220;<a href=\"file:\/\/$computer\/c$\/LogFolder\/$folder\">\\$computerc$LogFolder$folder<\/a>&#8220;))<br \/>\n\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0 New-Item &#8220;<a href=\"file:\/\/$computer\/c$\/LogFolder\/$folder\">\\$computerc$LogFolder$folder<\/a>&#8221; -type Directory -force | out-Null<br \/>\n\u00a0\u00a0\u00a0 } #end if<br \/>\n\u00a0Backup-EventLogs($Folder)<br \/>\n} #end Get-BackUpFolder<\/p>\n<p>Function Backup-EventLogs<br \/>\n{<br \/>\n\u00a0$Eventlogs = Get-WmiObject -Class Win32_NTEventLogFile -ComputerName $computer<br \/>\n\u00a0Foreach($log in $EventLogs)<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $path = &#8220;<a href=\"file:\/\/%7b0%7d\/c$\/LogFolder\/$folder\/%7B1%7D.evt\">\\{0}c$LogFolder$folder{1}.evt<\/a>&#8221; -f $Computer,$log.LogFileName<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $ErrBackup = ($log.BackupEventLog($path)).ReturnValue<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if($clear)<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if($ErrBackup -eq 0)<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $errClear = ($log.ClearEventLog()).ReturnValue<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } #end if<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;Unable to clear event log because backup failed&#8221;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;Backup Error was &#8221; + $ErrBackup<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } #end else<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } #end if clear<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Copy-EventLogsToArchive -path $path -Folder $Folder<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } #end foreach log<br \/>\n} #end Backup-EventLogs<\/p>\n<p>Function Copy-EventLogsToArchive($path, $folder)<br \/>\n{<br \/>\n\u00a0Copy-Item -path $path -dest &#8220;$LogsArchive$folder&#8221; -force<br \/>\n} # end Copy-EventLogsToArchive<\/p>\n<p>Function Get-HelpText<br \/>\n{<br \/>\n\u00a0$helpText= `<br \/>\n@&#8221;<br \/>\n\u00a0DESCRIPTION:<br \/>\n\u00a0NAME: BackUpAndClearEventLogs.ps1<br \/>\n\u00a0This script will backup, archive, and clear the event logs on<br \/>\n\u00a0both local and remote computers. It will accept a computer name,<br \/>\n\u00a0query AD, or read a text file for the list of computers.<\/p>\n<p>\u00a0PARAMETERS:<br \/>\n\u00a0-LogsArchive local or remote collection of all computers event logs<br \/>\n\u00a0-List path to a list of computer names to process<br \/>\n\u00a0-Computers one or more computer names typed in<br \/>\n\u00a0-AD switch that causes script to query AD for all computer accounts<br \/>\n\u00a0-Localhost switch that runs script against local computer only<br \/>\n\u00a0-Clear switch that causes script to empty the event log if the back succeeds<br \/>\n\u00a0-Help displays this help topic<\/p>\n<p>\u00a0SYNTAX:<br \/>\n\u00a0BackUpAndClearEventLogs.ps1 -LocalHost<\/p>\n<p>\u00a0Backs up all event logs on local computer. Archives them to C:logarchive.<\/p>\n<p>\u00a0BackUpAndClearEventLogs.ps1 -AD -Clear<\/p>\n<p>\u00a0Searches AD for all computers. Connects to these computers, and backs up all event<br \/>\n\u00a0logs. Archives all event logs to C:logarchive. It then clears all event logs<br \/>\n\u00a0if the backup operation was successful.<\/p>\n<p>\u00a0BackUpAndClearEventLogs.ps1 -List C:fsoListOfComputers.txt<\/p>\n<p>\u00a0Reads the ListOfComputers.txt file to obtain a list of computer. Connects to these<br \/>\n\u00a0computers, and backs up all event logs. Archives all event logs to C:logarchive.<\/p>\n<p>\u00a0BackUpAndClearEventLogs.ps1 -Computers &#8220;Berlin,Vista&#8221; -LogsArchive &#8220;<a href=\"file:\/\/berlin\/C$\/fso\/Logs\">\\berlinC$fsoLogs<\/a>&#8221;<\/p>\n<p>\u00a0Connects to a remote computers named Berlin and Vista, and backs up\u00a0\u00a0\u00a0 all event<br \/>\n\u00a0logs. Archives all event logs from all computers to the path c:fsoLogs directory on<br \/>\n\u00a0\u00a0 a remote computer named Berlin.<\/p>\n<p>BackUpAndClearEventLogs.ps1 -help<\/p>\n<p>Prints the help topic for the script<br \/>\n&#8220;@ #end helpText<br \/>\n\u00a0 $helpText<br \/>\n}<\/p>\n<p># *** Entry Point To Script ***<\/p>\n<p>If($AD) { $Computers = Get-ADComputers; Test-ComputerConnection; exit }<br \/>\nIf($List) { $Computers = Get-Content -path $list; Test-ComputerConnection; exit }<br \/>\nIf($LocalHost) { $computers = $env:computerName; Test-ComputerConnection; exit }<br \/>\nIf($Computers)<br \/>\n\u00a0 {<br \/>\n\u00a0\u00a0 if($Computers.Contains(&#8220;,&#8221;)) {$Computers = $Computers.Split(&#8220;,&#8221;)}<br \/>\n\u00a0\u00a0 Test-ComputerConnection; exit<br \/>\n\u00a0 }<br \/>\nIf($help) { Get-HelpText; exit }<br \/>\n&#8220;Missing parameters&#8221; ; Get-HelpText<\/p>\n<p>[\/code]<\/p>\n<p>&nbsp;<\/p>\n<p>basically to run this create a Script (powershell if you like) or just type:<\/p>\n<p>&nbsp;<\/p>\n<p>c:BackupAndClearEventLogs.ps1 -Computers &#8220;Server1,Server2&#8221; -LogsArchive &#8220;c:logs&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a work in progress script that will Save and clear event logs. I got this from http:\/\/technet.microsoft.com\/en-us\/magazine\/2009.07.heyscriptingguy.aspx In the futre I will be adding code to upload the files to Sharepoint Document Library or List (still thinking of the best way I want to review these). I will&#8230; <a class=\"continue-reading-link\" href=\"https:\/\/sysadminnightmare.com\/index.php\/2012\/07\/27\/power-shell-script-to-clear-and-save-event-logs\/\"> Continue reading <span class=\"meta-nav\">&rarr; <\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2],"tags":[35,39],"class_list":["post-59","post","type-post","status-publish","format-standard","hentry","category-admin","tag-powershell","tag-scripting"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2bgeE-X","_links":{"self":[{"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/posts\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/comments?post=59"}],"version-history":[{"count":0,"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/posts\/59\/revisions"}],"wp:attachment":[{"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/media?parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/categories?post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/tags?post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}