How to gather vCenter Inventory, Number of VMs, ESXi Hosts and VCSA Node ID Using PowerCLI – VirtuallyThatGuy

I am the biggest fan of using CSV for automating my VMware estate but wanted to try the HTML flavour for a change. Join me explore the possibilities of collecting these results in an html readable format.

#Script for collecting VM details and exporting them to HTML report.

$vCenterServers = @()

#Set vCenter Servers Variables.
$vCenterServers = "uk3p-vc01.lab.local", "ntcp-vc01.lab.local", "hdcp-vc01.lab.local"

$a = "<style>"
$a = $a + "BODY{background-color:peachpuff;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;width: 100%}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:palegoldenrod}"
$a = $a + "</style>"

foreach ($vCenterServer in $vCenterServers)
{
#Connect to the vCenters Specified above
Connect-VIServer $vCenterServer

#Using .count to count the entities
$total_hosts = (Get-VMHost).count
$total_vms = (get-vm).count
$file_name = Get-Date -UFormat "C:\Scripts\$vCenterServer/%B/%Y-%b-%d @ %I-%M%p.html"

Get-VM |
select Name,Host,@{N="Cluster";E={@($_.host.parent.name)}}, PowerState, @{N="IP Address";E={@($_.guest.IPAddress[0])}} |
Sort Name | ConvertTo-Html -Head $a -Body "
<h2>No of Hosts : $total_hosts -+- No of VMs : $total_vms</h2>
" |
Out-File -FilePath $file_name
Disconnect-VIServer $vCenterServer -Force -Confirm:$false
}
## Bonus retrieving vCenter Details

$vCenterServers = "uk3p-vc01.lab.local", "ntcp-vc01.lab.local", "hdcp-vc01.lab.local"
Connect-VIServer $vCenterServer

### Get vCenter ID

$si = Get-View ServiceInstance
$setting = Get-View $si.Content.Setting
$setting.QueryOptions("instance.id") | Select -ExpandProperty Value 

disconnect-VIServer $vCenterServer 

#Script for collecting VM details and exporting​​ them​​ to HTML report.

 

$vCenterServers​​ =​​ @()

 

#Set vCenter Servers Variables.

$vCenterServers​​ =​​ "uk3p-vc01.lab.local",​​ "ntcp-vc01.lab.local",​​ "hdcp-vc01.lab.local"

 

$a​​ =​​ "<style>"

$a​​ =​​ $a​​ +​​ "BODY{background-color:peachpuff;}"

$a​​ =​​ $a​​ +​​ "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;width: 100%}"

$a​​ =​​ $a​​ +​​ "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}"

$a​​ =​​ $a​​ +​​ "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:palegoldenrod}"

$a​​ =​​ $a​​ +​​ "</style>"

 

foreach​​ ($vCenterServer​​ in​​ $vCenterServers)

{

#Connect to the vCenters Specified above

Connect-VIServer​​ $vCenterServer

 

#Using​​ .count to count the entities

$total_hosts​​ =​​ (Get-VMHost).count

$total_vms​​ =​​ (get-vm).count

$file_name​​ =​​ Get-Date​​ -UFormat​​ "C:\Scripts\$vCenterServer/%B/%Y-%b-%d @ %I-%M%p.html"

 

Get-VM​​ |

select​​ Name,Host,@{N="Cluster";E={@($_.host.parent.name)}},​​ PowerState,​​ @{N="IP Address";E={@($_.guest.IPAddress[0])}}​​ |

Sort​​ Name​​ |​​ ConvertTo-Html​​ -Head​​ $a​​ -Body​​ "

<h2>No of Hosts :​​ $total_hosts​​ -+- No of VMs :​​ $total_vms</h2>

"​​ |

Out-File​​ -FilePath​​ $file_name

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

}

## Bonus retrieving vCenter Details

 

$vCenterServers​​ =​​ "uk3p-vc01.lab.local",​​ "ntcp-vc01.lab.local",​​ "hdcp-vc01.lab.local"

Connect-VIServer​​ $vCenterServer

 

### Get vCenter ID

 

$si​​ =​​ Get-View​​ ServiceInstance

$setting​​ =​​ Get-View​​ $si.Content.Setting

$setting.QueryOptions("instance.id")​​ |​​ Select​​ -ExpandProperty​​ Value​​ 

 

disconnect-VIServer​​ $vCenterServer​​ 

 

You May Also Like

About the Author: VirtuallyThatGuy

Leave a Reply

Your email address will not be published. Required fields are marked *