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: 1 Get-VM | Get-Snapshot | Where { $_.Name -like "201502*" } | Format-Table -Property VM, Name, Created, Description, SizeMB -AutoSize Now, I could use another one-liner to delete all snapshots, that’ll look like this: ...

February 23, 2015 · 2 min · 231 words · christian

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. 1 2 3 4 5 6 7 $vmhosts = Get-Cluster "UCSCL01-02" | Get-VMHost foreach ($vmhost in $vmhosts) { import-csv -Delimiter ";" .\pg.csv | foreach { $vmhost | Get-VirtualSwitch -Name "vSwitch3" | New-VirtualPortGroup ` -Name $_.portgroup -VLanId $_.vlanid } } And the corresponding CSV would look like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 portgroup;vlanid 1700_routers;1700 1701_routers;1701 1702_routers;1702 1703_routers;1703 1704_routers;1704 1705_routers;1705 1706_routers;1706 1707_routers;1707 1708_routers;1708 1709_routers;1709 1710_routers;1710 1711_routers;1711 1712_routers;1712 1713_routers;1713 1714_routers;1714 1715_routers;1715 1716_routers;1716 1717_routers;1717 1718_routers;1718 1719_routers;1719 2000_virtual-machines;2000 2001_virtual-machines;2001 2002_virtual-machines;2002 2003_virtual-machines;2003 2004_virtual-machines;2004 2005_virtual-machines;2005 2006_virtual-machines;2006 2007_virtual-machines;2007 2008_virtual-machines;2008 2009_virtual-machines;2009 2010_virtual-machines;2010 2011_virtual-machines;2011 2012_virtual-machines;2012 2013_virtual-machines;2013 2014_virtual-machines;2014 2015_virtual-machines;2015 2016_virtual-machines;2016 2017_virtual-machines;2017 2018_virtual-machines;2018 2019_virtual-machines;2019

January 3, 2015 · 1 min · 160 words · christian

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: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 param( [string] $vcenter, [string] $cluster, [string] $oldpg, [string] $newpg, [string] $vlan ) # Add the VI-Snapin if it isn't loaded already if ( (Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin -Name "VMware.VimAutomation.Core" } If ( !($vcenter) -or !($cluster) -or !($oldpg) -or !($newpg) -or !($vlan) ) { Write-Host `n "pg-cluster-rename: <vcenter-server> <cluster> <oldpg> <newpg>" `n Write-Host "This script renames each port group with the name <oldpg> to <newpg>" `n Write-Host " <vcenter-server> - DNS name of your vCenter server." `n Write-Host " <cluster> - Display-Name of the vCenter cluster, on which we are" Write-Host " gonna create the new portgroup." `n Write-Host " <oldpg> - Name of the old Port Group that is to be replaced (ie VLAN2)." `n Write-Host " <newpg> - Name of the new Port Group (ie PG-VLAN2-Produktion)." `n Write-Host " <vlan> - VLAN-ID for of the new port group." `n exit 1 } Connect-VIServer -Server $vcenter Get-Cluster $cluster | Get-VMHost | Get-VirtualSwitch -Name $vswitch | ` New-VirtualPortGroup -Name $pg -VLanId $vlan Get-Cluster $cluster | Get-VM | Get-NetworkAdapter | Where { $_.NetworkName -eq "$oldpg" } | ` Set-NetworkAdapter -NetworkName $newpg -Confirm:$false Get-Cluster $cluster | Get-VMHost | %{Get-View (Get-View $_.ID).configmanager.networkSystem} | %{ $_.RemovePortGroup($oldpg) } Disconnect-VIServer -server $vcenter -Confirm:$false 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.

September 24, 2014 · 2 min · 321 words · christian

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: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 param( [string] $vCenter ) # Add the VI-Snapin if it isn't loaded already if ( (Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin -Name "VMware.VimAutomation.Core" } If ( !($vCenter) ) { Write-Host Write-Host "vcenter-remove-vsc-attributes: <vcenter-server>" Write-Host Write-Host " <vcenter-server> - DNS name of your vCenter server." Write-Host exit 1 } Connect-VIServer -Server $vCenter Remove-CustomAttribute -CustomAttribute "PnC.CustSpec", "PnC.Deployed", "PnC.GroupID", "PnC.Source" -Confirm:$false Disconnect-VIServer -server $vCenter -Confirm:$false

October 17, 2012 · 1 min · 142 words · christian

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. ...

April 30, 2012 · 3 min · 431 words · christian

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 ...

February 11, 2012 · 2 min · 299 words · christian

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: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 param( [string] $vcenter, [string] $ntpserver1, [string] $ntpserver2 ) # Add the VI-Snapin if it isn't loaded already if ( (Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin -Name "VMware.VimAutomation.Core" } If ( !($vcenter) -or !($ntpserver1) -or !($ntpserver2) ) { Write-Host `n "vcenter-ntp-reconfigure: <vcenter-server> <ntpserver1> <ntpserver2>" `n Write-Host "This script clears the NTP servers currently configured and" `n Write-Hsot "adds the ones supplied on the command line." `n Write-Host " <vcenter-server> - DNS name of your vCenter server." `n Write-Host " <ntpserver1> - NTP server #1" `n Write-Host " <ntpserver2> - NTP server #2" `n exit 1 } Connect-VIServer -Server $vcenter foreach ($esxhost in Get-VMHost) { Get-VMHost $esxhost | Remove-VMHostNtpServer -Confirm:$false -NtpServer (Get-VMHost $esxhost | ` Get-VMHostNtpServer) Get-VMHost $esxhost | Add-VMHostNtpServer -NtpServer $ntpserver1 Get-VMHost $esxhost | Add-VMHostNtpServer -NtpServer $ntpserver2 } Disconnect-VIServer -server $vcenter -Confirm:$false 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.

February 4, 2012 · 2 min · 219 words · christian

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: ...

January 26, 2012 · 2 min · 272 words · christian

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. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 param( [string] $vCenter, [string] $VMname ) # Add the VI-Snapin if it isn't loaded already if ( (Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin -Name "VMware.VimAutomation.Core" } If ( !($vCenter) -or !($VMname) ) { Write-Host Write-Host "vm-reboot: <vcenter-server> <VMname>" Write-Host Write-Host " <vcenter-server> - DNS name of your vCenter server." Write-Host " <VMname> - Display name of the VM in this vCenter server." Write-Host exit 1 } Connect-VIServer -Server $vCenter $VM = Get-VM $VMname # First, try shutting down the virtual machine gracefully Write-Host "Stopping VM $( $VM )" Write-Host " - Graceful shutdown" Shutdown-VMGuest -VM $VM -Confirm:$false $VM = Get-VM $VMname $i = 0 While ($VM.PowerState -ne "PoweredOff") { # If that doesn't work, break out the hammer and just kill it if ( $i -gt 12 ) { Write-Host " - Forced shutdown" Stop-VM -VM $VMname -Confirm:$false } Start-Sleep -Seconds 5 $VM = Get-VM $VMname $i++ } If ($VM.PowerState -eq "PoweredOff") { Write-Host "Starting VM $( $VM )" Start-VM -VM $VM -Confirm:$false >$NULL } Disconnect-VIServer -server $vCenter -Confirm:$false 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 … ...

January 16, 2012 · 2 min · 401 words · christian

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: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 param( [string] $vCenter ) # Add the VI-Snapin if it isn't loaded already if ( (Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin -Name "VMware.VimAutomation.Core" } If ( !($vCenter) ) { Write-Host Write-Host "cluster-remove-mem-limits: <vcenter-server>" Write-Host Write-Host " <vcenter-server> - DNS name of your vCenter server." Write-Host exit 1 } Connect-VIServer -Server $vCenter Get-VM | Get-VMResourceConfiguration | Where-Object { $_.memlimitmb -ne '-1' } |` Set-VMResourceConfiguration -memlimitmb $null Disconnect-VIServer -server $vCenter -Confirm:$false This script is basically what the guy over at get-admin.com did, just only for memory limits.

January 13, 2012 · 1 min · 170 words · christian