# ************************************************** # XS-DeleteSnapshotByTag.ps1 # # Author: Urs Heeb # Create: 27.03.2020 # # Note: Enumerate all VMs with a specific Tag # and delete assigned snapshots # # 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" # 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 Snapshots # --- Base scriptlet from pastebin.com --- # Only snapshots of VMs with the tag defined in the variables #Get-XenVM | Where {$_.is_a_snapshot -eq $True -and $_.tags -contains $XenTag} | select name_label $BkpVMs2 = Get-XenVM | Where {$_.is_a_snapshot -eq $True -and $_.tags -contains $XenTag} # Delete snapshot of each VM found before # --- Base scriptlet from pastebin.com --- # Loop added to remove all snapshots found before $BkpVMs2 | ForEach-Object { $Snapshotname2 = $_.name_label Write-Host -ForegroundColor DarkYellow "Snapshot with name $Snapshotname2 will be deleted" Remove-XenVM -Name $Snapshotname2 }