#Logging
Start-Transcript -Path C:\ProgramData\NewMornings\Backup\LastBackup.log -WarningAction
#Configuration parameters
$BackupPath = "\\nm-dt1\c$\ProgramData\Newmornings\VM Backups"
$Limit = (Get-Date).AddDays(-4)
$Date = Get-Date -Format yyyyMMdd
#Email notification settings
$SMTPServer = "mail.intranet.example.com"
$To = "[email protected]"
$From = "[email protected]"
$Subject = "$Date backup logs"
#Execution -->
#Delete old backup files
Get-ChildItem "$BackupPath" -Directory -Recurse | Where-Object { $_.CreationTime -lt $Limit
} | Remove-Item -Recurse
#Backup VMs
Export-VM DC,FileServer -Path $BackupPath\$Date
#Send directory listing for backup verification
$Username = "anonymous"
$Password = ConvertTo-SecureString -String "anonymous" -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($Username,$Password)
$Body = Get-ChildItem -Path $BackupPath\$Date -Recurse -Filter "*.vhd*" |
Select-Object Name,LastWriteTime,Length | ConvertTo-Html -Fragment
Send-MailMessage -SmtpServer $SMTPServer -From $From -To $To -Subject $Subject -BodyAsHtml -Body "$Body" -Credential $Credentials
Stop-Transcript
