VMware Cloud Community
Mulliganx
Enthusiast
Enthusiast
Jump to solution

Exporting Loop into CSV

I keep receiving input object when adding Export-CSV at the end of the following script.  I tried adding a pipe at the beginning and that returns errors as well.


How can I export this script to a csv?   The trouble area is in bold

$myvmhosts = Get-VMHost | select Name

foreach($myvmhost in $myvmhosts)

{

  Get-VMHostNetworkAdapter -vmkernel -VMHost $myvmhost.Name | select VMhost, Name, Mac, IP | format-table -autosize  | Out-String

Export-csv C:\info.csv -NoTypeInformation

0 Kudos
1 Solution

Accepted Solutions
Wh33ly
Hot Shot
Hot Shot
Jump to solution

   $myvmhosts = Get-VMHost

    foreach($myvmhost in $myvmhosts)

    {

     Get-VMHostNetworkAdapter -vmkernel -VMHost $myvmhost.Name | select VMhost, Name, Mac, IP | Export-csv C:\info.csv -NoTypeInformation -UseCulture -Append

    }

View solution in original post

0 Kudos
4 Replies
Wh33ly
Hot Shot
Hot Shot
Jump to solution

   $myvmhosts = Get-VMHost

    foreach($myvmhost in $myvmhosts)

    {

     Get-VMHostNetworkAdapter -vmkernel -VMHost $myvmhost.Name | select VMhost, Name, Mac, IP | Export-csv C:\info.csv -NoTypeInformation -UseCulture -Append

    }

0 Kudos
Zsoldier
Expert
Expert
Jump to solution

You could just do this:

$data = Get-VMHost | Get-VMHostNetworkAdapter -vmkernel | select vmhost, name, mac, ip

$data | convertto-csv -notypeinformation | out-file C:\mycsv.csv

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos
Mulliganx
Enthusiast
Enthusiast
Jump to solution

I see I needed to add those arguments.  When I added just the export-csv into the loop it returned nothing as a value.  I will note to add those values every time I am adding that command into a loop.  Thank you

0 Kudos
Wh33ly
Hot Shot
Hot Shot
Jump to solution

Chris's option is a bit better

You could just do this:

*changed convertto-csv to export-csv

*added -useculture

$data = Get-VMHost | Get-VMHostNetworkAdapter -vmkernel | select vmhost, name, mac, ip

$data | export-csv -notypeinformation -useculture| out-file C:\mycsv.csv

0 Kudos