Created
December 7, 2018 15:27
-
-
Save alainassaf/fd7d3484df0f79d63afd117531d27df7 to your computer and use it in GitHub Desktop.
Code to delete XenServer VM and its disks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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