RSS .92| RSS 2.0| ATOM 0.3
  • Home
  • About
  •  

    PowerCLI – Generate Count of Running VM’s

    September 8th, 2010

    If you are like me, you have virtual machines in different states in your virtual environment.  Running, paused, or powered-off.  If you have ever been asked “How many VM’s do we have,” and you know the right answer is technically not what vCenter lists as total VM’s, run the following PowerCLI script:

    $vcounter=0
    
    (get-vm )| %{
      $vm = $_
      if( get-vmguest -VM $vm.Name |where-object {$vm.State -eq "Running"}){
        $vcounter++
        }
      }
    
    echo $vcounter
    
    

    Let’s break down line 5, where all of the magic happens.  We are running get-vmguest on the current VM pulled from get-vm on line 3, and then determine if it’s state is “Running.”  PowerCLI and vCenter differ in how they display the state of a VM: “Running” or “Not-Running” in PowerCLI vs “Powered On” or “Powered Off” in vCenter.

    Voila, you now have the count of virtual machines that are Powered-On and theoretically doing work.


    PowerCLI – Gather Virtual Machine Network Information

    August 24th, 2010

    Mapping the virtual networking can be troublesome at times.  How often are you asked to provide MAC addresses to determine what switch port a Virtual Machine is using?  This quick PowerCLI script will walk through and display the VM name, Port Group(s), IP(s) and MAC Address.

    (get-vm) | %{
      $vm = $_
      echo $vm.name----
      $vm.Guest.Nics | %{
        $vminfo = $_
        echo $vminfo.NetworkName $vminfo.IPAddress $vminfo.MacAddress
    
      }
    }
    

    In this example, %{ } will conduct a foreach on each virtual machine from get-vm, and then a foreach on each virtual network adapter assigned to the VM.


    PowerCLI – Physical and Virtual CPU statistics

    June 29th, 2010

    I was recently tasked with providing information regarding CPU averages across our production environment, both physical and virtual.  I know there are other PowerCLI scripts floating around, but it was a good way for me to learn more PowerShell.

    Measure-object and getting the average to show two digits was one of the harder problems to solve (along with get-vmhost $_.NumCPU not working correctly with the version of PowerCLI I had installed).  Lines 29, 31, 49 and 51 are highlighted to show the snip-its.

    It will grab the CPU stats between the days you specify (lines 13,14) from your Virtual Center server, at line 10.  It will spit out a count of your physical hosts, CPUs, Virtual Machines and vCPUs.

    
    # Add in the VI Toolkit goodness
    if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
    {
     Add-PSsnapin VMware.VimAutomation.Core
    }
    
    # Connect to the vCenter server(s)
    $vcserver= @()
    
    $vcserver += connect-VIServer "_Your VC Server_"
    
    # define our start and finish days
    $startdate=(get-date).adddays(-7)
    $finishdate=(get-date).adddays(-1)
    
    # define our silly little counters
    $pCounter = 0
    $vCounter = 0
    $pCPU=0
    $vCPU=0
    
     # print stat dates
     write-host `n --- Statistics for $startdate through $finishdate --- `n
    
     # ---- grab stats for Physical ----
     foreach ($esx_server in (get-vmhost))
     {
     # grab the cpu.usage.average, days are configurable
     $pStats = (get-stat -entity $esx_server -stat cpu.usage.average -Start $startdate -Finish $finishdate | measure-object -property value -average)
     # we only want two decimal places
     $pAvg = [system.math]::round($pStats.average,2)
    
     write-host Physical: $esx_server.name CPU-Average: $pAvg
    
     # increment physical counter
     $pCounter++
    
     # add up the total number of pCPU in cluster
     $pCPU += $esx_server.NumCPU
     }
    
     write-host `nPhysical Host Count: $pCounter
     write-host Physical CPUs: $pCPU `n
    
     # ---- grab stats for Virtual ----
     foreach ($vm_server in (get-vm))
     {
     #grab the cpu.usage.average, days are configurable
     $vStats = (get-stat -entity $vm_server -stat cpu.usage.average -Start $startdate -Finish $finishdate | measure-object -property value -average)
     # we only want two decimal places
     $vAvg = [system.math]::round($vStats.average,2)
    
     write-host Virtual: $vm_server.name CPU-Average: $vAvg
    
     # increment virtual cointer
     $vCounter++
    
     # add up total number of vCPU in cluster
     $vCPU += $vm_server.NumCPU
     }
    
     write-host `nVirtual Host Count: $vCounter
     write-host Virtual CPUs: $vCPU `n
    
    # Disconnect from the vCenter server(s)
    disconnect-viserver $vcserver -Confirm:$False
    
    # END
    ###
    
    

    PowerCLI – Get VMware-Tools versions

    June 22nd, 2010

    Imagine this:

    You are sitting in your cube on a Tuesday morning and your boss walks in and says: “I need all of the installed versions of VMware-Tools to report for compliance!”  Your initially reaction is probably: “really?”  And then you say, OK, I can do that.

    And here is how to do it:

    
    # Add in the VI Toolkit goodness
    if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
    {
    Add-PSsnapin VMware.VimAutomation.Core
    }
    
    # Connect to the vCenter server(s)
    $vcserver= @()
    
    $vcserver += connect-VIServer "<Your vCenter Server>"
    
    # get the vmware tools version for each VM
    
    get-vm |% { get-view $_.id } | select Name, @{ Name="ToolsVersion"; Expression={$_.config.tools.toolsVersion}}
    
    # Disconnect from the vCenter server(s)
    disconnect-viserver $vcserver -Confirm:$False
    
    # END
    ###
    
    

     

    Your output will be of the form:

    server1.example.com   200000

    server2.example.com   0

    etc.

    Update (3/25/2011): My colleagues found a post on the VMTN page that has to do with my script.  LucD had another idea on how to run the script that would generate so much load on vCenter.  Check it out here.


    PowerCLI Part I

    June 21st, 2010

    Microsoft did a great job with PowerShell.  I take it as 1 part BASH, 2 parts PERL, and probably a few dashes of VB (which I know no dashes).

    VMware blended up their own cmdlets for system automation and reporting.  The useage for the most part is straight-forward, but I have found a few oddities.

    For a few months, I was getting all kids of requests for information from the Management.  Off the top of my head, I can think of:

    • VMware-tools versions installed across the board
    • VM counts across clusters
    • Show VM to LUN mappings

    I leaned heavily on posts by LucD, Virtu-Al, and others, so I’ll post some things I came up with.

    Part I

    I always load in the VI Toolkit PSSnapin at the beginning of every PS1, like so:

    # Add in the VI Toolkit goodness
    if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
    {
    Add-PSsnapin VMware.VimAutomation.Core
    }