Showing posts with label PowerCLI. Show all posts
Showing posts with label PowerCLI. Show all posts

Tuesday, January 15, 2013

Writing PowerCLI for Performance

I was building a piece of PowerCLU code to check on my vCD Deployed vAPPs for certain errors and because it’s me I was curious what performance difference there was between using array based operations verses piped operations in powershell. Here’s what I found out.

Here is the array based operation – time to completion 2 minutes 30 seconds.

$myVAPP = @(Get-CIVApp | Select-Object Org, Name, Status)
        foreach ($myVAPP.Name in $myVAPP)
            {
                IF ($myVAPP.Status -eq "FailedCreation")
                    {
                        Write-Host 'vAPP' $myVAPP.Name' - Status is' $myVAPP.Status
                    }
                ELSE
                    {
                            IF ($myVAPP.Status -eq "Unresolved")
                            {
                                Write-Host 'vAPP' $myVAPP.Name' - Status is' $myVAPP.Status
                            }
                            else
                            {
                                IF ($myVAPP.Status -eq "Unknown")
                                {
                                    Write-Host 'vAPP' $myVAPP.Name' - Status is' $myVAPP.Status
                                }
                                else
                                {
                                }
                            }                           
                    }
            }
           

Here is the pipe based operation which is recommended for reasons that I now can testify for first hand.

Get-CIVApp -Status "Unresolved", "FailedCreation", "Unknown", "Unrecognized" | Select-Object Org, Name, Status

This operation returned the same results as the array based operation in 14 seconds, that’s 9% of the original time of using an array…

Friday, February 17, 2012

How to find where a VM is hosted

A fairly common problem is if you know a machine is virtual but you don't know where it is hosted. A perfect example is if you get notified that a machine in your environment has a virus and needs powered off but nobody knows where the machine is hosted. This gets even more interesting when you have nested virtual ESX hosts with guests running under them. With a quick google search I found 2 PowerCLI scripts that got close to what I wanted and then tweaked them to make this process very easy.

1. Script #1 is a modified version of the script located on VMware's website on information about the CDP network information: http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1007069 and allows you to find what host is tied to a specific CDP enabled switchport. Change the port in bold and cut and paste into PowerCLI

------ Start Script -----
Get-VMHost Where-Object {$_.State -eq "Connected"}
%{Get-View $_.ID}
%{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem}
%{ foreach($physnic in $_.NetworkInfo.Pnic){
$pnicInfo = $_.QueryNetworkHint($physnic.Device)
foreach($hint in $pnicInfo)
{

if( $hint.ConnectedSwitchPort.PortID -eq "GigabitEthernet3/13" )
{
Write-Host We have a match... $esxname is connected to remote switch port $hint.ConnectedSwitchPort.PortID on $physnic.Device -fore green
$hint.ConnectedSwitchPort
}
else
{
Write-Host "Not guilty..."; Write-Host
}
}
}
}
----- End Script ----

Script #2 is a very slightly modified script that I got from one of the comments at http://www.virtu-al.net/2009/07/07/powercli-more-one-liner-power/ and modified ever so slightly to show current power state.

------ Start Script -----
Get-VM where { (Get-NetworkAdapter $_).MacAddress -eq "00:50:56:86:01:f1" } Format-Table Name,Host, PowerState
----- End Script ----

Nothing terribly original but hopefully it will help somebody else.