I am trying to get a basic report of all the hosts that we have and the IPs of the hosts. I have been able to get everything that I need except the IP Address. I found when I run the $hostadaptera and b part of the script alone, I am able to get the IP address no problem. When I try to incorporate this part into my script below, it does not return the IP address but I do see that the variable has information in it. We are running an mixed environment of ESX 3.5 and ESXi 4.x. In this case some of the IPs are Service Console port Groups and some are VMKernel ports. That is why I am doing the comparison.
Here is the variable output when checking powergui's variables for $hostchoice:
{@{Name=vmk0; IP=172.17.1xxx.xxx; PortGroupName=Management Network}, @{Name=vmk1; IP=172.17.xxx.xxx; PortGroupName=Management Network}, @{Name=vmk0; IP=172.17.xxx.xxx; PortGroupName=Management Network}, @{Name=vmk1; IP=172.17.xxx.xxx; PortGroupName=Management Network}}
Here is the script Example:
foreach ($VCServer in $VCServers)
{
Connect-VIServer $VCServer
$clusters = Get-Cluster | Sort Name
ForEach ($cluster in $clusters)
{
$esxhosts = Get-VMHost -Location $cluster | Select-Object Name, Version, Build
ForEach ($esxhost in $esxhosts)
{
$hostadaptera = Get-VMHostNetworkAdapter -Console | where { $_.PortGroupName -eq "Service Console"} | Select-Object Name, IP, PortGroupName
$hostadapterb = Get-VMHostNetworkAdapter -VMKernel | where { $_.PortGroupName -eq "Management Network"} | Select-Object Name, IP, PortGroupName
If ($hostadaptera -eq "Service Console") {$hostchoice = $hostadpatera} else {$hostchoice = $hostadapterb}
$myObj = "" | Select-Object HostName, IPAddress, Cluster, Version, Build
$myObj.HostName = $esxhost.Name
$myObj.IPAddress = $hostchoice.IP
$myObj.Cluster = $cluster.Name
$myObj.Version = $esxhost.Version
$myObj.Build = $esxhost.Build
$myCol += $myObj
}
}
Disconnect-VIServer -Confirm:$false
}
Try it with this
$myCol = @() foreach ($VCServer in $VCServers) { Connect-VIServer $VCServer $clusters = Get-Cluster ITSDEV | Sort Name foreach ($cluster in $clusters) { $esxhosts = Get-VMHost -Location $cluster | Select-Object Name, Version, Build foreach ($esxhost in $esxhosts) { $hostadaptera = Get-VMHostNetworkAdapter -Console -VMHost $esxhost.Name | where { $_.PortGroupName -eq "Service Console"} | Select-Object Name, IP, PortGroupName $hostadapterb = Get-VMHostNetworkAdapter -VMKernel -VMHost $esxhost.Name | where { $_.PortGroupName -eq "Management Network"} | Select-Object Name, IP, PortGroupName if ($hostadaptera.PortGroupName -eq "Service Console") {$hostchoice = $hostadaptera} else {$hostchoice = $hostadapterb} $myObj = "" | Select-Object HostName, IPAddress, Cluster, Version, Build $myObj.HostName = $esxhost.Name $myObj.IPAddress = $hostchoice.IP $myObj.Cluster = $cluster.Name $myObj.Version = $esxhost.Version $myObj.Build = $esxhost.Build $myCol += $myObj } } Disconnect-VIServer -Confirm:$false }
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference