VMware Cloud Community
VJ_VMware_111
Enthusiast
Enthusiast
Jump to solution

VMware PowerCLI script Error. Urgent help needed!

Hello VSphere Powercli Experts,

Can some one please tell me why ESX hosts from vcenter1 is appearing on all 4 vcenters in the output? Thanks a lot in advance!

$Username = Read-Host -Prompt "Please enter your Username:"

$Password = Read-Host -Prompt "Please enter your Password:"

$vcenters = @("vcenter1","vcenter2","vcenter3","vcenter4")

$HostReport = @()

ForEach ($vcenter in $vcenters)

{

Connect-VIServer $vcenter -User $Username -Password $Password

Get-VMHost |Get-View |%{

     $HReport = "" | select vCenter, Hostname, ESX_Version

     $HReport.vCenter = $vcenter

  $HReport.Hostname = $_.Name

  $HReport.ESX_Version = $_.config.product.version

  $HostReport += $HReport

}

Disconnect-VIServer $vcenter -Confirm:$false

}

$HostReport | Export-Csv "C:\ESXHostReport.csv" -NoTypeInformation

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I suspect you might be connected to multiple vCenters.

Check what is in $global:defaultviservers.

You can limit your Get-VMHost to a specific vCenter by adding the Server parameter.

Get-VMHost -Server $vcenter


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

I suspect you might be connected to multiple vCenters.

Check what is in $global:defaultviservers.

You can limit your Get-VMHost to a specific vCenter by adding the Server parameter.

Get-VMHost -Server $vcenter


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
VJ_VMware_111
Enthusiast
Enthusiast
Jump to solution

I can see vcenter1 when i run $global:defaultviservers.

I added Server parameter for Get-VMHost, and it fixed the issue. Thanks a lot!

Reply
0 Kudos