Wednesday, March 26, 2014

ESXi Host inventory Script-PowerCli

# Setup array with hosts
$vcenters = @(
    "vcenter1.prod.local",
   "vcenter2.prod.local",
   "vcenter3.prod.local",
   "vcenter4.prod.local"
);

$table = New-Object system.Data.DataTable "Results"
$col1 = New-Object system.Data.DataColumn EncName,([string])
$col2 = New-Object system.Data.DataColumn Blade,([string])
#$col3 = New-Object system.Data.DataColumn Cluster,([string])
$table.columns.add($col1)
$table.columns.add($col2)
#$table.columns.add($col3)
# Store your U&P
$user = "your_admin"
$password = "YourSecret"

function GetInfo(){
foreach($vcenter in $vcenters) {
#lets check and see if we're already connected somewhere
disconnect-viserver -confirm:$false > $NULL 2>&1
# Connect
$SERVER = Connect-VIServer -Server $vcenter -User $user -Password $password > $NULL 2>&1
$ESX = @(get-vmhost| Select Name, ConnectionState, @{N="Cluster";E={Get-Cluster -VMHost $_}}, Version, $global:DefaultVIServer| Sort-Object Name)
foreach ($es in $esx)
{
$row = $table.NewRow()
  $row.EncName = ($es.Name).Remove(7,17)
  $row.Blade = ($es.Name).Substring(8,2)
  #$row.Cluster= (Get-Cluster -vm $vm.Name)
  $table.Rows.Add($row)
}
$table | Format-Table
$ESX | Format-Table -AutoSize
}
}
getinfo
disconnect-viserver -confirm:$false > $NULL 2>&1

No comments:

Post a Comment