您的位置:首页 > 运维架构 > Shell

利用powershell删除早于某个指定日期的文件 推荐

2014-07-10 15:43 453 查看
近日监控到有台WEB服务器C盘接近满容,检查发现有两处自动增长;
(1)Windows Update保存更新文件的文件夹;(处理方式:更改Windows Update设置或者设置powershell每天自动删除,我选择了后者)
(2)web站点的日志记录;(处理方式:使用powershell保留近两天的日志文件即可)

把把以下命令保存为ps1脚本,添加到Windows计划任务中设定每天固定时间执行即可(可参考这篇文章http://281816327.blog.51cto.com/907015/1436748);

#delelte system update files
Stop-Service wuauserv
Get-ChildItem -path C:\Windows\SoftwareDistribution | Remove-Item -Recurse -force
Start-Service wuauserv

#delete logs in specify website, just save logs in two days~
$TimeOutDays=1
$filePath="logspath"
$allFiles=get-childitem -path $filePath
foreach ($files in $allFiles)
{
$daypan=((get-date)-$files.lastwritetime).days
if ($daypan -gt $TimeOutDays)
{
remove-item $files.fullname -Recurse -force
}
}

参数说明:
-Recurse 表示递归,删除子文件和子文件夹
-Force 表示强制删除,不询问
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  删除文件 powershell