Skip to content

Instantly share code, notes, and snippets.

@peterhynes
Created July 28, 2017 08:45
Show Gist options
  • Select an option

  • Save peterhynes/8487a8357b18d48774387ed2e8a64d75 to your computer and use it in GitHub Desktop.

Select an option

Save peterhynes/8487a8357b18d48774387ed2e8a64d75 to your computer and use it in GitHub Desktop.
#Script to delete VMs by Peter Hynes
#Version 1.10
#Created on the 16\03\2017
#Connect to the vCenter or vcenters and Run within PowerCLI
#You will need to create a txt file called vms.txt and place it in the same folder as this script. Put the list of VMs you want to delete in to this txt file.
#Load powercli modules
Get-Module -Name VMware* -ListAvailable | Import-Module
Add-PSSnapin vmware*
#importing list of VMs from txt file
$vms = get-content -path '.\vms.txt'
#Create Powered off and Powered on date function
function Get-VMLastPoweredOffDate {
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine] $vm)
process {
$Report = "" | Select-Object -Property Name,LastPoweredOffDate
$Report.Name = $_.Name
$Report.LastPoweredOffDate = (Get-VIEvent -Entity $vm | `
Where-Object { $_.Gettype().Name -eq "VmPoweredOffEvent" } | `
Select-Object -First 1).CreatedTime
$Report
}
}
function Get-VMLastPoweredOnDate {
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine] $vm)
process {
$Report = "" | Select-Object -Property Name,LastPoweredOnDate
$Report.Name = $_.Name
$Report.LastPoweredOnDate = (Get-VIEvent -Entity $vm | `
Where-Object { $_.Gettype().Name -eq "VmPoweredOnEvent" } | `
Select-Object -First 1).CreatedTime
$Report
}
}
New-VIProperty -Name LastPoweredOffDate -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOffDate -vm $Args[0]).LastPoweredOffDate} | Out-Null
New-VIProperty -Name LastPoweredOnDate -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOnDate -vm $Args[0]).LastPoweredOnDate} | Out-Null
#vCenter you are working in
$vcenter = read-host "Please enter the name of the vCenter you will be working in, if working in multiple VCs seperate them with a comma"
#connect to the VC
connect-viserver $vcenter
#space
write-host "`n"
#Check for powerstate of VMs
Write-Host "Checking for powered on VMs in your list of VMs...." -foregroundcolor Green
$poweredonvms = Get-vm $vms | where {$_.PowerState -eq "PoweredOn"}
$poweredoffvms = Get-vm $vms | where {$_.PowerState -eq "PoweredOff"}
if ($poweredonvms -eq $null){
#space
write-host "`n"
Write-host "There are no powered on VMs..."
}
Elseif ($poweredoffvms -eq $null){
#space
write-host "`n"
Write-host "All the VMs you listed seem to be powered on... The script will exit in 30 seconds or press Ctrl+C to exit now and check your VMs"
$poweredonvms | select name, powerstate | Out-File .\PoweredOnVMs.txt
Sleep -s 30
Exit
}
Else{
#space
write-host "`n"
#List powered on VMs and Write list of powered on VMs to text file
write-host "The following VMs are powered on and will not be deleted. This list of VMs will be written to a txt file @ .\PoweredOnVMs.txt " -foregroundcolor Green
$poweredonvms
$poweredonvms | select name, powerstate | Out-File .\PoweredOnVMs.txt
}
#space
write-host "`n"
#Prints a list of VMs to be deleted and the their power stats
Write-Host "Listing last powered off and last powered on date for your powered off VMs. VMs should be powered off for 7 days before deletion" -foregroundcolor Green
Get-VM $poweredoffvms | Select-Object -property Name, LastPoweredOnDate, LastPoweredOffDate
#space
write-host "`n"
Write-host "The powered off VMs listed will be deleted. Press Y to continue or N to quit " -foregroundcolor Green
$confirmation = read-host
if ($confirmation -eq 'y'){
Remove-VM $poweredoffvms -DeletePermanently -Confirm:$false
Write-host "The powered off VMs have been deleted."
Disconnect-VIServer -server $vCenter -confirm:$false
}
else {
Write-Host "Deletions Cancelled"
Disconnect-VIServer -server $vCenter -confirm:$false
Sleep -s 5
Exit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment