VMware Cloud Community
JayMiller29
Contributor
Contributor
Jump to solution

Script to Delete VM on a nightly basis

Hoping someone can point me in the right direction on this one...

I have a requirement to clone a VM on a nightly basis. The cloned VM will be in production after it's customization. Obviouisly, I want to delete the existing clone before creating the new clone, otherwise I'm going to waste large amounts of disk space.

I've been going through as many scripting forums as I can find, yet cannot find anything stating how this can be accomplished. From what I've read, it "should" be possible via the PowerGUI & VItoolkit, yet I haven't found anything stating that anyone has successfully done this.

Have any of you been able to get a script going that will delete a VM on a nightly basis?

Thanks in advance,

Jay

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Did you look at the Remove-VM cmdlet ?

Get-VM <VM-name> | Remove-VM –DeleteFromDisk

The -DeleteFromDisk parameter will remove all the files from the datastore.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Did you look at the Remove-VM cmdlet ?

Get-VM <VM-name> | Remove-VM –DeleteFromDisk

The -DeleteFromDisk parameter will remove all the files from the datastore.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
JayMiller29
Contributor
Contributor
Jump to solution

Thanks LucD....that's all I needed...the push in the right direction w/ the cmdlet. I appreciate the help!!

For those of you just getting started w/ scripting in VMware, here's what I did w/ my script.

Note: My requirement here is to clone a VM into 2 differenet VM's every night, hence my needing to delete the VM's on a nightly basis. So, before the clone job in Vcenter runs, I run the following script:

Connect-VIServer cwvcenter -User myvcenteruser -Password ########

Get-VM demo | Stop-VM -Confirm:$false

Start-Sleep -Seconds 10

Get-VM demo | Remove-VM -DeleteFromDisk -Confirm:$false

Start-Sleep -Seconds 10

Get-VM train | Stop-VM -Confirm:$false

Start-Sleep -Seconds 10

Get-VM train | Remove-VM -DeleteFromDisk -confirm:$false

Notes about the above script:

  • I decided to use a script to power down the VM's. It can be done in Vcenter's scheduled tasks, however I chose to do it in a script.

  • I put in the "start-sleep -seconds 10" commands just to be sure that nothing was stomping on anything. I figured 10 second "pauses" between certain commands couldn't hurt.

So, at this point, I've got my script to do what I need. It powers down the VM's, and then deletes them. Vcenter comes in a little while later & clones the "source" VM and customizes w/ the customization templates.

However....I was then stuck with the question "How do I schedule this? There has to be a way to do it w/ a Windows scheduled task". After some research and testing, I found a way to do it.

I wrote a batch file that the windows scheduled task calls on a nightly basis. The batch file syntax is as such:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSconsolefile "C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1" -command C:\scripts\main-train-poweroffanddelete.ps1

I then scheduled the task to use the batch file, and it works exactly how I wanted it to.

I hope this helps anyone in the future that is getting into powershell/VItoolkit scripting in your VMware Infrastructures.

0 Kudos