Simplifying Solutions
I have to cleanup a server share daily. The following powershell script will remove old files and email a note to me telling me it ran.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # # setup variables # $ageInDays = 7 $fileShare = <span class="str">"serversharesubfolder"</span> # # cleanout $fileShare and leave # $ageInDays worth of recent files # Get-ChildItem $fileShare -Recurse | <span class="kwrd">where</span> {$_.LastWriteTime -le (Get-Date).AddDays(-$ageInDays)} | Remove-Item -recurse # # setup to mail the results # $SmtpClient = <span class="kwrd">new</span>-<span class="kwrd">object</span> system.net.mail.smtpClient $SmtpServer = <span class="str">"smtp.domain.com"</span>$SmtpClient.host = $SmtpServer $From = <span class="kwrd">new</span>-<span class="kwrd">object</span> System.Net.Mail.MailAddress(<span class="str">"scheduledtask@domain.com"</span>, <span class="str">"Schedule Task"</span>) $To = <span class="str">"me@domain.com"</span> $Title = <span class="str">"Snapshot cleanup run"</span> $Body = <span class="str">"The powershell snapshot cleanup of $fileShare successfully ran. This left $ageInDays days of snapshots. Script location is 'serversharecleanup_snapshots.ps1'"</span> # # mail the results # $SmtpClient.Send($from,$to,$title,$Body) |
I need to get it to mail on failure, but I’ve not gotten that far.
Comments Closed.