VM Inventory as e-mail with hyperlinks to vCenter Web UI and VM console

This post is to publish the enhanced version of the script PowerCLI sccript for exporting VM details to HTML. This version has the following features added.

1. Report will be sent as e-mail in HTML format.
2. Added Hyperlink to the vCenter Web Client to login.
3. Each VM will have the Hyperlink to the corresponding VM Console.

The output that is produced by the script looks like this

get-vmInventory1

Notes:
1. Hyperlinks will work only on vCenters with vSphere version 5.1 or above.

2. The client integration plug-in has to be installed on your desktops to see remote console of the VM. The following VMware Article has the steps for installing plug-in.
http://pubs.vmware.com/vsphere-51/index.jsp?lang=en&single=true&topic=/com.vmware.vsphere.vcenterhost.doc/GUID-A618EF76-638A-49DA-991D-B93C5AC0E2B1.html

[crayon lang=”powershell”]
#.SYNOPSIS
#     PowerCLI Script for collecting the VM details and e-mail in HTML table format.
#.DESCRIPTION
#    This PowerCLI scritpt will help to get the VM inventory from multiple vCenter Servers. The script can be scheduled with the help of windows task scheduler to run it automatically everyday.
#.VERSION
#    v1.1
#.NOTES
#    File Name : Get-vmInventory.ps1
#    Author : Sreejesh Damodaran
#    Requires : PowerCLI 5.1 Release 2 or above, vCenter 5.1 or above
#    Documentation : Follow the following URL to install vSphere Client Integration plugin for your internet browser.
#    http://pubs.vmware.com/vsphere-51/index.jsp?lang=en&single=true&topic=/com.vmware.vsphere.vcenterhost.doc/GUID-A618EF76-638A-49DA-991D-B93C5AC0E2B1.html
#.LINK
#    This script posted to: http://www.pingforinfo.com
#.EXAMPLE
#    .\Get-vmInventory.ps1

#####################################
# VMware VirtualCenter server names #
#####################################
$vCenterServers = @()
$vCenterServers = “vcenter1.test.com”,”vcenter2.test.com”,”vcenter3.test.com”,”vcenter4.test.com”,”vcenter5.test.com”

##################
# Mail variables#
##################
$body = @()
$smtpserver = “relay.test.com”
$from = “[email protected]
$to = “[email protected]
$subject = Get-Date -UFormat “VM Inventory generated on %d-%b-%Y %I:%M%p”

########################
# Format of HTML table #
########################
$a = ”

Set-PowerCLIConfiguration -DefaultVIServerMode single -Confirm:$false

foreach ($vCenterServer in $vCenterServers)
{
$UUID = ((Connect-VIServer $vCenterServer).InstanceUUID).ToUpper()

$total_hosts = (Get-VMHost).count
$total_vms = (get-vm).count

$body += Get-VM | select @{N=”vCenter”;E={((“” +
$vCenterServer + “
“}},@{N=”Cluster”;E={@($_.host.parent.name)}},Host,@{N=”VM”;E={((“” + $_.Name +
“}}, PowerState, @{N=”IP Address”;E={@($_.guest.IPAddress[0])}} | Sort VM |
ConvertTo-Html -Head $a -Body ”

vCenter : $vCenterServer, No of Hosts : $total_hosts, No of VMs : $total_vms

” |
%{$tmp = $_ -replace “<“,”<“; $tmp -replace “>”,”>”;}| Out-String

Disconnect-VIServer $vCenterServer -Force -Confirm:$false
}

$finalBody = [string]$body
Send-MailMessage -smtpserver $smtpserver -from $from -to $to -subject $subject -body $finalBody -bodyashtml
$body.Clear

[/crayon]

-SD