VMware Cloud Community
TimMcGee
Contributor
Contributor

Export to a file

Hello,

Can someone tell me how to export to a file?

I need the VM name, IPAddress and NetworkName(vlan id).  

The below gives me the information that I need in PowerCLI, but I need to have it in a csv or txt.

The output is larger than the screen, so I can't just copy all of it.

Needless to say, I don't use PowersCLI that often and couldn't find my answer. Smiley Happy

Thank you!

Get-VM |
ForEach-Object {
 
$VM = $_
 
$VM | Get-VMGuest | Select-Object -ExpandProperty Nics |
 
ForEach-Object {
   
$Nic = $_
   
foreach ($IP in $Nic.IPAddress)
    {
     
if ($IP.Contains('.'))
      {     
       
"" | Select-Object -Property @{Name='VM';Expression={$VM.Name}},
          @{Name
='IPAddress';Expression={$IP}},
          @{Name
='NetworkName';Expression={$Nic.NetworkName}}
      }
    }
  }
}

0 Kudos
1 Reply
LucD
Leadership
Leadership

Just send it over the pipeline to Export-Csv

Get-VM |

ForEach-Object {

    $VM = $_

    $VM | Get-VMGuest | Select-Object -ExpandProperty Nics |

    ForEach-Object {

        $Nic = $_

        foreach ($IP in $Nic.IPAddress) {

            if ($IP.Contains('.')) {   

                "" | Select-Object -Property @{Name = 'VM'; Expression = { $VM.Name } },

                @{Name = 'IPAddress'; Expression = { $IP } },

                @{Name = 'NetworkName'; Expression = { $Nic.NetworkName } }

            }

        }

    }

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

0 Kudos