Posts Tagged ‘ PowerShell

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: Reset CD-drives using PowerShell

As most of you know and probably experienced from time to time: when a Virtual Machine’s CD-drive is connected to an ISO-file on one of your Datastores or even connected to the physical drive of your ESX-host the migration due VMotion of a Virtual Machine will not work.

Normally this isn’t really a problem except if you put a ESX-host in Maintenance mode and Virtual Center will simply not tell you why the Maintenance mode process is hanging or even giving a time-out after 15 minutes for no obvious reason. Most of the times it’s a Virtual Machine which has a CD-drive connected to an ISO file. A waste of time if you ask me.

So to prevent this from happening I’ve written a simple PowerShell oneliner/script disconnect these CD-drives from the ISO-files or from the physical drives and set them to Client-drives which is ok for VMotion:

(Get-VM -Location :( Get-VMHost "your.esx.host")) | `
ForEach ( $_ ) { Get-CDDrive $_ | `
Where { $_.IsoPath.Length -gt 0 -OR $_.HostDevice.Length -gt 0 } | `
Set-CDDrive -NoMedia -Confirm:$False }

Instead of executing this just on one host you can also execute this for your entire cluster:

(Get-VM -Location :( Get-Cluster "Your Cluster Name")) | `
ForEach ( $_ ) { Get-CDDrive $_ | `
Where { $_.IsoPath.Length -gt 0 -OR $_.HostDevice.Length -gt 0 } | `
Set-CDDrive -NoMedia -Confirm:$False }

Or ofcourse, by Datacenter:

(Get-VM -Location :( Get-Datacenter "Your Datacenter Name")) | `
ForEach ( $_ ) { Get-CDDrive $_ | `
Where { $_.IsoPath.Length -gt 0 -OR $_.HostDevice.Length -gt 0 } | `
Set-CDDrive -NoMedia -Confirm:$False }

It’s as easy as that, now there will be no Virtual Machine interrupting your VMotions anymore and you can put your ESX hosts in maintenance mode without any problems ;)

Cheers!

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

Refresh Datastores with PowerShell (oneliner)

As I mentioned in the previous article about Refreshing Datastores I would see if I could make a oneliner for this action and I succeed in doing that.

This is what the line should be:

Get-Datacenter | Get-Cluster -Name "ClusterName" | Get-VMHost |
ForEach { $_ } { Get-VMHostStorage -VMHost:$_ -Refresh }

The result on the PowerCLI would probably look like this:

image

The ‘Recent Tasks’ window in the vSphere client will show you a line with “Refresh Host Storage System” for each server.

Thanks for Arne Fokkema for helping me out with the start of the oneliner ;)

Cheers!

NOTE: Get-Datacenter isn’t really required so the oneliner can actually be shorter ;)

Refresh Datastores with PowerShell

When using NFS for your datastores on your SAN (eg. NetApp) and you resize your datastore on the fly it always takes a while before the ESX/vSphere servers refresh the datastore so you can see the new size of the volume.

This can be annoying when you want to create a VM on one of these volumes and ESX still thinks the volume is too small because it hasn’t refreshed it’s datastores yet. Normally I would pick one ESX server where I want to put the VM on for starters and refresh the NFS volume which I want to use:

image

Ofcourse I wanted to make it easier for myself so I wrote a little PowerShell script to refresh the datastores on all ESX Servers in our Cluster. I think it’s also possible to make a oneliner out of this and I will try to create one and if I succeed I’ll post it again ;)

$vcHost = "virtual.center.host"
$vcClusterName = "Your ClusterName"
$vcUser = "username"
$vcPass = "password"

$vcServer = Connect-VIServer -Server:$vcHost -User:$vcUser -Password:$vcPass -WarningAction:SilentlyContinue

$vmHosts = Get-Datacenter | Get-Cluster -Name:$vcClusterName | Get-VMHost | Sort

ForEach ($vmHost in $vmHosts) {
    If (Get-VMHostStorage -VMHost:$vmHost -Refresh -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue) {
        Write-Host $vmHost.Name," host storage information refreshed"
    } else {
        Write-Host $vmHost.Name," host storage refresh failed!"
    }
}

Disconnect-VIServer -Server:$vcServer -Confirm:$false

 

After the script has been running the datastores on all your ESX-servers in the cluster will be refreshed so all resizes  of your NFS volumes on your SAN will be visible and “active”.

PowerGUI Script Editor – Empty Your Runspace

Here’s a useful tip for anyone who enjoys the handy debugger in the PowerGUI Script Editor during their intensive PowerShell scripting activities ;)

Variables

The most useful feature while debugging is the “Variables” window in the editor.

It allows you to see all the variables/objects that you declared and look inside them to see what data they contain.

 

 

 

The only problem is if you work in a script, making a lot of changes in the process, the Variables window will keep on filling up with all the variables that are used in the scripts even though some variables are not even used anymore.

This causes a mess in the Variables window so you kind of lose track of the things that actually matter and are worth keeping an eye on. So, I went looking in the Options (menu Tools > Options) and I found an option that resets the debug/variables windows (Runspace) every time you start a new debugging session. 
 
imageSimply select the “Reset PowerShell runspace each time debugging is started.” option, press OK and you will have a nice and tidy runspace (variables/debug window) from now on.

List NFS Datastore Usage

Some time ago someone we agreed on putting a minimum of 15 Virtual Machines on each NFS-volume on our NetApp storage. Now, almost a year later we have about 14 NFS volumes and the spreading of the VM’s on the Volumes have become a bit out of the ordinary. Mostly because we didn’t have an easy overview of how many VM’s there are present on each volume or because VM’s needed to be created on a short notice.

Since I’ve been busy with PowerShell I decided to create a nice script/utility to create this overview and to increase our insight in our NFS volumes. At first I made a console script, but soon -while I was discovering the posibilities of PowerShell- I created a GUI for it using PrimalForms.

List NFS Store Usage - Example Output

List NFS Store Usage - Example Output

The source is no rocket science, but I just found the utility very handy/helpful to monitor our storage environment for VMware. If you intend to use this sourcecode, don’t forget to adjust the VirtualCenter hostname and access credentials. Also, the names of our DataStores are all start with “VMWare_NFSxx” and I have a condition running to check if the DataStore name starts with that. You might want to adjust that to your datastore name convention.

Read more