# ************************************************** # XS-BackupCTXHYV.ps1 # # Author: Urs Heeb # Create: 27.03.2020 # # Note: Enumerate all VMs, create a snapshot, # export the snapshot, and remove it # # Change: 27.03.2020 / uh / Script created # 28.03.2020 / uh / Script modified for blog # # ************************************************** # Import SDK Import-Module XenServerPSModule # Define Variables $Xenhost = "192.168.199.210" $XenUser = "root" $XenPW = "PITPassword" $BkpDate = Get-Date -Format yyyyMMdd-HHmm $BkpStart = (Get-Date).AddHours(-1) # Subtract one hour for the system time / check with summer time $BkpTag = "Backup" $ExportPath = "S:\VM-Backups" # Connect to Citrix Hypervisor # Try the defined host otherwise change to new PoolMaster # --- Base scriptlet from pastebin.com --- Try { $Session = Connect-XenServer -Url https://$Xenhost -UserName $XenUser -Password $XenPW -NoWarnCertificates -SetDefaultSession Write-Host -ForegroundColor DarkGreen "Successful connected to $Xenhost" } Catch [XenAPI.Failure] { [string]$PoolMaster = $_.Exception.ErrorDescription[1] Write-Host -ForegroundColor Red "$($Pools.$Pool) is slave, Master was identified as $PoolMaster, trying to connect" $Pools.Pool = $PoolMaster $Session = Connect-XenServer -url "http://$PoolMaster" -UserName $XenUser -Password $XenPW -NoWarnCertificates -SetDefaultSession Write-Host -ForegroundColor DarkGreen "Successful connected to $PoolMaster" } # Enumerate all VMs # --- Base scriptlet from pastebin.com --- # Added selection of "Backup" Tag $BkpVMs = Get-XenVM | Where {$_.is_a_template -eq $False -and $_.is_a_snapshot -eq $False -and $_.domid -ne 0 -and $_.tags -contains $BkpTag} # Create Snapshot of each found VM # --- Base scriptlet from pastebin.com --- # Loop added and define the snapshot name based on VM name and date/time of backup start $BkpVMs | ForEach-Object { $VMName = $_.name_label $VMUuid = $_.uuid $Snapshotname = "$VMName - $BkpDate" Write-Host -ForegroundColor Gray "Snapshot with name $Snapshotname for VM $VMName (UUID $VMUuid ) will be created" Invoke-XenVM -Uuid $VMUuid -XenAction Snapshot -NewName $Snapshotname } # Enumerate all Snapshots # --- Base scriptlet from pastebin.com --- # Only snapshots of VMs with the "Backup" tag and snapshots newer the start time $BkpVMs2 = Get-XenVM | Where {$_.is_a_snapshot -eq $True -and $_.tags -contains $BkpTag -and $_.snapshot_time -gt $BkpStart} # Export the enumerated Snapshots # --- Base scriptlet from pastebin.com --- $BkpVMs2 | ForEach { $Snapshotname2 = $_.name_label $SnapshotUuid2 = $_.uuid $ExportFile = "$Exportpath\$Snapshotname2.xva" Write-Host -ForegroundColor DarkCyan "Snapshot with name $Snapshotname2 will be exported to $ExportFile" Export-XenVm -Uuid $SnapshotUuid2 -XenHost $Xenhost -path "$ExportFile" } # Delete snapshot of each VM which was created before # --- Base scriptlet from pastebin.com --- # Loop added to remove all snapshots created before $BkpVMs2 | ForEach-Object { $Snapshotname3 = $_.name_label Write-Host -ForegroundColor DarkYellow "Snapshot with name $Snapshotname3 will be deleted" Remove-XenVM -Name $Snapshotname3 }