VMware Cloud Community

Help on some simple coding

I'm wondering if someone could help me with a bit of code. I've hacked this script together to produce information on each of 8 VC instances we have globally. The report info is simple for an executive who wants it as a weekly report, not for anyone who wants alot of detail.

The script goes and connects to each VC, which all allow the same credentials, and then writes the output. The issue is that it is producing the same numbers for all the VC's. Am I not doing the "foreach" statement right or ? Here's just part of it, not the whole thing as to not clog up the forum. Any help is appreciated.

$vc = @(










$report =@()

$cred = Get-Credential

$listVC = Connect-VIServer -Server $vc -Credential $cred

foreach ( $VC in $listVC ) {

Write-Output $VC.Name

Write-Output (Get-VMhost).count

Write-Output (Get-VM).count


0 Kudos
2 Replies

You need to pass $vc to the Server parameter of each toolkit cmdlet

(almost everyone has this param). You can pass the whole array and

you'll get one huge list of VMs or whatever, or you can do it in a

loop like you are doing in your example.

simple e.g.

$vc | foreach-object { (get-vm -server $vc).Count }


$signature = "Hal Rottenberg, MVP - Admin Frameworks"

$projects = @{ title = "Blog Author"; url = "http://halr9000.com" },

@{ title = "Co-host"; url = "http://powerscripting.net" },

@{ title = "Community Director"; url = "http://PowerShellCommunity.org" },

@{ title = "Psi Webmaster"; url = "http://psi-im.org" }

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos

You will need to add the VI Server on the cmdlets in the loop with the -Server parameter.

Something like

(Get-VMHost -Server $VC).count

Hal types faster than me Smiley Wink

Message was edited by: LucD

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

0 Kudos