##################################################################################################### # Written by Cengiz Ulusahin # Version 1.1 | 04/02/2019 # This script will backup all Citrix VD templates (or any VM) defined in a CSV file by exporting templates as OVF files using the OVF Tool utility # The script will power off the templates (if powered on) before carrying out the export process # The OVF files will be overwritten each time this script runs # Logs will be archived in the Log-Archive folder # The script will email log files to the user addresses defined in the variables section for verification purposes ##################################################################################################### #Function to zip log files Function ZipFiles($logzipfile, $logsourcedir) { Add-Type -Assembly System.IO.Compression.FileSystem $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal [System.IO.Compression.ZipFile]::CreateFromDirectory($logsourcedir, $logzipfile, $compressionLevel, $false) } #Function to send logs via email Function Send-Logs { Send-MailMessage -SmtpServer "$($SMTPserver)" -To "$($SMTPto)" -From "$($SMTPfrom)" -Subject "$($region) VDI Template backup job is complete" -Body "$($region) VDI Template backup job is complete, please check attached log files!" -Attachment $($logzipfile) } #Modify variables below $region = "UK" $drive = "D:" $backuppath = "\Template-Backups" $SMTPserver = "smtp.domain.com" $SMTPfrom = "email@domain.com" $SMTPto = "emeail@domain.com" $vcenterUsr = "user@vsphere.local" $vcenterUsrPsw = "PASSWORD" $vcenter = "vcenter.domain.com" $VDC = "Datacentre" $Folder ="VM folder" #Save and schedule script in Windows scheduler using an account which has access to the backup path #Do not modify following variable $console_log = "$($drive)$($backuppath)\console-log.txt" $( #Connect to vCenter Connect-Viserver $vcenter -Username $vcenterUsr -Password $vcenterUsrPsw #Create folder structure $ChkBkpPath = "$($drive)$($backuppath)\Backups" $BkpFolderExists = Test-Path -PathType Container $ChkBkpPath If ($BkpFolderExists -eq $True) {} else { New-Item -ItemType directory -Path "$($drive)$($backuppath)\Backups" } $ChkLogPath = "$($drive)$($backuppath)\Logs" $LogFolderExists = Test-Path -PathType Container $ChkLogPath If ($LogFolderExists -eq $True) {} else { New-Item -ItemType directory -Path "$($drive)$($backuppath)\Logs" } $ChkLogArchivePath = "$($drive)$($backuppath)\Log-Archive" $LogArchiveFolderExists = Test-Path -PathType Container $ChkLogArchivePath If ($LogArchiveFolderExists -eq $True) {} else { New-Item -ItemType directory -Path "$($drive)$($backuppath)\Log-Archive" } #Delete old log files Get-ChildItem -Path "$($drive)$($backuppath)\Logs\*.*" | Remove-Item #Do not modify following variables $srcFile = "$($drive)$($backuppath)\VDI-templates-$($region).csv" $templates = Import-Csv -path $srcFile $ovftool = "$($drive)$($backuppath)\VMware OVF Tool\ovftool.exe" $ovflogexportdir = "$($backuppath)\Logs" $ovfexportdir = "$($backuppath)\Backups" $date = get-date -f dd-MM-yyyy_HH-mm $logzipfile = "$($drive)$($backuppath)\Log-Archive\logs_$($date).zip" $logsourcedir = "$($drive)$($backuppath)\Logs" #When testing add "--noDisks" to prevent the VMDK file from downloading each time foreach ($template in $templates){ & $ovftool "--X:logFile=$($ovflogexportdir)\$($template.Name).txt" "--X:logLevel=verbose" "--noDisks" "--shaAlgorithm=sha1" "--allowAllExtraConfig" "--powerOffSource" "--overwrite" "vi://$($vcenterUsr):$($vcenterUsrPsw)@$($vcenter)/$($VDC)/vm/$($Folder)/$($template.Name)" "$($ovfexportdir)" } #Discconect from vCenter Disconnect-VIServer -server $vcenter -Confirm:$false #Zip log files ZipFiles $logzipfile $logsourcedir #Send logs for verification Send-Logs ) *>&1 > "$console_log"