周末将这个脚本做了一些改进,主要是借用wpk module,将参数部分通过gui来输入,同时对各参数做了一些判断,防止误操作。具体的代码如下:
#* FileName:  remove-log.ps1
########################################################
# remove log file
#* Created:   
#* Version  2.0
#* Author:     Shawn Shi
#* Company:     PUMCH
#* Email:     shicheng@live.it
#* Web:         http://orca2007.spaces.live.com
#* Reqrmnts:  powershell 2.0,WPK
#* Keywords: 
########################################################
Write-Warning "This cmdlet will delete the file under the Directory with assignment type and time"
write-host -ForegroundColor yellow  "Please input path as c:\windows\logs,input type as log,input date as 5"

$Param = New-Grid -Rows 4 -Columns 'Auto','1*' {
    $TextChanged = {
        $path = Get-Resource path | Select-Object -ExpandProperty Text
        $type = Get-Resource type | Select-Object -ExpandProperty Text
 $date = Get-Resource date | Select-Object -ExpandProperty Text
        $this.Parent.Tag = "$path,$type,$date"
    }
    New-Label "Directory"
    New-TextBox -Name path  -Column 1 -On_Loaded {
        Set-Resource -Name path  -Value $this -Depth -1
    } -On_TextChanged $TextChanged
    New-Label "type" -Row 1
    New-TextBox -Name type  -Column 1 -Row 1 -On_Loaded {
        Set-Resource -Name type  -Value $this -Depth -1
    } -On_TextChanged $TextChanged
        New-Label "date" -Row 2
    New-TextBox -Name date  -Column 1 -Row 2 -On_Loaded {
        Set-Resource -Name date  -Value $this -Depth -1
    } -On_TextChanged $TextChanged
    New-Button -Name Done "Done" -Row 4 -On_Click {
        #$path = $window | Get-ChildControl path
        #$type = $window | Get-ChildControl type
        #$date = $window | Get-ChildControl date
        #if (-not $path.Text) {
        #    [Windows.Messagebox]::show("What is Directory?")
        #}
        #if (-not $type.Text) {
        #    [Windows.Messagebox]::show("What is type?")
        #}
        #if (-not $date.Text) {
        #    [Windows.Messagebox]::show("What is date?")
        #}
       
        $window.Close()
    }
} -show

$temp=$Param.Split(",")
$fileDirectory=$temp[0]
$filetype="*."+$temp[1]
$date=$temp[2]
Try
{
if(-not  $fileDirectory )
{
write-host -ForegroundColor red  "The Directory can't be null"
}
if(-not $filetype )
{
write-host -ForegroundColor red  "The type can't be null"
}
if(!($date -ge "0"))
{
write-host -ForegroundColor red  "The date can't be null"
}
else
{
$logfiles=Get-ChildItem $fileDirectory -Recurse -Force -include $filetype
foreach($logfile in $logfiles)
{
$logfilepath=Join-Path $logfile.Directoryname $logfile.Name
$LastAccessTime=Get-ItemProperty $logfilepath
$currentdate=Get-Date
$currentdate.AddDays($date)
if($LastAccessTime.LastWriteTime -lt $currentdate.AddDays(-$date))
{
Remove-Item $logfilepath -Recurse -Force
write-host -ForegroundColor red  "Removing  "  $logfilepath
}
}
}
}
Catch
{
Write-Host "操作失败。错误原因:"$Error[0]
}

Finally
{
Remove-Variable fileDirectory
Remove-Variable filetype
Remove-Variable date
Remove-Variable Param
Remove-Variable temp
}
 
 
 
在做系统管理时,如果开启了较多的审计项目,系统的日子尺寸将是非常惊人的。为了保证系统的性能,删除日志也是管理员需要处理的问题之一。
今天就给出一个删除日志文件的脚本范例,大家可以根据自己的情况进行剪裁。
 
$logfiles=Get-ChildItem C:\Windows\Logs -Recurse -Force -include *.log
foreach($logfile in $logfiles)
{
$logfilepath=Join-Path $logfile.Directoryname $logfile.Name
$LastAccessTime=Get-ItemProperty $logfilepath
$currentdate=Get-Date
if($LastAccessTime.LastWriteTime -lt $currentdate.AddDays(-5))
{
Remove-Item $logfilepath -Recurse -Force
}
}
 
如果大家愿意,可以将文件路径、文件类型、时间三个部门做成参数,在传递给移除日志的这个function。