VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Help with Export to .csv

How can I export the output to .csv

foreach($cluster in Get-Cluster){

    foreach($esx in (Get-VMHost -Location $cluster)) {

        $esxcli = Get-EsxCli -VMHost $esx

        $esxcli.storage.vmfs.extent.list() | %{

            $esxcli.storage.core.path.list($_.DeviceName) |

            where{$_.Transport -eq 'fc'} | select -first 1 | %{

                New-Object PSObject -Property @{

                    #VC = $vc

                    Cluster = $cluster.Name

                    VMHost = $esx.Name

                    Datastore = $_.DeviceDisplayName

                    LUN_ID = $_.LUN

                    #SIOC = (Get-View -ViewType Datastore -Property 'IormConfiguration.Enabled' -Filter @{'Name'="^$($_.DeviceDisplayName)$"}).IormConfiguration.Enabled

                }

            }

        }

    }

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

A ForEach doesn't place anything in the pipeline, so you have to capture the output, which you can then pipe to Export-Csv.

For example like this

$report = foreach($cluster in Get-Cluster){

    foreach($esx in (Get-VMHost -Location $cluster)) {

        $esxcli = Get-EsxCli -VMHost $esx

        $esxcli.storage.vmfs.extent.list() | %{

            $esxcli.storage.core.path.list($_.DeviceName) |

            where{$_.Transport -eq 'fc'} | select -first 1 | %{

                New-Object PSObject -Property @{

                    #VC = $vc

                    Cluster = $cluster.Name

                    VMHost = $esx.Name

                    Datastore = $_.DeviceDisplayName

                    LUN_ID = $_.LUN

                    #SIOC = (Get-View -ViewType Datastore -Property 'IormConfiguration.Enabled' -Filter @{'Name'="^$($_.DeviceDisplayName)$"}).IormConfiguration.Enabled

                }

            }

        }

    }

}

$report | Export-Csv .\report.csv -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

A ForEach doesn't place anything in the pipeline, so you have to capture the output, which you can then pipe to Export-Csv.

For example like this

$report = foreach($cluster in Get-Cluster){

    foreach($esx in (Get-VMHost -Location $cluster)) {

        $esxcli = Get-EsxCli -VMHost $esx

        $esxcli.storage.vmfs.extent.list() | %{

            $esxcli.storage.core.path.list($_.DeviceName) |

            where{$_.Transport -eq 'fc'} | select -first 1 | %{

                New-Object PSObject -Property @{

                    #VC = $vc

                    Cluster = $cluster.Name

                    VMHost = $esx.Name

                    Datastore = $_.DeviceDisplayName

                    LUN_ID = $_.LUN

                    #SIOC = (Get-View -ViewType Datastore -Property 'IormConfiguration.Enabled' -Filter @{'Name'="^$($_.DeviceDisplayName)$"}).IormConfiguration.Enabled

                }

            }

        }

    }

}

$report | Export-Csv .\report.csv -NoTypeInformation -UseCulture


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much Smiley Happy

0 Kudos