VMware Cloud Community
johndavidd
Enthusiast
Enthusiast
Jump to solution

place multiple adapters info in same row/column in csv

For VM's that have multiple adapters I would like all the network names and mac addresses to go in the same two columns on the same row... rather than having to do MAC1 MAC2, MAC3 ... so on and so forth

 
$array = @()
foreach ($item in $csv) {
    $info = "" | Select VMName, DNSName, IP, PortGroup, MAC, vCenter
    $vm = Get-VM $item.name
    $adapters = $vm | Get-NetworkAdapter
    $info.VMName = $vm.name
    $info.DNSName = $vm.guest.hostname
    $info.IP = $vm.guest.ipaddress[0]
    foreach ($adapter in $adapters) {
        $info.PortGroup = $adapter.NetworkName
        $info.MAC = $adapter.MacAddress
    }
    $info.vCenter = $vm.vCenterServer
    $array += $info
}
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try it like this

$array = @()
foreach ($item in $csv) {
    $info = "" | Select VMName, DNSName, IP, PortGroup, MAC, vCenter
    $vm = Get-VM $item.name
    $adapters = $vm | Get-NetworkAdapter
   
$info.VMName = $vm.name
    $info.DNSName = $vm.guest.hostname
   
$info.IP = $vm.guest.ipaddress[0]     $info.PortGroup = [string]::Join(',',($adapters | Select -ExpandProperty NetworkName))     $info.MAC = [string]::Join(',',($adapters | Select -ExpandProperty MacAddress))     $info.vCenter = $vm.vCenterServer
    $array += $info
}

With the Join function you paste together multiple values into 1 string


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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try it like this

$array = @()
foreach ($item in $csv) {
    $info = "" | Select VMName, DNSName, IP, PortGroup, MAC, vCenter
    $vm = Get-VM $item.name
    $adapters = $vm | Get-NetworkAdapter
   
$info.VMName = $vm.name
    $info.DNSName = $vm.guest.hostname
   
$info.IP = $vm.guest.ipaddress[0]     $info.PortGroup = [string]::Join(',',($adapters | Select -ExpandProperty NetworkName))     $info.MAC = [string]::Join(',',($adapters | Select -ExpandProperty MacAddress))     $info.vCenter = $vm.vCenterServer
    $array += $info
}

With the Join function you paste together multiple values into 1 string


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

Reply
0 Kudos
johndavidd
Enthusiast
Enthusiast
Jump to solution

Thanks for the prompt response, let me give it a try.

Reply
0 Kudos
johndavidd
Enthusiast
Enthusiast
Jump to solution

Throws a parsing error... think you have an extra "(" after the join function.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Correct, just updated the code


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

Reply
0 Kudos