VMware Cloud Community
michaelbrux
Contributor
Contributor
Jump to solution

Get-view SearchRoot when duplicate .MoRef Exists

I created a script to list out cluster details and I thought I would make it faster by using get-view instead of Get command for every object.  I ran into a problem on line 14.  It seems I have datacenters in different virtual centers with the same .MoRef vaules.  Not sure how I can script around that.  

Note this script below is a stripped down version of the overall script

$csvreport = @()

$ClusterList=@()

$OutputFile="C:\Temp\looptesting.csv"

$Date=Get-Date -Format d

  foreach ($virtualcenter in $DefaultVIServers) {

  ForEach ($DC in (Get-View -ViewType  Datacenter -Server $virtualcenter)){

  Foreach ($cluster in (Get-View -ViewType ClusterComputeResource -SearchRoot $DC.MoRef)){

  $ClusterList = "" |select-object "Collection Date","Virtual Center",DataCenter,Cluster

  $ClusterList."Collection Date"=$Date

  $ClusterList."Virtual Center"=$virtualcenter

  $ClusterList.DataCenter=$DC.Name

  $ClusterList.Cluster=$cluster.Name

  $csvreport += $ClusterList

  }

  }

  }

$csvreport |export-csv -NoTypeinformation $OutputFile

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Can't you just use the Server parameter on line 14, as you did on line 12 ?


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

View solution in original post

Reply
0 Kudos
4 Replies
ccalvetTCC
Enthusiast
Enthusiast
Jump to solution

It seems I have datacenters in different virtual centers with the same .MoRef vaules.  Not sure how I can script around that.

Extract from a post from William Lam.

A Managed Object Reference ID also known just as MoRef ID is a unique value that is generated by the vCenter Server and is guaranteed to be unique for a given entity in a single vCenter instance.

So your guess seems to be correct.

We are in the same boat regarding working with multiples vCenter servers.

The solution i am using at a high level is:
A file containing the list of all vCenter name.

Import the list with import-csv

For each vCenter

Connect to this vcenter

Extract data needed and store them in a variable like $DataForThisVcenter

$FinalResult = $FinalResult + $DataForThisVcenter

Disconnect from this vcenter

And finally export the $FinalResult

It is not perfect but at least there are no conflicts with identical moref.
If you need it, i can provide example.

Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can't you just use the Server parameter on line 14, as you did on line 12 ?


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

Reply
0 Kudos
michaelbrux
Contributor
Contributor
Jump to solution

That was my 1st thought but I wanted to be logged into all the vcenters so that any errors connecting to vcenters could be handled up front.  My fear is that the script will run for 2 hours and then fails connecting to the last vcenter and then I will have to start all over.  

Reply
0 Kudos
michaelbrux
Contributor
Contributor
Jump to solution

I started typing a response that was no I can't replace -searchroot with - server.   I then realized you meant use them both  :smileylaugh:.  And it worked

Foreach ($cluster in (Get-view -ViewType ClusterComputeResource -Server $virtualcenter -Searchroot $DC.moref)){


					
				
			
			
				
			
			
			
			
			
			
			
		
Reply
0 Kudos