Skip to content

Instantly share code, notes, and snippets.

@alainassaf
Created December 7, 2018 15:27
Show Gist options
  • Save alainassaf/fd7d3484df0f79d63afd117531d27df7 to your computer and use it in GitHub Desktop.
Save alainassaf/fd7d3484df0f79d63afd117531d27df7 to your computer and use it in GitHub Desktop.
Code to delete XenServer VM and its disks
$vmtodelete = Get-XenVM | Where-Object {$_.is_a_snapshot -eq $false -and $_.is_a_template -eq $false -and $_.is_control_domain -eq $false -and $_.name_label -eq $virtualMachine}
if ($vmtodelete) {
if ($vmtodelete.power_state -eq "Halted") {
Write-Verbose "$virtualmachine is powered off. Deleting $virtualmachine from $xsn"
$vmtodelete.VBDs | Remove-XenVBD #Deletes attached disks
$vmtodelete | Remove-XenVM #Deletes VM
Get-XenSession -Server $_ | Disconnect-XenServer
Exit 0
} else {
Write-Warning "$virtualmachine is not powered off. Exiting script"
Get-XenSession -Server $_ | Disconnect-XenServer
Exit 1
}
} else {
Write-Warning "VM: $virtualMachine does not exist on $xsn"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment