PowerCLI: Delete all snapshots by name

I have the issue that we use a cloud automation software, which for whatever reason failed to delete the hypervisor snapshots.

Now I was looking into a quick way to delete all those 520 snapshots with PowerCLI, and I found something pretty quick.


Based on that, I came up with my own quick PowerCLI one-liner, that’ll list all VMs and their snapshots:

Now, I could use another one-liner to delete all snapshots, that’ll look like this:

However, this will either crash an ESXi host (because the amount of the snapshots is too much) or overwealm the storage. So in the end I used a script like this:

This’ll limit the maximum amount of snapshots to delete at times to 5 (or 10 if you change the $maxtasks value).

Quick-Add a bunch of Virtual Machine Port-Groups from CSV

Well, I’m currently migrating between different cluster layouts, and I had to create the new PortGroups on the old hosts, in order for me to switch between old and new hosts.


And the corresponding CSV would look like this:


vCenter: Removing VSC custom attributes

Well, yesterday I got pissed of those Virtual Storage Console custom attributes.

Currently we don’t use the Provisioning & Cloning feature of the VSC, thus we don’t need the custom attributes. After poking around, I decided to write a short PowerCLI script to do the task.

It’s really rather simple, so here goes:

vm-online-backup – Another day, another PowerCLI script

Well, on Friday I had a short chat with someone from one of our application departments, stating he wanted a backup copy of a VM (ain’t to hard), but a) they don’t want any downtime and b) it has to be identical to the original.

So I sat down today, googled for a bit and actually found something that pretty much does what I want, though I had to fix it up a bit … So find attached a script, which creates a hot-clone from a snapshot and then only if the latest clone was successful deletes the old one.

The backupvms.csv looks pretty simple:

Microsoft Cluster on VMware and Devices

Well, once again the Microsoft Cluster on VMware bit my ass … As you might know, MSCS on VMware is a particular kind of pain, with each upgrade you end up with the same problem over and over again (SCSI reservations on the RDM-LUNs being one, and the passive node not booting being the other).

So I opened up another support case with VMware, and the responded like this:

Please see this kb entry: http://kb.vmware.com/kb/1016106

This doesn’t completely fit my case, but since the only active cluster-node failed yesterday evening (it’s only our internal file-share server, thus no worries), I thought I’d try to set the options.

And guess what ? My damn cluster works again 🙂

Reconfiguring NTP settings vCenter-wide

I recently started reinstalling all my ESX hosts, so I wrote up a short script that is reconfiguring all hosts and sets the NTP configuration according to my wish:

As you can see, the script takes the vCenter hostname and two NTP servers and basically applies it to each host in your vCenter environment.

Rename a Standard Port Group on all hosts in a cluster

Well, I recently decided to rename a bunch of my Standard Port Groups, since they did no longer reflect the network they were providing. Since I’m a lazy bastard (well lazy as in click lazy), I wrote this little PowerCLI script:

This script basically takes a vCenter instance and a single cluster, then creates a new Port Group on each host, after which it reconfigures all VMs possessing a virtual NIC with that Port Group and then deletes the old Port Group.

Emptying a VMFS datastore with PowerCLI

Well, once again I hacked at the Powershell/PowerCLI the other day. Since we don’t yet have a Enterprise Plus license at work (which would support Datastore Maintaince and Storage DRS), I needed a way to empty one datastore and move all the content into another one, while enabling Thin-Provisioning.

So I googled for a bit, and actually found a few hints … So without further yada-yada, here’s the script I came up with:

Initially I have something different, which was a bit shorter (based on Brian‘s work), but it had a huge downside: If you have vRDMs (like I do), it converts every damn vRDM into a VMDK, so make sure you only run this script on a datastore on VMs that either have only pRDMs or VMs with only VMDKs.

Rebooting a virtual machine via Task scheduler

Since the Scheduled Tasks in vCenter ain’t exportable, I went ahead and wrote a rather simple script, which lets me do this in Windows own Task Scheduler. What this script does, is initiate a graceful shutdown and if the VM isn’t shutdown within 60 seconds (12 * 5 seconds) it simply powers the VM off and immediately after that powers it back on.

Before this implementation in PowerCLI, I needed three tasks for each VM that was to be scheduled. And when migrating vCenters (and I usually do an empty install) vCenter’s scheduled tasks are not exportable, thus you need to re-create the tasks on the new vCenter by yourself again, which for more than four virtual machines is really a pain in the ass …

Update: well, found an error that caused shit not to work … Basically Stop-VM also needs the object for $VMname, otherwise the whole point of waiting for the VM to be stopped is kinda moot (seeing as Stop-VM never stops obsessing about not having a Get-VM object or a VM name to work with).

Reoccurring memory limits in vCenter

We recently had, after we migrated from vSphere 4 to vSphere 5, a memory limit in size of the configured memory on each and every VM. Since memory limits on VM level pretty much destroy performance, I went ahead an wrote this simple script to remove all memory limits on all VMs that don’t have “Unlimited” configured:

This script is basically what the guy over at get-admin.com did, just only for memory limits.