VMware Cloud Community
a2alpha
Expert
Expert
Jump to solution

system.object in get-datacenter get-cluster script

Given the script below as part of an HTML report, when I run it in the toolkit it brings back {cluster1, cluster2}

However when it is run through the script in the html cell it returns System.Object[].

I think I need to specify which one to return with a but can't find where to put it. Ideally if it would just list them one after another that would be great but happy with splitting them up into upto 4 columns.

Thanks,

Dan

$datacenters = get-datacenter | sort name

$Report = @()

ForEach ($datacenter in $datacenters)

{

$clinds = get-cluster -location $datacenter | get-view

$ReportObj = "" | Select "Datacenter Name", "Cluster Name 1", "Cluster Name 2", "Cluster Name 3", "Cluster Name 4"

$ReportObj."Datacenter Name" = $datacenter.name

$ReportObj."Cluster Name 1" = $clinds | foreach{$_.name}

$ReportObj."Cluster Name 2" = $clinds | foreach{$_.name}

$ReportObj."Cluster Name 3" = $clinds | foreach{$_.name}

$ReportObj."Cluster Name 4" = $clinds | foreach{$_.name}

$Report += $ReportObj

}

$Report | ConvertTo-Html -title "Datacenter Information" -body "<H4>Datacenter Information</H4>" | Out-File -Append $filelocation

0 Kudos
1 Solution

Accepted Solutions
Zsoldier
Expert
Expert
Jump to solution

Try this:

$datacenters = get-datacenter | sort name

$Report = @()

ForEach ($datacenter in $datacenters)

{

$clinds = get-cluster -location $datacenter

ForEach ($clind in $clinds)

{

$ReportObj = "" | Select "Datacenter Name", "Cluster Name"

$ReportObj."Datacenter Name" = $datacenter.name

$ReportObj."Cluster Name" = $clind.Name

$Report += $ReportObj

}

}

$Report

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

I suspect you only want the cluster names in the report.

You can do

$datacenters = get-datacenter | sort name
$Report = @()
foreach ($datacenter in $datacenters)
{
	$clinds = get-cluster -location $datacenter | get-view
	$ReportObj = "" | Select "Datacenter Name", "Cluster Name 1", "Cluster Name 2", "Cluster Name 3", "Cluster Name 4"
	$ReportObj."Datacenter Name" = $datacenter.name
	$ReportObj."Cluster Name 1" = $clinds[0].Name
	$ReportObj."Cluster Name 2" = $clinds[1].Name
	$ReportObj."Cluster Name 3" = $clinds[2].Name
	$ReportObj."Cluster Name 4" = $clinds[3].Name
	$Report += $ReportObj
}
$Report | ConvertTo-Html -title "Datacenter Information" -body "<H4>Datacenter Information</H4>" | Out-File -Append $filelocation


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

a2alpha
Expert
Expert
Jump to solution

That seems to output fine when on a datacenter with more than one cluster, if it has only one then the fields are blank, also as it is running it gives the following in red.

Unable to index into an object of type VMware.Vim.ClusterComputeResource.

At line:9 char:47

+ $ReportObj."Cluster Name 4" = $clinds[3 &lt;&lt;&lt;&lt; ].Name

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, I thought you always had 4 clusters in each datacenter.

Try this, it should allow for 0 to 4 clusters in each datacenter.

$datacenters = get-datacenter | sort name
$Report = @()
foreach ($datacenter in $datacenters)
{
	$clinds = @(get-cluster -location $datacenter | get-view)
	$ReportObj = "" | Select "Datacenter Name", "Cluster Name 1", "Cluster Name 2", "Cluster Name 3", "Cluster Name 4"
	$ReportObj."Datacenter Name" = $datacenter.name
	$i = 1
	foreach($cluster in $clinds){
		$ReportObj."Cluster Name $i" = $clinds[$i-1].Name
		$i++
	}
	$Report += $ReportObj
}
$Report | ConvertTo-Html -title "Datacenter Information" -body "<H4>Datacenter Information</H4>" | Out-File -Append $filelocation


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

0 Kudos
Zsoldier
Expert
Expert
Jump to solution

Try this:

$datacenters = get-datacenter | sort name

$Report = @()

ForEach ($datacenter in $datacenters)

{

$clinds = get-cluster -location $datacenter

ForEach ($clind in $clinds)

{

$ReportObj = "" | Select "Datacenter Name", "Cluster Name"

$ReportObj."Datacenter Name" = $datacenter.name

$ReportObj."Cluster Name" = $clind.Name

$Report += $ReportObj

}

}

$Report

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos
a2alpha
Expert
Expert
Jump to solution

thanks for this, appreciate it.

0 Kudos