# ************************************************** # XS-CreateSnapshotByTag.ps1 # # Author: Urs Heeb # Create: 27.03.2020 # # Note: Enumerate all VMs with a specific Tag # and create a snapshot # # 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" $XenTag = "Citrix" $BkpDate = Get-Date -Format yyyyMMdd-HHmm # 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 Tag defined in the variables $BkpVMs = Get-XenVM | Where {$_.is_a_template -eq $False -and $_.is_a_snapshot -eq $False -and $_.domid -ne 0 -and $_.tags -contains $XenTag} # 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 }