VMware Cloud Community
Hetfield84
Enthusiast
Enthusiast
Jump to solution

Get-View Results from script either work, are blank or returns System.Object[] in Output File

Hi,

When I run the code below, it successfully gets the data for everything except for "Location" and "License". The results in those two variables vary, it collects the data for some hosts, for others it's blank and for others it shows System.Object[].

I know I'm doing something wrong here, this has happened a few times when working with Get-View and I lose patience and use Get-VM or Get-VMHost instead. I figured this time I'd actually ask and learn, so I can leverage Get-View commands as I know they're faster.

I also welcome any constructive criticism in how I'm approaching this.

$hosts = Get-view -ViewType hostSystem

ForEach ($hst in $hosts) {

    $lic = Get-VMHost $hst.name | Select LicenseKey

    $cls = get-vmhost -Name $hst.Name | Get-Cluster | Select Name

    $mem = [math]::round($hst.Hardware.memorysize / 1GB, 0)

    $obj = new-object psobject

        $obj | Add-Member -MemberType NoteProperty -Name Name -Value $hst.Name

        $obj | Add-Member -MemberType NoteProperty -Name Location -Value $cls.Name

        $obj | Add-Member -MemberType NoteProperty -Name Vendor -Value $hst.summary.hardware.vendor

        $obj | Add-Member -MemberType NoteProperty -Name Model -Value $hst.summary.hardware.model  

        $obj | Add-Member -MemberType NoteProperty -Name Sockets -Value $hst.summary.hardware.NumCpuPkgs

        $obj | Add-Member -MemberType NoteProperty -Name Cores -Value $hst.summary.hardware.NumCpuCores

        $obj | Add-Member -MemberType NoteProperty -Name MemoryGB -Value $mem

        $obj | Add-Member -MemberType NoteProperty -Name Version -Value $hst.Config.Product.Version

        $obj | Add-Member -MemberType NoteProperty -Name License -Value $lic.LicenseKey

       

        $obj | export-csv "$PSScriptRoot\hostDetails.csv" -append -notypeinformation

   

}

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That is due to the Select-Object you use.

That doesn't produce a singular value, but an object.

Normally you can avoid that by using the ExpandProperty switch.

$hosts = Get-view -ViewType hostSystem

$report = @()

ForEach ($hst in $hosts) {

    $lic = Get-VMHost $hst.name | Select -ExpandProperty LicenseKey

    $cls = get-vmhost -Name $hst.Name | Get-Cluster | Select -ExpandProperty Name

    $mem = [math]::round($hst.Hardware.memorysize / 1GB, 0)

    $obj = new-object psobject

    $obj | Add-Member -MemberType NoteProperty -Name Name -Value $hst.Name

    $obj | Add-Member -MemberType NoteProperty -Name Location -Value $cls.Name

    $obj | Add-Member -MemberType NoteProperty -Name Vendor -Value $hst.summary.hardware.vendor

    $obj | Add-Member -MemberType NoteProperty -Name Model -Value $hst.summary.hardware.model

    $obj | Add-Member -MemberType NoteProperty -Name Sockets -Value $hst.summary.hardware.NumCpuPkgs

    $obj | Add-Member -MemberType NoteProperty -Name Cores -Value $hst.summary.hardware.NumCpuCores

    $obj | Add-Member -MemberType NoteProperty -Name MemoryGB -Value $mem

    $obj | Add-Member -MemberType NoteProperty -Name Version -Value $hst.Config.Product.Version

    $obj | Add-Member -MemberType NoteProperty -Name License -Value $lic.LicenseKey

    $report += $obj

}


$report | export-csv "$PSScriptRoot\hostDetails.csv" -append -notypeinformation


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

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

You are overwriting the CSV file for each iteration of the foreach loop.

Accumulate all objects in an array, then, after the foreach loop, send the array to the CSV file.

$hosts = Get-view -ViewType hostSystem

$report = @()

ForEach ($hst in $hosts) {

    $lic = Get-VMHost $hst.name | Select LicenseKey

    $cls = get-vmhost -Name $hst.Name | Get-Cluster | Select Name

    $mem = [math]::round($hst.Hardware.memorysize / 1GB, 0)

    $obj = new-object psobject

    $obj | Add-Member -MemberType NoteProperty -Name Name -Value $hst.Name

    $obj | Add-Member -MemberType NoteProperty -Name Location -Value $cls.Name

    $obj | Add-Member -MemberType NoteProperty -Name Vendor -Value $hst.summary.hardware.vendor

    $obj | Add-Member -MemberType NoteProperty -Name Model -Value $hst.summary.hardware.model

    $obj | Add-Member -MemberType NoteProperty -Name Sockets -Value $hst.summary.hardware.NumCpuPkgs

    $obj | Add-Member -MemberType NoteProperty -Name Cores -Value $hst.summary.hardware.NumCpuCores

    $obj | Add-Member -MemberType NoteProperty -Name MemoryGB -Value $mem

    $obj | Add-Member -MemberType NoteProperty -Name Version -Value $hst.Config.Product.Version

    $obj | Add-Member -MemberType NoteProperty -Name License -Value $lic.LicenseKey

 

    $report += $obj

}


$report | export-csv "$PSScriptRoot\hostDetails.csv" -append -notypeinformation


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

Hetfield84
Enthusiast
Enthusiast
Jump to solution

Hi,

Thanks for the feedback. I modified my script, but it's still returning System.Object[] for the Location and License in some cases. Any idea why that may be?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is due to the Select-Object you use.

That doesn't produce a singular value, but an object.

Normally you can avoid that by using the ExpandProperty switch.

$hosts = Get-view -ViewType hostSystem

$report = @()

ForEach ($hst in $hosts) {

    $lic = Get-VMHost $hst.name | Select -ExpandProperty LicenseKey

    $cls = get-vmhost -Name $hst.Name | Get-Cluster | Select -ExpandProperty Name

    $mem = [math]::round($hst.Hardware.memorysize / 1GB, 0)

    $obj = new-object psobject

    $obj | Add-Member -MemberType NoteProperty -Name Name -Value $hst.Name

    $obj | Add-Member -MemberType NoteProperty -Name Location -Value $cls.Name

    $obj | Add-Member -MemberType NoteProperty -Name Vendor -Value $hst.summary.hardware.vendor

    $obj | Add-Member -MemberType NoteProperty -Name Model -Value $hst.summary.hardware.model

    $obj | Add-Member -MemberType NoteProperty -Name Sockets -Value $hst.summary.hardware.NumCpuPkgs

    $obj | Add-Member -MemberType NoteProperty -Name Cores -Value $hst.summary.hardware.NumCpuCores

    $obj | Add-Member -MemberType NoteProperty -Name MemoryGB -Value $mem

    $obj | Add-Member -MemberType NoteProperty -Name Version -Value $hst.Config.Product.Version

    $obj | Add-Member -MemberType NoteProperty -Name License -Value $lic.LicenseKey

    $report += $obj

}


$report | export-csv "$PSScriptRoot\hostDetails.csv" -append -notypeinformation


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

Hetfield84
Enthusiast
Enthusiast
Jump to solution

Great thank you! I still had to tweak it slightly, but the code below works perfectly:

$hosts = Get-view -ViewType hostSystem

$report = @()

ForEach ($hst in $hosts) {

    $esxi = $hst.Name

    $lic = Get-VMHost $esxi | Select -ExpandProperty LicenseKey

    $cls = Get-VMHost -Name $esxi| Get-Cluster | Select -ExpandProperty Name

    $mem = [math]::round($hst.Hardware.memorysize / 1GB, 0)

    write-host $esxi

    write-host $lic

    write-host $cls

    $obj = new-object psobject

    $obj | Add-Member -MemberType NoteProperty -Name Name -Value $hst.Name

    $obj | Add-Member -MemberType NoteProperty -Name Location -Value $cls

    $obj | Add-Member -MemberType NoteProperty -Name Vendor -Value $hst.summary.hardware.vendor

    $obj | Add-Member -MemberType NoteProperty -Name Model -Value $hst.summary.hardware.model

    $obj | Add-Member -MemberType NoteProperty -Name Sockets -Value $hst.summary.hardware.NumCpuPkgs

    $obj | Add-Member -MemberType NoteProperty -Name Cores -Value $hst.summary.hardware.NumCpuCores

    $obj | Add-Member -MemberType NoteProperty -Name MemoryGB -Value $mem

    $obj | Add-Member -MemberType NoteProperty -Name Version -Value $hst.Config.Product.Version

    $obj | Add-Member -MemberType NoteProperty -Name License -Value $lic

    $report += $obj

}

$report | export-csv "$PSScriptRoot\hostDetails.csv" -append -notypeinformation

0 Kudos