W2KSG: Unique file names

Having seen how to back up our event logs we will need to do this periodically so we need to create unique file names for the backups. The obvious candidate is to base it on the date.

Listing 12.7

$date = Get-Date
Get-WmiObject -Class Win32_NTEventLogFile | Where {$_.NumberofRecords -gt 5} | Foreach {
    $file = "c:\test\" + $_.LogFileName + "_{0}_{1}_{2}.evt" -f $date.Year, $date.Month, $date.Day
    $_.BackupEventLog($file)
    $_.ClearEventLog()
}

This adapts the previous script by getting the date. We then push the year, month and day into the string for the file name as shown.  That -f operator pops up in a number of very useful places.

The backup and clearing occur as previously. In this case we are using number of records rather than size. You could use both!

 

Share this post :

 

Technorati Tags: ,,

Read the complete post at http://richardsiddaway.spaces.live.com/Blog/cns!43CFA46A74CF3E96!1821.entry

Published Thu, Oct 16 2008 11:46 by Richard Siddaway's Blog
Filed under: