VMware Cloud Community
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

help for the script

Hello team

I written script to collect host info, basically i am getting some information is correctly but for some fields i am getting  System.Object[]

here is the script for all field  

vswitch    vnic    vnicspec    pnicdevice    pnicmac    linkspeed    pnickey    pnicdriver    nodewwn    portwwn    hbadevice

i am getting System.Object[], kindly help

 

 

  # Getthe VM hosts

 

  $objHosts = @()

 

  $vmhosts = Get-vmhost

 

  if ($null -eq $vmhosts) {

 

  write-host "Retrying "$VIServ

 

  $vmhosts = Get-vmhost

 

  }

 

 

 

  # for each host get the inventory info I want...

 

  foreach ($vmhost in $vmhosts) {

 

  $vmhostview = $vmhost | Get-View

 

  $objHost = "" | Select 

Region,Name,OSType,OSFullName,OSVersion,OSBuild,Vendor,Model,UUID,MemorySize,CpuModel,CpuMHz,NumCPuPkgs,NumCPuCores,Vm

otionEnabled,ESXHostID,vmnicscount,hbacount,OCPU,OMEM,uptime,memreservation,cpureservation,memlimit,cpulimit,prdley,vs

witch,vnic,vnicspec,pnicdevice,pnicmac,linkspeed,pnickey,pnicdriver,nodewwn,portwwn,hbadevice,lboot,ClusterID

 

  write-host "Processing Host for "$vmhostview.Name

 

  $objHost.Region = $Region

 

  $objHost.Name = $vmhostview.Name

 

  $objHost.OSType = $vmhostview.Config.product.Name

 

  $objHost.OSFullName = $vmhostview.Config.product.FullName

 

  $objHost.OSVersion = $vmhostview.Config.product.Version

 

  $objHost.OSBuild = $vmhostview.Config.product.Build

 

  $objHost.Vendor = $vmhostview.summary.hardware.Vendor

 

  $objHost.Model = $vmhostview.summary.hardware.Model

 

  $objHost.UUID = $vmhostview.summary.hardware.UUID

 

  $objHost.MemorySize = ([int]($vmhostview.summary.hardware.MemorySize / 1MB))

 

  $objHost.CpuModel = $vmhostview.summary.hardware.CpuModel

 

  $objHost.CpuMhz = $vmhostview.summary.hardware.CpuMhz

 

  $objHost.NumCPuPkgs = $vmhostview.summary.hardware.NumCPuPkgs

 

  $objHost.NumCPuCores = $vmhostview.summary.hardware.NumCPuCores

  $objHost.VmotionEnabled = $vmhostview.summary.config.VmotionEnabled

  $objHost.ESXHostID = $vmhostview.moref.value

$objHost.vmnics=$vmhostview.summary.hardware.numnics

$objHost.vmhbas=$vmhostview.summary.hardware.numhbas

$objHost.lboot=$vmhostview.summary.runtime.Boottime

$objHost.OCPU=$vmhostview.summary.quickstats.overallCPUusage

$objHost.OMEM=$vmhostview.summary.quickstats.overallMemoryUsage

$objHost.uptime=$vmhostview.summary.quickstats.Uptime

$objHost.memreservation=$vmhostview.config.systemresources.config.memoryallocation.reservation

$objHost.cpureservation=$vmhostview.config.systemresources.config.cpuallocation.reservation

$objHost.memlimit=$vmhostview.config.systemresources.config.memoryallocation.limit

$objHost.cpulimit=$vmhostview.config.systemresources.config.CPUallocation.limit

$objHost.prdley=$vmhostview.config.network.portgroup.key

$objHost.vswitch= $vmhostview.config.network.portgroup.vswitch

$objHost.vnic=$vmhostview.config.network.vnic.portgroup

$objHost.vnicspec= $vmhostview.config.network.vnic.spec.mac

$objHost.pnicdevice=$vmhostview.config.network.pnic.device

$objHost.pnicmac=$vmhostview.config.network.pnic.mac

$objHost.linkspeed=$vmhostview.config.network.pnic.linkspeed

$objHost.pnickey=$vmhostview.config.network.pnic.key

$objHost.pnicdriver=$vmhostview.config.network.pnic.driver

$objHost.Nodewwn=$vmhostview.config.storagedevice.hostbusadapter.NodeWorldWideName

$objHost.portwwn=$vmhostview.config.storagedevice.hostbusadapter.PortWorldWideName

$objHost.hbadevice= $vmhostview.config.storagedevice.hostbusadapter.device

   $objHost.ClusterID = ($vmhost | Get-Cluster).Name

  $objHosts = $objHosts + $objHost

  }

 

  # Write host inventory to a CSV...

  $Filename = "ESX_Host_Report1.csv"

  $objHosts | Export-Csv $Filename -noType

 
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

No tricks, just a Join of the array elements.

# Get the VM hosts

$objHosts = @()

$vmhosts = Get-vmhost

if ($null -eq $vmhosts) {

    write-host "Retrying "$VIServ

    $vmhosts = Get-vmhost

}

# for each host get the inventory info I want...

foreach ($vmhost in $vmhosts) {

    $vmhostview = $vmhost | Get-View

    $objHost = "" | Select

        Region,Name,OSType,OSFullName,OSVersion,OSBuild,Vendor,Model,UUID,MemorySize,CpuModel,CpuMHz,NumCPuPkgs,NumCPuCores,Vm

        otionEnabled,ESXHostID,vmnicscount,hbacount,OCPU,OMEM,uptime,memreservation,cpureservation,memlimit,cpulimit,prdley,vs

        witch,vnic,vnicspec,pnicdevice,pnicmac,linkspeed,pnickey,pnicdriver,nodewwn,portwwn,hbadevice,lboot,ClusterID

    Write-Host "Processing Host for "$vmhostview.Name

    $objHost.Region = $Region

    $objHost.Name = $vmhostview.Name

    $objHost.OSType = $vmhostview.Config.product.Name

    $objHost.OSFullName = $vmhostview.Config.product.FullName

    $objHost.OSVersion = $vmhostview.Config.product.Version

    $objHost.OSBuild = $vmhostview.Config.product.Build

    $objHost.Vendor = $vmhostview.summary.hardware.Vendor

    $objHost.Model = $vmhostview.summary.hardware.Model

    $objHost.UUID = $vmhostview.summary.hardware.UUID

    $objHost.MemorySize = ([int]($vmhostview.summary.hardware.MemorySize / 1MB))

    $objHost.CpuModel = $vmhostview.summary.hardware.CpuModel

    $objHost.CpuMhz = $vmhostview.summary.hardware.CpuMhz

    $objHost.NumCPuPkgs = $vmhostview.summary.hardware.NumCPuPkgs

    $objHost.NumCPuCores = $vmhostview.summary.hardware.NumCPuCores

    $objHost.VmotionEnabled = $vmhostview.summary.config.VmotionEnabled

    $objHost.ESXHostID = $vmhostview.moref.value

    $objHost.vmnics=$vmhostview.summary.hardware.numnics

    $objHost.vmhbas=$vmhostview.summary.hardware.numhbas

    $objHost.lboot=$vmhostview.summary.runtime.Boottime

    $objHost.OCPU=$vmhostview.summary.quickstats.overallCPUusage

    $objHost.OMEM=$vmhostview.summary.quickstats.overallMemoryUsage

    $objHost.uptime=$vmhostview.summary.quickstats.Uptime

    $objHost.memreservation=$vmhostview.config.systemresources.config.memoryallocation.reservation

    $objHost.cpureservation=$vmhostview.config.systemresources.config.cpuallocation.reservation

    $objHost.memlimit=$vmhostview.config.systemresources.config.memoryallocation.limit

    $objHost.cpulimit=$vmhostview.config.systemresources.config.CPUallocation.limit

    $objHost.prdley=$vmhostview.config.network.portgroup.key

    $objHost.vswitch= $vmhostview.config.network.portgroup.vswitch

    $objHost.vnic=$vmhostview.config.network.vnic.portgroup

    $objHost.vnicspec= $vmhostview.config.network.vnic.spec.mac

    $objHost.pnicdevice=$vmhostview.config.network.pnic.device -join ' | '

    $objHost.pnicmac=$vmhostview.config.network.pnic.mac -join ' | '

    $objHost.linkspeed=$vmhostview.config.network.pnic.linkspeed -join ' | '

    $objHost.pnickey=$vmhostview.config.network.pnic.key -join ' | '

    $objHost.pnicdriver=$vmhostview.config.network.pnic.driver -join ' | '

    $objHost.Nodewwn=$vmhostview.config.storagedevice.hostbusadapter.NodeWorldWideName -join ' | '

    $objHost.portwwn=$vmhostview.config.storagedevice.hostbusadapter.PortWorldWideName -join ' | '

    $objHost.hbadevice= $vmhostview.config.storagedevice.hostbusadapter.device -join ' | '

    $objHost.ClusterID = ($vmhost | Get-Cluster).Name

    $objHosts = $objHosts + $objHost

}

# Write host inventory to a CSV...

$Filename = "ESX_Host_Report1.csv"

$objHosts | Export-Csv $Filename -noType


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

View solution in original post

Reply
0 Kudos
16 Replies
LucD
Leadership
Leadership
Jump to solution

When you see System.Object[] appearing in yoru CSV file, that means that the value in that property was not a single value, but an array.

And Export-Csv presents an array like that in the CSV file.

You have a number of options:

Make sure you only have a single value in each property of the objects you export

If you want to see all the values, a quick and dirty way of doing that is by joining all the values of the array in one string.

For example

$array = 1,2,3,4

$array -join '|'


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

Reply
0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

Thanks Lucd for the response

could you please let me know where i need to place Join and array, i am confused, if you provide  with correction of my script, i will see if i can do it next time onwards.

can you please edit my script? and do let me know where you are recomending to place?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

On which columns in the CSV are you getting this text ?

Perhaps you adding a screenshot would make it more clear ?


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

Reply
0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

as requested , adding screenshot for columns which i am not getting value

$8A5C53061E7171A.jpg

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

And what would you like to see in those columns, the first value, or all values concatenated ?


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

Reply
0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

I am interesting for all values not sure what tricks you do 🙂

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No tricks, just a Join of the array elements.

# Get the VM hosts

$objHosts = @()

$vmhosts = Get-vmhost

if ($null -eq $vmhosts) {

    write-host "Retrying "$VIServ

    $vmhosts = Get-vmhost

}

# for each host get the inventory info I want...

foreach ($vmhost in $vmhosts) {

    $vmhostview = $vmhost | Get-View

    $objHost = "" | Select

        Region,Name,OSType,OSFullName,OSVersion,OSBuild,Vendor,Model,UUID,MemorySize,CpuModel,CpuMHz,NumCPuPkgs,NumCPuCores,Vm

        otionEnabled,ESXHostID,vmnicscount,hbacount,OCPU,OMEM,uptime,memreservation,cpureservation,memlimit,cpulimit,prdley,vs

        witch,vnic,vnicspec,pnicdevice,pnicmac,linkspeed,pnickey,pnicdriver,nodewwn,portwwn,hbadevice,lboot,ClusterID

    Write-Host "Processing Host for "$vmhostview.Name

    $objHost.Region = $Region

    $objHost.Name = $vmhostview.Name

    $objHost.OSType = $vmhostview.Config.product.Name

    $objHost.OSFullName = $vmhostview.Config.product.FullName

    $objHost.OSVersion = $vmhostview.Config.product.Version

    $objHost.OSBuild = $vmhostview.Config.product.Build

    $objHost.Vendor = $vmhostview.summary.hardware.Vendor

    $objHost.Model = $vmhostview.summary.hardware.Model

    $objHost.UUID = $vmhostview.summary.hardware.UUID

    $objHost.MemorySize = ([int]($vmhostview.summary.hardware.MemorySize / 1MB))

    $objHost.CpuModel = $vmhostview.summary.hardware.CpuModel

    $objHost.CpuMhz = $vmhostview.summary.hardware.CpuMhz

    $objHost.NumCPuPkgs = $vmhostview.summary.hardware.NumCPuPkgs

    $objHost.NumCPuCores = $vmhostview.summary.hardware.NumCPuCores

    $objHost.VmotionEnabled = $vmhostview.summary.config.VmotionEnabled

    $objHost.ESXHostID = $vmhostview.moref.value

    $objHost.vmnics=$vmhostview.summary.hardware.numnics

    $objHost.vmhbas=$vmhostview.summary.hardware.numhbas

    $objHost.lboot=$vmhostview.summary.runtime.Boottime

    $objHost.OCPU=$vmhostview.summary.quickstats.overallCPUusage

    $objHost.OMEM=$vmhostview.summary.quickstats.overallMemoryUsage

    $objHost.uptime=$vmhostview.summary.quickstats.Uptime

    $objHost.memreservation=$vmhostview.config.systemresources.config.memoryallocation.reservation

    $objHost.cpureservation=$vmhostview.config.systemresources.config.cpuallocation.reservation

    $objHost.memlimit=$vmhostview.config.systemresources.config.memoryallocation.limit

    $objHost.cpulimit=$vmhostview.config.systemresources.config.CPUallocation.limit

    $objHost.prdley=$vmhostview.config.network.portgroup.key

    $objHost.vswitch= $vmhostview.config.network.portgroup.vswitch

    $objHost.vnic=$vmhostview.config.network.vnic.portgroup

    $objHost.vnicspec= $vmhostview.config.network.vnic.spec.mac

    $objHost.pnicdevice=$vmhostview.config.network.pnic.device -join ' | '

    $objHost.pnicmac=$vmhostview.config.network.pnic.mac -join ' | '

    $objHost.linkspeed=$vmhostview.config.network.pnic.linkspeed -join ' | '

    $objHost.pnickey=$vmhostview.config.network.pnic.key -join ' | '

    $objHost.pnicdriver=$vmhostview.config.network.pnic.driver -join ' | '

    $objHost.Nodewwn=$vmhostview.config.storagedevice.hostbusadapter.NodeWorldWideName -join ' | '

    $objHost.portwwn=$vmhostview.config.storagedevice.hostbusadapter.PortWorldWideName -join ' | '

    $objHost.hbadevice= $vmhostview.config.storagedevice.hostbusadapter.device -join ' | '

    $objHost.ClusterID = ($vmhost | Get-Cluster).Name

    $objHosts = $objHosts + $objHost

}

# Write host inventory to a CSV...

$Filename = "ESX_Host_Report1.csv"

$objHosts | Export-Csv $Filename -noType


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

Reply
0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

excellent!!! thanks a lot dear ..

I am still yet to get link speed and other parameter, could you please help me?

$76853A480469D8D6.jpg

Reply
0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

Thanks Lucd.. I corrected myself.. thanks for thought me this lesson

Reply
0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

LucD

I am seeing wrong WWN and WWPN number, when i run the script, i can see different than what is available in VC . anything wrong ?

can you please let me know how to get correct WWN/WWPn number?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What are you seeing then ?

I didn't provide you with that code, that was the code you gave me btw.

Which PowerShell version are you using ?

Do a $PSVersionTable


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

Reply
0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

yes correct Lucd.. that code was taken by me, but trying to understand why that script showing incorrect node and Port wwn numbers

version is below

Name                           Value

----                           -----

PSVersion                      4.0

WSManStackVersion              3.0

SerializationVersion           1.1.0.1

CLRVersion                     4.0.30319.34014

BuildVersion                   6.3.9600.16394

PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}

PSRemotingProtocolVersion      2.2

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

And what is it showing for the WWN ?

From the script in and in the Web Client ?

Trying to understand what goes wrong


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

Reply
0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

In VC

$6F0EFD3E533881BD.jpg

from the script report

$460687762C3CD1B3.jpg

its quite different

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, its because one is displayed in hex and the other isn't.

Try changing those 2 lines to

$objHost.Nodewwn= (($vmhostview.config.storagedevice.hostbusadapter.NodeWorldWideName | %{"{0:x2}" -f $_}) -join ':'-join ' | '

$objHost.portwwn=(($vmhostview.config.storagedevice.hostbusadapter.PortWorldWideName | %{"{0:x2}" -f $_}) -join ':') -join ' | '


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

Reply
0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

Thanks Lucd, Its worked 🙂

Reply
0 Kudos