Powercli and UCS: Report which FI/blade type your hosts are contained in.

In smaller environments this script may not be that great, but what if you have 20 pairs of Fabric Interconnects and 1000 hosts?  Don’t have a good CMDB or are trying to track down which FI your hosts are in to troubleshoot?

Expanding on my last post we will create a new VI property and also dig into the CDP (Cisco Discovery Protocol) to determin which FI your hosts is connected to along with what VCenter.

As part of the script you will need to connect to any and all VCenters with Powercli then run the script below.  It will output: hostname, cluster, version, vcenter build, blade type, and FI name.

New-VIProperty -Name vCenterServer -ObjectType VMHost -Value {$Args[0].Uid.Split(“:”)[0].Split(“@”)[1]} -Force | Out-Null
$date = (get-date).tostring('M-d-y')
Get-VMHost | %{
        $vmhostview = Get-View $_
        $networksystem = Get-view $vmhostview.ConfigManager.NetworkSystem
        $FI = $networksystem.QueryNetworkHint($networksystem.pnic).connectedSwitchPort.systemname[1].split("-")[0] 
        $_ | select Name,Version,Build,Parent,Model,@{Name="Vcenter";Expression={($_ | select vcenterserver).vcenterserver}},@{Name="FI";Expression={$FI}}
        } | Export-Csv -NoTypeInformation master_hostlist-$date.csv

Output will look as follows:



PowerCLI: Host and VM location script across many Vcenters

If a company has a very large environment with many vcenters it sometimes can be difficult to track down where a host my be located.  Then let’s say you need to know what VM’s and what hosts are running on top of it? For this script you do need a list of hosts that you want to find.  If you want to just find out all the information you could modify the script to make the hostlist to just be get-vmhost instead of from a file.

I wrote a small script that once connected to all your vcenters, you can pass a csv with a list of hostnames and it will pull the VM’s, their host, and which VC it is in back out into a CSV.  In the script we are using the VIProperty cmdlet which lets you make your own property for objects in vmware.

The CSV will need to have the header “hostname” and then the FQDN of the hosts below it.

#Ready host file name in
$hostlist = Read-Host "Enter server list name"
$hostlist = Import-Csv $hostlist

# Create Vcenter Location Property
New-VIProperty -Name vCenterServer -ObjectType VMHost -Value {$Args[0].Uid.Split(“:”)[0].Split(“@”)[1]} -Force | Out-Null

#Loop through the hostlist and create spreadsheet.  Write for any hosts that aren't found.
$hostlist | %{
    Write-Host "Starting" $_.hostname
    if (Get-VMHost $_.hostname -ErrorAction SilentlyContinue ) { 
    $vmhost = get-vmhost $_.hostname
    $vmhost| get-vm | select Name,VMHost,@{Name="Vcenter";Expression={($vmhost | select vcenterserver).vcenterserver}}
        Write-host "Failed to find" $_.hostname 
} | Export-Csv -NoTypeInformation vm_list.csv