VMware Cloud Community
kevinjj
Contributor
Contributor
Jump to solution

multi vcenter report

In a multi vCenter environment, how to relate VM's to the vCenters that they reside on in a report? For example, in a VM inventory report, I need to get VM, ESX host, and vCenter. Could any of you help on this? Thanks.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The variable $defaultVIServers contains all the vCenters you are connected to.

So you could loop through the content of this variable.

$defaultVIServers | %{
  $vCenter = $_
  Get-VM -Server $vCenter | Select @{N="vCenter";E={$vCenter.Name}},Host,Name
}

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

The variable $defaultVIServers contains all the vCenters you are connected to.

So you could loop through the content of this variable.

$defaultVIServers | %{
  $vCenter = $_
  Get-VM -Server $vCenter | Select @{N="vCenter";E={$vCenter.Name}},Host,Name
}

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
kevinjj
Contributor
Contributor
Jump to solution

Hi Luc,

I wanted to apply to the following script as well, but it only collects information from the first vcenter server, vcenter1. Could you take a look when you get a chance? Much appreciated.

$start = (Get-Date).AddDays(-10)

$eventNr = 9999

$viservers = "vcenter1","vcenter2","vcenter3"

Connect-VIServer $Viservers

$defaultVIServers | %{

~ $vCenter = $_~

$report = @()

Get-VIEvent -server $vCenter -Start $start -MaxSamples $eventNr | `

Where-Object {$_.GetType().Name -eq "VmCreatedEvent"} | % {

~ $row = "" | Select Date, VM, User, Cluster, Host,Datacenter,vCenter~

~ $row.Date = $_.createdTime~

~ $row.vm = $_.vm.name~

~ $row.User = $_.userName~

~ $row.Datacenter = $_.datacenter.name~

~ $row.vCenter = $vCenter.Name~

~ $t = New-Object VMware.Vim.ManagedObjectReference~

~ $t.type = $_.computeResource.computeResource.type~

~ $t.Value = $_.computeResource.computeResource.Value~

~ $row.Cluster = (Get-View $t).Name~

~ $t.type = $_.host.host.type~

~ $t.Value = $_.host.host.Value~

~ $row.Host = (Get-View $t).Name~

~ $report += $row~

~ }~

}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you see the 2 connections to the vCenters being made ?

Btw, you can use the MoRefs in the object a lot easier like this

$start = (Get-Date).AddDays(-10)
$eventNr = 9999
$viservers = "vcenter1","vcenter2","vcenter3"

Connect-VIServer $Viservers
$defaultVIServers | %{
	$vCenter = $_
	$report = @()

	Get-VIEvent -server $vCenter -Start $start -MaxSamples $eventNr | `
	Where-Object {$_.GetType().Name -eq "VmCreatedEvent"} | % {
		$row = "" | Select Date, VM, User, Cluster, Host,Datacenter,vCenter
		$row.Date = $_.createdTime
		$row.vm = $_.vm.name
		$row.User = $_.userName
		$row.Datacenter = $_.datacenter.name
		$row.vCenter = $vCenter.Name
		$row.Cluster = (Get-View $_.computeResource.computeResource).Name
		$row.Host = (Get-View $_.host.host).Name
		$report += $row
		}
}

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
kevinjj
Contributor
Contributor
Jump to solution

Hi Luc, still the same issue. vcenter1, vcenter2, and vcenter3 were connected successfully. If the sequence is change from $viservers = "vcenter1","vcenter2","vcenter3" to $viservers = "vcenter2","vcenter1","vcenter3". Only events on vcenter2 can be retrieved. My PowerCLI version is 4.0 U1 build 208462.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could you check if the DefaultVIServerMode is set to "multi" ?

Get-PowerCLIConfiguration

If not, change it like this

 Set-PowerCLIConfiguration -DefaultVIServerMode "Multiple" -Confirm:$false

And try to run the previous script again.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
kevinjj
Contributor
Contributor
Jump to solution

It was "multiple", and I reran the script after setting it to "multiple" again. but had no luck...

0 Kudos
kevinjj
Contributor
Contributor
Jump to solution

It is OK to get events from all 3 vcenters if the loop is removed, but it won't get the vcenter info for each vm.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I overlooked this as well, but the initialisation of the $report array should be done outside the loop.

$start = (Get-Date).AddDays(-10)
$eventNr = 9999
$viservers = "vcenter1","vcenter2","vcenter3"
$report = @()

Connect-VIServer $Viservers
$defaultVIServers | %{
	$vCenter = $_

	Get-VIEvent -server $vCenter -Start $start -MaxSamples $eventNr | `
	Where-Object {$_.GetType().Name -eq "VmCreatedEvent"} | % {
		$row = "" | Select Date, VM, User, Cluster, Host,Datacenter,vCenter
		$row.Date = $_.createdTime
		$row.vm = $_.vm.name
		$row.User = $_.userName
		$row.Datacenter = $_.datacenter.name
		$row.vCenter = $vCenter.Name
		$row.Cluster = (Get-View $_.computeResource.computeResource).Name
		$row.Host = (Get-View $_.host.host).Name
		$report += $row
		}
}

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
kevinjj
Contributor
Contributor
Jump to solution

Thanks Luc that fixed it!!

0 Kudos