Expert
Expert

quick question about reporting script

Jump to solution

hi

any difference between these 2 reporting formats?

connect-viserver vc

Get-VMHost | % {

  $vmhost = $_

  $Report = "" | select Hostname, version, powerstate, OSVersion

  $Report.Hostname = $vmhost.Name

  $Report.version = $vmhost.Config.Product.version

  $Report.powerstate = $vmhost.Runtime.PowerState

  $Report.OSversion = $vmhost.Config.Product.FullName

}

$HostReport

----------------------------------------------------------------------------------------

connect-viserver vc

$HostReport = @()

Get-vmhost | get-view | %{

  $vmhost = $_

  $Report = "" | select Hostname, version, powerstate, OSVersion

  $Report.Hostname = $vmhost.Name

  $Report.version = $vmhost.Config.Product.version

  $Report.powerstate = $vmhost.Runtime.PowerState

  $Report.OSversion = $vmhost.Config.Product.FullName

$HostReport += $Report

}

$HostReport

why define an empty array on the second one for?

0 Kudos
1 Solution

Accepted Solutions
Leadership
Leadership

I think I answered that earlier.

See "Because you want to use the addition (+=) of a row to the report array."

If you wouldn't declare the array, the addition of the object $Report to the variable $HostReport would not work.

Defining $HostReport as an [array] provides the addition of an object to the array.


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

View solution in original post

0 Kudos
9 Replies
Leadership
Leadership

Because you want to use the addition (+=) of a row to the report array.

Not sure how the 1st one is producing output ?


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

0 Kudos
Expert
Expert

lucd

both gives same output.

0 Kudos
Leadership
Leadership

Strange, in the 1st script you are collecting the data in $Report but you are displaying $HostReport.

Since you don't seem to assign anything to $HostReport, there should be no output.

Could it be that $HostReport is still populated from the 2nd script.

You don't declare $HostReport as an array in the 1st script


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

0 Kudos
Expert
Expert

sorry there was typo error. but both gives same output

=======================================================

Get-VMHost | Get-View |  % {


$vmhost = $_

  $Report = "" | select Hostname, version, powerstate, OSVersion

  $Report.Hostname = $vmhost.Name

  $Report.version = $vmhost.Config.Product.version

  $Report.powerstate = $vmhost.Runtime.PowerState

  $Report.OSversion = $vmhost.Config.Product.FullName

  $Report

}

========================================================

output

Hostname   : esxhost1.domain.com

version    : 6.0.0

powerstate : poweredOn

OSVersion  : VMware ESXi 6.0.0 build-2494585

Hostname   : esxhost2.domain.com

version    : 6.0.0

powerstate : poweredOn

OSVersion  : VMware ESXi 6.0.0 build-2494585

========================================================

$HostReport = @()

Get-vmhost | get-view | %{

  $vmhost = $_

  $Report = "" | select Hostname, version, powerstate, OSVersion

  $Report.Hostname = $vmhost.Name

  $Report.version = $vmhost.Config.Product.version

  $Report.powerstate = $vmhost.Runtime.PowerState

  $Report.OSversion = $vmhost.Config.Product.FullName

$HostReport += $Report

}

$HostReport

=======================================================

Hostname   : esxhost1.domain.com

version    : 6.0.0

powerstate : poweredOn

OSVersion  : VMware ESXi 6.0.0 build-2494585

Hostname   : esxhost2.domain.com

version    : 6.0.0

powerstate : poweredOn

OSVersion  : VMware ESXi 6.0.0 build-2494585

0 Kudos
Leadership
Leadership

Ok, got it.

And I suppose your question regarding the empty array declaration is answered as well ? Or not ?


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

0 Kudos
Expert
Expert

lucd, i guess question is why do we need the array defined in the 2nd script

$HostReport = @()

$HostReport += $Report

if the 1st script gives same output

0 Kudos

If you just want a dump in the order returned by Get-VMHost then you don't need the collection (array). You'd want the collection if you plan on doing something else with the data besides just dumping it to the screen "as is" (i.e. if you wanted a report that dumped out the hosts sorted by version number and a count of how many for each version where powered on and powered off)

0 Kudos
Leadership
Leadership

I think I answered that earlier.

See "Because you want to use the addition (+=) of a row to the report array."

If you wouldn't declare the array, the addition of the object $Report to the variable $HostReport would not work.

Defining $HostReport as an [array] provides the addition of an object to the array.


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

View solution in original post

0 Kudos
Expert
Expert

thanks that makes sense

0 Kudos