PowerCLI

  • 1.  multi vcenter report

    Posted Jun 22, 2010 05:30 AM

    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.



  • 2.  RE: multi vcenter report
    Best Answer

    Posted Jun 22, 2010 05:39 AM

    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



  • 3.  RE: multi vcenter report

    Posted Jun 22, 2010 07:24 AM

    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~

    ~ }~

    }



  • 4.  RE: multi vcenter report

    Posted Jun 22, 2010 08:04 AM

    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



  • 5.  RE: multi vcenter report

    Posted Jun 22, 2010 08:38 AM

    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.



  • 6.  RE: multi vcenter report

    Posted Jun 22, 2010 08:43 AM

    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



  • 7.  RE: multi vcenter report

    Posted Jun 22, 2010 09:01 AM

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



  • 8.  RE: multi vcenter report

    Posted Jun 22, 2010 09:39 AM

    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



  • 9.  RE: multi vcenter report

    Posted Jun 22, 2010 09:53 AM

    Thanks Luc that fixed it!!



  • 10.  RE: multi vcenter report

    Posted Jun 22, 2010 09:22 AM

    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.