Archive for the ‘ PowerCLI ’ Category

PowerCLI: Unfreeze/Resume Your Virtual Machines

NOTE: I published this article a bit earlier, but due to the fact that this was a not-yet-released feature I removed this post once again. Since this feature is released for a long time already I deciced to re-publish it, enjoy!

Last night I got a message from our monitoring system that one of our NFS-volumes on our NetApp SAN was almost full but at this particular moment I was driving home and by the time I got home the NFS volume was completely full which resulted in the Virtual Machines freezing up.

After sizing up the NFS volume the machines do not automatically start to ‘unfreeze’ again, and they were waiting for me to answer the question in the vSphere client to “Retry” or “Abort”. Ofcourse I could’ve just answered “Retry” on each machine to get them going again, but I thought it would be nicer to solve this with the almighty PowerCLI since it were like 30 Virtual Machines.

Get-Cluster "Your Cluster" | Get-VM | Sort | Get-VMQuestion | `
Set-VMQuestion -Option "Retry" -Confirm:$false

This will result in answering all machines waiting to continue with the “Retry” option in your cluster (in this example I used “Your Cluster” as clustername).

I hope you won’t be needing it much, but if you do it will be handy ;)

PowerCLI: Migrate VM’s to another VLAN/Portgroup

Scenario:

Suppose you have several Virtual Machines running in a VLAN of which you decide they should be migrated to a new VLAN because of infrastructural changes in your network. Mind you this is only handy if the machines you are about to migrate use DHCP to get their network addresses.

Approach:

First of all you need to make sure all the ESX-servers in your cluster have the ability to use that VLAN so you need to have your your switchports/trunks tagged with it.

Secondly you need to add the new VLAN (portgroup) to the complete cluster, in this example I’m using vSwitch0 as your Virtual Switch name, make sure you change it into what applies to you in case if it’s different.

Get-Cluster "Your Cluster" | Get-VMHost | Get-VirtualSwitch -Name "vSwitch0"`
| New-VirtualPortGroup -Name "New VLAN" -VLanId 123

This will add the “New VLAN” to all of the ESX Servers in “Your Cluster” with VLAN ID 123 on vSwitch0.

Since all the ESX Server are provided with access to the new VLAN to place their Virtual Machines in it’s time to migrate them to the new VLAN. There are two ways you can do this:

The first is to configure all the network adapters of all VM’s from the old to the new VLAN/Portgroup and the second is to do exactly the same but to shortly disconnect/reconnect the network adapters of the Virtual Machines to initiate a DHCP request for your VM’s.

First method:
Get-Cluster "Your Cluster" | Get-VM | Get-NetworkAdapter | `
Where { $_.NetworkName -eq "Old VLAN" } | `
Set-NetworkAdapter -NetworkName "New VLAN"
Second method:
Get-Cluster "Your Cluster" | Get-VM | Get-NetworkAdapter | `
Where { $_.NetworkName -eq "Old VLAN" } | `
Set-NetworkAdapter -NetworkName "New VLAN" -Connected:$false | `
Set-NetworkAdapter -Connected:$True
Note:

Then again if you do use static addresses for your Virtual Machines you will need to configure them within the Operating Systems afterwards. Although that might be scriptable I haven’t found a way to do this.

Quick and simple VMware ESX Host Statistics

Just a small oneliner to display all the servers, their overall status, CPU and Memory usage in all your Datacenters (can be handy if you have multiple datacenters).

Get-Datacenter | Sort | Get-VMHost | Sort | Get-View |`
Select Name, OverallStatus, `
@{N="CPU Usage (GHz)";E={[math]::round(
$_.Summary.QuickStats.OverallCpuUsage/1024,2)}}, `
@{N="Memory Usage (GB)";E={[math]::round(
$_.Summary.QuickStats.OverallMemoryUsage/1024,2)}} 

And it will give you an output that looks like this:

image

You may not find it very useful like this, but you can also add a Where statement to this line to filter on several things. For example, you can decide you only want to see the servers that have yellow or red Overall Status due to high memory or CPU usage:

Get-Datacenter | Sort | Get-VMHost | Sort | Get-View | `
Select Name, OverallStatus, `
@{N="CPU Usage (GHz)";E={[math]::round(
$_.Summary.QuickStats.OverallCpuUsage/1024,2)}}, `
@{N="Memory Usage (GB)";E={[math]::round(
$_.Summary.QuickStats.OverallMemoryUsage/1024,2)}} | `
Where { $_.OverallStatus -ne "green" }

Which will give you something like this:

image

Ofcourse these onliners are cool and handy to use, but you can also use these oneliners to write a script around it to monitor your servers. I will post a script like that soon to show you different interpretations of this script.

List all of your snapshots (oneliners)

It’s not hard to create a PowerShell script to list your current snapshots, but of course it’s a nice challenge to create a oneliner that’ll take care of this.

I’ve created several oneliners with different output, for instance: this one is to show snapshots of your total VMware infrastructure:

Get-VM | Sort Name | Get-Snapshot | Where { $_.Name.Length -gt 0 } | Select
VM,Name,Description,Created

While in fact in the organisation I work for we have several locations throughout the country and several VMware ESX Servers running on each one which we’ve divided in different Datacenters in our VMware Infrastructure.

So for instance you can also use the following line to list all Snapshots in a specific Datacenter:

Get-Datacenter -Name "Your Datacenter" | Get-VM | Sort Name | Get-Snapshot | 
Where { $_.Name.Length -gt 0 } | Select VM,Name,Description,Created

Or just for one specific cluster:

Get-Cluster "Cluster Name" | Get-VM | Sort Name | Get-Snapshot | Where
{ $_.Name.Length -gt 0 } | Select VM,Name,Description,Created

All of these oneliners give somewhat the same output, which looks like this (company sensitive names are sensored, sorry ;) )

Snapshot listing

You can also add several other cmdlets to change the output of the result data, such as  ConvertTo-HTML or Out-GridView (if you use PowerShell 2.0 already).