# This script will connect to each Virtual Centre listed in the array and query
# for a list of VM's, returning them all in an HTML table. This table can then be searched
# This will also include
$VISRVs = "VCNAME"
# Enter Labs credentials below
$UserLabs = User
$PassLabs = Pass
#Enter the Environment variables below, these will determine where the the output will end up
#this should go to the webroot folder so that it can be linked to.
$Envir = "Test"
$File = "testvmlocation.html"
$Fileloc = "D:\Webroot\VMLocation"
$Date = (get-date -uformat %m%d%Y%H%M)
$DateFile = (Get-Date -format "d MMM yyyy H:mm" )
#This archives the current file to the archive folder set below and adds the date on the end to keep the unique file.
Copy-Item $Fileloc\$File $Fileloc\Archive
Rename-Item $Fileloc\Archive\$File $File$Date.htm
#HTML formating is outlined below, we enter the header and the title then the formatting.
$body = "
List of Virtual Machines in the $Envir Environment - $Datefile
"
$title = "Virtual Machines $Envir"
$head = '
'
$postcont = ''
$tableout = ''
# The array below will connect to all of the Virtual Centers in the list
# and pull all data required for each VM
$Report = @()
Foreach ($VISRV in $VISRVs) {
if ($VISRV.contains($ProdDom))
{Connect-VIServer -Server $VISRV -user $UserProd -pass $PassProd}
else
{Connect-VIServer -Server $VISRV -user $UserLabs -pass $PassLabs}
$VMInfo = Get-VM | Select Name, `
@{N="DNS";E={$_.Guest.Hostname}}, `
NumCPU, MemoryMB, `
@{N="DiskGB";E={[Math]::Round((($_.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)}}, `
@{N="IP Address";E={$_.Guest.IPAddress[0]}}, `
@{N="State";E={$_.Powerstate}}, `
@{N="VM VC";E={$VISRV}},`
@{N="Cluster";E={Get-Cluster -VM $_}}, `
@{N="ESX Host";E={Get-VMHost -VM $_}}, `
@{N="Datastore";E={Get-Datastore -VM $_}}, `
@{N="VM URL";E={"$_"}}
$Report += $VMInfo
Disconnect-VIServer -Server $VISRV -Confirm:$false
}
($Report | Convertto-Html -head $head -body $body -title $title -PostContent $postcont) -replace '<', "<" -replace '>', ">" -replace '', $tableout | Out-File "$fileloc\$file"