-1

我有一个 Powershell 脚本,它每晚都作为计划任务运行。它工作得很好。我现在要做的是在另一台机器上使用相同的脚本来删除早于 x 天的特定日志文件。日志文件的路径:C:\Company\Logs 这里有 4 个不同的日志文件。我需要能够独立删除每种类型并发送脚本中已经存在的相同类型的报告。

main.log.date.txtlog

companyname.date.txtlog

dupdump.txt

sfsync.date.txt

这是我目前可以使用的脚本。

dir C:\inetpub\logs\LogFiles\W3SVC1 -recurse | where {!$_.PsIsContainer -AND $_.lastWriteTime -lt (Get-Date).AddDays(-10) } | select LastWriteTime,@{n="Path";e={convert-path $_.PSPath}} | tee C:\LogCleanup\deleted.txt | Remove-Item -force

$filename = "C:\LogCleanup\deleted.txt"
$smtpserver = “smtp.company.net”
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($filename)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.EnableSsl = $True
$smtp.Credentials = New-Object System.Net.NetworkCredential(“monitor@company.net”, “password”);  # Put username without the @GMAIL.com or – @gmail.com
$msg.From = “DeletedFilesLog@company.net”
$msg.To.Add(”is@company.net")
$msg.Subject = “The IIS log files in the attached file have been deleted.”
$msg.Body = “The IIS log files on MACHINE NAME have been deleted. See the attached file to view     the log files that were deleted today”
$msg.Attachments.Add($att)
$smtp.Send($msg)
4

1 回答 1

0

这应该有效:

$time = "-7"    
$filename = main.log.date.txtlog
Get-ChildItem "\\$PATH" -Recurse | Where {$_.creationtime -lt (Get-Date).AddDays($time)} | Remove-Item -Include $filename -Force 

$Time 确定文件在被删除之前必须存在多长时间。$filename 是确切的文件名,不管它叫什么。

于 2013-10-18T06:05:18.640 回答