Connect-VCenter & Set-Creds


Set-Creds
Connect-Vcenter

Here are 2 seemingly useless functions that I’ve loved for some time now. Both tasks that are achieved are effectively not that difficult individually, or frankly by them self, but when repeated multiple times become very tiresome!

Set-Creds

The first function, Set-Creds, is a basic wrapper for the pre-existing Get-Credential function already supplied by Microsoft. This command can easily be run, the data stored in an encrypted Variable (to obfuscate the data from prying eyes) and then passed on to various  other commands where the current user shell context (typically the currently logged in user/password) is not suitable.

Here is the function it self.


function Set-Creds {
    param(
    [parameter(Mandatory=$false)]$VariableName='creds',
    [parameter(Mandatory=$False)][switch]$ClearVariable=$false
    )
    IF ($ClearVariable -eq $false)
        {
            Set-Variable -Name $VariableName -Value (Get-Credential) -Scope global
        }
    ElseIf ($ClearVariable -eq $true)
        {
            Remove-Variable -Name $VariableName -Scope global
        }
    }

Here is the code in action:

Set-Creds

Connect-VCenter

Here is another seemingly easy task to achieve. Loading the VMWare PowerCLI commands and then connecting to what ever VCenter you have in mind. This is actually again, a very simple task, but IT CAN BE MADE SO MUCH EASIER. How you might ask? Yes, that’s right, another Function!!!!

WARNING – This is a little lazy this code. I’ve not tested this on machines running anything less than PowerCLI version 5 – Prior to that, I would need to write some try/catch kinda stuff – So, its not going to break anything, but you might get a few errors.

function Connect-Vcenter {
    param(
        [parameter(Mandatory=$true)]$vcenter,
        [parameter(mandatory=$false)]$Credentials=$null,
        [parameter(Mandatory=$false)][switch]$Multiple=$false
    )
    #Load snap-in
& "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"

    if ($Credentials -eq $null)
        {
            $credentials = Get-Credential
        }
    if ($Multiple -eq $true)
        {
            # Connect up - Allows for multiple vcenter connections :O
            Connect-VIServer $vCenter -Credential $Credentials
        }
    else
        {
            #Connect to vCenter - But not if you are already connected
            if ($global:DefaultVIServers.Count -lt 1) 
                {
                    Connect-VIServer $vCenter -Credential $Credentials
                }
                else
                {
                    Write-Host "Already connected to a vcenter server!!"
                }
    }
}

Here we show the code in use, much the same as before…

Connect-Vcenter

All together now!

Right, now here is where things start to make a little more sense. Lets set the scene. You’ve arrived onsite at customers site. You need to connect to their VCenter server and do some work and you are in a hurry, your username and password aren’t on the same domain as the customers. Easy, now we just use the Set-Creds and the Connect-VCenter commands from within the first Powershell you can get open:

finaloutcome

So, what have we achieved? We’ve managed to load the VMWare PowerCLI into a standard Powershell Shell, create a variable with a SECURELY stored password and connect to the vcenter server all in a few simple commands.

Plenty of uses for this moving forward, I’m sure you’ll see me reference them in the future as I build up my online arsenal of functions. Which leads me to another point. How will having these functions make life easier if I am constantly having to load them into a shell? I’ve plans to write up how I make my own custom Powershell module and also customizing your $Profile to ensure they are always in there! But that is for a later date…

VMWare CPU Over Prescription!

Recently I was asked to look into performance issues on a VMware cluster. The IT department was adamant that the issues they were seeing were caused by poor SAN (Storage area network) I/O (Input/output). Having worked in infrastructure for many years, but most recently embarking on a journey into the world of DevOps (Development Operations), I was quick to put my hand up to look into the issues as I felt I had plenty of value to add!

INITIAL ANALYSIS:

Normally in a smaller environment, or more to the point, a smaller business, I would do a simple calculation to see how over provisioned the CPU is. This simple and extremely quick & mainly unscientific calculation came about for 2 reasons

  • Labor cost
  • Time frame (or lack of)

I call this calculation “CPU Contention Ratio” – Or CCR as it will be known moving forward. CCR is a crude method used for a quick analysis of the over prescription of physical CPU resource to virtual CPU resource. EG: How much physical CPU resource is actually backing the virtual CPU resource

Calculating CCR:
$a = (number of host logical CPU cores within the cluster)
$b = (number of virtual cores assigned to running Virtual Machines within the cluster)
$a / $b = (CCR)

Or, here is a function I’ve written to give us exactly that. Requirements to run this script are as follows:

  • VMWare PowerCLI installed
  • VMWare PowerCLI modules loaded into the current shell session (I’ve written a function for just that found here)
  • A Username & Password to the VCenter with at least read-only rights to the cluster and VM’s

function Get-VMCCR {
    param (
    $Datacenter,
    $Cluster
    )
    #Get the VM Host specific info and store in a variable
    $VMHosts = Get-Datacenter -Name $Datacenter | Get-Cluster -Name $Cluster | Get-VMHost
    $VMs = Get-Datacenter -Name $Datacenter | Get-Cluster -Name $Cluster | Get-VMHost | Get-VM | where {$_.powerstate -eq 'PoweredOn'}

    # Initilizing a new array 
    $VMHostCPUCounts = @()

    # Loop through the hosts to see if they have HyperThreading on and calculate as required
    foreach ($VMHost in $VMHosts)
        {
            IF ($VMHost.HyperthreadingActive -eq 'True')
                {
                    $VMHostCPUCount = "" | Select 'Name','PhysicalCPUCount','TotalLogicalCPUCount'
                    $VMHostCPUCount.Name = $VMHost.Name
                    $VMHostCPUCount.PhysicalCPUCount = $VMHost.NumCPU
                    $VMHostCPUCount.TotalLogicalCPUCount = ($VMHost.NumCPU)*2
                    $VMHostCPUCounts += $VMHostCPUCount
                }
            Else
                {
                    $VMHostCPUCount = "" | Select 'Name','PhysicalCPUCount','TotalLogicalCPUCount'
                    $VMHostCPUCount.Name = $VMHost.Name
                    $VMHostCPUCount.PhysicalCPUCount = $VMHost.NumCPU
                    $VMHostCPUCount.TotalLogicalCPUCount = $VMHost.NumCPU
                    $VMHostCPUCounts += $VMHostCPUCount
                }
        }



    # Dump the output to the console
    $Outputs = @()
    $Output = "" | Select 'Datacenter','Cluster','TotalLogicalCPUCount','AssignedVCPU','CCR'
    $Output.Datacenter = $Datacenter
    $Output.Cluster = $Cluster
    $Output.TotalLogicalCPUCount = ($VMHostCPUCounts.TotalLogicalCPUCount | Measure-Object -sum).sum
    $Output.AssignedVCPU = ($VMs.NumCPU | Measure-Object -Sum).sum
    $output.CCR = "{0:N2}" -f ((($VMHostCPUCounts.TotalLogicalCPUCount| Measure-Object -Sum).sum/($VMs.NumCPU | Measure-Object -Sum).sum))
    $Outputs += $Output
    
    return $outputs

}

And after running this function, I am output with the following:


Datacenter Cluster    TotalLogicalCPUCount AssignedVCPU CCR 
---------- -------    -------------------- ------------ --- 
Region01   Production                  136          434 0.31

As you can see, I have 0.31 of a physical core backing each virtual CPU assigned to a virtual machine within the cluster. In my opinion, this is to little. For real time applications, no less than about 0.9 CCR is acceptable and for general application servers that require performance, but are not real time, no less than 0.75 CCR is acceptable.

BUT ALAS – THIS WASN”T ENOUGH

So, now more analysis is required, as IT want a more scientific analysis of the exact issue and also proof that the storage isn’t the bottle neck as they are suggesting – BUT, more on the in-depth analysis and specifically VMWare statistics collection using VMWare PowerCLI in my next post.