06 December 2014

Get VMs' Current EVC Mode via PowerCLI

EVC Mode
Need to report on what is the current EVC mode in which some VMs are running?  We recently did, and out popped a bit of PowerShell/PowerCLI code to do it, of course. As part of some migrations and retirements, we were moving VMs from clusters at one EVC mode (lower) to clusters with higher- or no EVC mode, but were not scheduling power-cycles for the VMs at migration time, so as to keep smaller the scope of work for that event.  Rather than have to keep track of "here are the X number of VMs that are at EVC mode [low]", which would need power-cycled at some later date to bring them up to the EVC mode of their new cluster, PowerCLI is there for us.
It is pretty straightforward, but is handy for identifying those VMs that are, say, at a different EVC mode than is configured on the cluster.  So, the code:
function Get-VMEVCMode {
    <#  .Description
        Code to get VMs' EVC mode and that of the cluster in which the VMs reside.  May 2014, vNugglets.com
        .Example
        Get-VMEVCMode -Cluster myCluster | ?{$_.VMEVCMode -ne $_.ClusterEVCMode}
        Get all VMs in given clusters and return, for each, an object with the VM's- and its cluster's EVC mode, if any
        .Outputs
        PSCustomObject
    #>
    param(
        ## Cluster name pattern (regex) to use for getting the clusters whose VMs to get
        [string]$Cluster_str = ".+"
    )

    process {
        ## get the matching cluster View objects
        Get-View -ViewType ClusterComputeResource -Property Name,Summary -Filter @{"Name" = $Cluster_str} | Foreach-Object {
            $viewThisCluster = $_
            ## get the VMs Views in this cluster
            Get-View -ViewType VirtualMachine -Property Name,Runtime.PowerState,Summary.Runtime.MinRequiredEVCModeKey -SearchRoot $viewThisCluster.MoRef | Foreach-Object {
                ## create new PSObject with some nice info
                New-Object -Type PSObject -Property ([ordered]@{
                    Name = $_.Name
                    PowerState = $_.Runtime.PowerState
                    VMEVCMode = $_.Summary.Runtime.MinRequiredEVCModeKey
                    ClusterEVCMode = $viewThisCluster.Summary.CurrentEVCModeKey
                    ClusterName = $viewThisCluster.Name
                })
            } ## end foreach-object
        } ## end foreach-object
    } ## end process
} ## end function

And, some examples: Get EVC mode info for all of the VMs in this cluster:
PS vN:\> Get-VMEVCMode -Cluster myCluster
Name                   PowerState   VMEVCMode        ClusterEVCMode   ClusterName
----                   ----------   ---------        --------------   -----------
somesvr01.dom.com       poweredOn   intel-westmere   intel-westmere   OurCluster
somesvrlm1.dom.com      poweredOn   intel-penryn     intel-westmere   OurCluster
somesvrbs03.dom.com    poweredOff                    intel-westmere   OurCluster
somesvrxp.dom.com       poweredOn   intel-penryn     intel-westmere   OurCluster
...


For all of the VMs in this cluster where their EVC mode does not match the config'd cluster EVC mode, return their info:
PS vN:\> Get-VMEVCMode -Cluster myCluster | ?{($_.VMEVCMode -ne $_.ClusterEVCMode) -and ($_.PowerState -eq "poweredOn")}
Name                   PowerState   VMEVCMode      ClusterEVCMode   ClusterName
----                   ----------   ---------      --------------   -----------
somesvrlm1.dom.com      poweredOn   intel-penryn   intel-westmere   OurCluster
somesvrp83.dom.com      poweredOn   intel-penryn   intel-westmere   OurCluster
somesvra.dom.com        poweredOn   intel-merom    intel-westmere   OurCluster
...


Note:  The function does expect that you are using at least PowerShell v3, which surely everyone is well beyond by now, anyway.  But, if not, the "[ordered]" cast should be the only thing one would need to take out in order to use in older PowerShell versions.

Straightforward, and uses everyone's favorite PowerCLI cmdlet, Get-View, so you know that it is FaF!  Enjoy.

No comments:

Post a Comment