####################################################
# WBBackup.ps1
# Backup Windows 2008 with WindowsBackup
# Version 1.0
# modified: 19/02/2010
# Autore: Gianluca Ferri
####################################################
# Mapping network share
#(New-Object -ComObject Wscript.Network.MapNetworkDrive)("X:", "\\nas01\Backup", $false, "BackupUser","BackupPassword")
# Var
$Customer="Customer"
$DestFolder="\\Nas01\Backup\" # Backup destination
$LogFile="C:\Dati\Logs\Backup\" # Logfile
$LogsFolder="C:\Dati\Logs\Backup\" # Logs archive folder
[Array] $Services="" #"Virtual Machine Management Hyper-V" # Array of services to stop/start
$SMTPServer="SMTP server"
$SMTPServerUser="SMTPUser"
$SMTPServerPass=ConvertTo-SecureString "SMTPPassword" -AsPlainText -Force
$MailFrom=$Customer + " Backup <indirizzo mittente>"
$MailTo="indirizzo destinatario"
$MailBcc="indirizzo destinatario"
$Emailsubject="[" + $Customer + "] Report backup " + (get-wmiobject win32_computersystem).name
$DaysOld=15 # Logs retain days
$Culture= New-Object Globalization.CultureInfo "it-IT"
$today=Get-Date
$DestFolder=$DestFolder + $today.dayofweek.tostring($Culture)
if (!(Test-Path $DestFolder))
{
# Make folder
New-Item $DestFolder -type directory
}
# Stop services
if ($Service.count>0) {foreach ($serv in $Services) {stop-service $serv}}
# Start backup
$commandline="wbadmin.exe"
$params={start backup `-backuptarget:$DestFolder `-include:"C:" `-VSSFull -allcritical -quiet}
$LogFile=$LogFile + $Customer + (Get-Date -f yyyyMMdd) + ".log"
$exec = "$commandline $params >$LogFile"
Invoke-Expression $exec
# Restart services
if ($Service.count>0) {foreach ($serv in $Services) {start-service $serv}}
# Send email report
$Credential=New-Object System.Management.Automation.PSCredential ($SMTPServerUser, $SMTPServerPass)
send-mailmessage -to $MailTo -bcc $MailBcc -from $MailFrom -subject $Emailsubject -Attachments $LogFile -smtpServer $SMTPServer -Credential $Credential
# Delete older logs
dir $LogsFolder -recurse | where {!$_.PsIsContainer -AND $_.lastWriteTime -lt (Get-Date).AddDays(-$DaysOld) } | select LastWriteTime,@{n="Path";e={convert-path $_.PSPath}} | Remove-Item -force