rss logo

My Windows Backup PowerShell Script

A simple PowerShell script to do a windows backup and which send an e-mail report.

Code

###########################
# author : shebangthedolphins.net
# version : 1.0
# date : 2017.08
# role : windows backup a local server to \\192.168.1.90\d$\ntbackup\ share and send an e-mail report
# other : Work only with PowerShellv2
# updates :
#       - 1.X (x/x/xxxx) :    

#get days name
$backupdate = Get-Date -Format ddd

#Function Mail
#Role : Send email with Subject in argument
Function Mail
 {
	param ([string]$emailbody, [string]$sujet)

        $encoding = [System.Text.Encoding]::UTF8
        Send-MailMessage -To destination@shebangthedolphins.net -Subject $sujet -From source@shebangthedolphins.net -smtpserver smtp.shebangthedolphins.net -Body $sujet -Encoding $encoding
 }

#create a destination folder
New-Item -ItemType directory -Name $backupdate -Path \\192.168.1.90\d$\ntbackup\SRV01\

#Backup command. It will save C and D disk to \\192.168.1.90\d$\ntbackup\SRV01\ share
wbadmin start backup -backuptarget:\\192.168.1.90\d$\ntbackup\SRV01\$backupdate -systemState -include:C:,D: -quiet

if ($LASTEXITCODE -eq '0')
{
    Mail "Backup OK" "[Windows Backup] SRV01 backup successed"
}
Else
{
    Mail "Backup KO" "[Windows Backup] SRV01 backup has failed"
}
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address