VMware PowerCLI Scripts: Delete all snapshots, Create new snapshots

I've started the process of automatically installing my Windows Server updates. Since it is going to be automatic I wanted a way to delete and create new snapshots before patch Tuesdays so I could recover easily if something goes wrong.

These scripts assume your working as an AD user that has proper access to the vcenter server.

Delete all snapshots:


add-pssnapin VMware.VimAutomation.Core
Connect-VIServer -Server vcenterserver
        foreach ($vm in get-vm | sort-object){
            $snaps = get-snapshot -vm $vm
            $vmname = $vm.name
                foreach ($snap in $snaps){
                $snapName = $snap.name
                    if ($snapname -ne $null){
                        #"  "
                        $strOut = "Found snapshot: $snapname on: $vmname"
                        $strOut | Out-Default
                        $snap | select-object *
                        # Rem the following line to perform just inventory, leave to get the remove prompt.
            remove-snapshot -snapshot $snap -confirm:$false
                    #else {"No snapshots found on $vmname"}
                    #"  "

Create new snapshots:


add-pssnapin VMware.VimAutomation.Core
Connect-VIServer -Server kohvc1-2k8r2
get-vm * | new-snapshot -name "Before Windows Updates"

I had some trouble coming up with a way to delete only a specific snapshot. As you see in the create script I am naming the snapshots "Before Windows Updates". It would be great if I could make a snapshot deletion script that only deleted those snapshots. The remove-snapshot command doesn't seem to have as many options as the create-snapshot command.


I decided to put all the servers I want to process into a folder in VMs and Templates, then query that folder with get-folder and get-vm. Here are the modified lines.

foreach ($vm in get-folder "Automatic Windows Updates" | get-vm | sort-object) {


get-folder "Automatic Windows Updates" | get-vm | new-snapshot -name "Before Windows Updates"

Exchange 2010 disconnected mailboxes

After moving accounts from a Exchange 2003 server to a new Exchange 2010 we discovered several disconnected mailboxes.

I believe these were disconnected because the mailbox was not properly removed from the 2003 server, and the mailbox cleanup agent wasn't ran to resolve the situation. Manually running the cleanup agent two times removes the mailboxes from Exchange 2003. Now we need to reconnect the disconnected mailboxes in Exchange 2010. This can be resolved from the Exchange Management Shell command line.

To list all mailboxes currently disconnected:

get-mailboxserver | get-mailboxstatistics | where { $_.DisconnectDate } | fl DisplayName, DisconnectDate

To reconnect mailboxes:

connect-mailbox -Database "mailbox database abcdefg" -Identify "Display, Name"

VMware View 4.5 Notes

Ran into a few issues so far with quick prep and Windows 2008R2 Active Directory. Quick prep would create the AD object for the VM, but the actual VM hadn't joined the domain or been renamed. It was essentially a clone of the master.

Windows XP master needs to have KB944043 installed.

VMware suggest enabling "Allow cryptography algorithms compatible with Windows NT 4.0" in the Default Domain Controller Policy.KB1028164

On the master before you shut it down to take your snapshot, remember to do a release and flush the DNS else it might be denied a DHCP lease when you build your pool or recompose.


Also I've been told the "Protect Object From Accidental Deletion" flag on Organizational Units can cause issues with recomposing.

« 1 2 3 4 5 6 7 8 9 10 11 12 »