Automation

 View Only
  • 1.  How to properly export ESXi host boot devices to CSV

    Posted Apr 18, 2023 12:20 AM

    I am using the script written by William Lam called "Get-ESXiBootDevice" (https://www.powershellgallery.com/packages/AsBuiltReport.VMware.vSphere/1.2.1/Content/Src%5CPrivate%5CGet-ESXiBootDevice.ps1) and it exports the data correctly to the console:

     

     Get-ESXiBootDevice *
    
    Host                                           Device                                                                       BootType Vendor Model            SizeMB IsSAS IsSSD IsUSB
    ----                                           ------                                                                       -------- ------ -----            ------ ----- ----- -----
    MyHost01.MyDomain    naa.600a098038303455575d484d46442f59                                         remote   NETAPP LUN C-Mode       10240  false false false
    MyHost02.MyDomain    naa.600a098038303455575d484d46442f2f                                         remote   NETAPP LUN C-Mode       10240  false false false
    MyHost03.MyDomain    naa.600a098038303455575d484d46442f5a                                         remote   NETAPP LUN C-Mode       10240  false false false

     

    However, now when I try to export the results to a CSV, I get this in the CSV file:

     

    et-ESXiBootDevice * | export-csv -Path MyvCenter_Host_BootDevices.csv -NoTypeInformation -UseCulture
    
    "ClassId2e4f51ef21dd47e99d3c952918aff9cd","pageHeaderEntry","pageFooterEntry","autosizeInfo","shapeInfo","groupingEntry"
    "033ecb2bc07a4d43b5ef94ed5a35d280",,,"Microsoft.PowerShell.Commands.Internal.Format.AutosizeInfo","Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo",
    "9e210fe47d09416682b841769c78b8a3",,,,,
    "27c87ef9bbda4f709f6b4002fa4af63c",,,,,
    "27c87ef9bbda4f709f6b4002fa4af63c",,,,,
    "27c87ef9bbda4f709f6b4002fa4af63c",,,,,
    "27c87ef9bbda4f709f6b4002fa4af63c",,,,,
    "27c87ef9bbda4f709f6b4002fa4af63c",,,,,

     

    I even wrote code to loop thru each host individually and return the results with the same outcome:

    $oVMHosts = Get-VMHost *
    $HostBootDevices = @()
    ForEach ($oVMHost in $oVMHosts)
        {
        $VMHostBootDevice = Get-ESXIBootDevice ($oVMHost)
        $HostBootDevices += $VMHostBootDevice
        }
    
    $HostBootDevices | export-csv -Path MyvCenter_Host_BootDevices.csv -NoTypeInformation -UseCulture

    What is the correct way to export the data to CSV?

     



  • 2.  RE: How to properly export ESXi host boot devices to CSV
    Best Answer

    Posted Apr 18, 2023 02:57 AM

     

    The issue you're facing is that the output of the Get-ESXiBootDevice command contains complex objects with properties that are not being properly formatted for CSV export. To correctly export the data to a CSV file, you can use the Select-Object cmdlet to specify the properties you want to export, and then use the Export-Csv cmdlet with the -NoTypeInformation parameter to remove the type information from the CSV file.

    Here's an example of how you can modify your code to properly export the data to CSV:

     

    powershellCopy code
    $VMHosts = Get-VMHost *
    $HostBootDevices = @()
    
    ForEach ($VMHost in $VMHosts) {
        $VMHostBootDevice = Get-ESXiBootDevice $VMHost | Select-Object Host, Device, BootType, Vendor, Model, SizeMB, IsSAS, IsSSD, IsUSB
        $HostBootDevices += $VMHostBootDevice
    }
    
    $HostBootDevices | Export-Csv -Path MyvCenter_Host_BootDevices.csv -NoTypeInformation -UseCulture

     

     

    In this example, the Select-Object cmdlet is used to specify the properties Host, Device, BootType, Vendor, Model, SizeMB, IsSAS, IsSSD, and IsUSB that you want to export to the CSV file. The resulting data will be formatted correctly as CSV without any type information, and the -UseCulture parameter ensures that the correct culture settings are used for formatting numbers and dates in the CSV file.