AlbertWT
Virtuoso
Virtuoso

Displaying VM IP addresses in single row

Jump to solution

Hi experts,

I need your help in modifying my basic script here to list VMs information, especially the IP addresses as a single row .CSV separated by a comma:

 

$paramExportCsv = @{    
Path
= 'C:\TEMP\Online-VMs-VCenterName.csv'
NoTypeInformation
= $true } Get-VMHost | Get-VM -PipelineVariable vm | Where-Object { $vm.PowerState -eq 'PoweredOn' } | Get-NetworkAdapter | Select-Object @{ N = 'VMHost'; E = { $vm.VMHost.Name } }, @{ N = 'vCPU'; E = { $vm.NumCpu } }, @{ N = 'vRAM'; E = { $vm.MemoryGB } }, @{ N = 'VM Name'; E = { $vm.Name } }, @{ N = 'Notes'; E = { $vm.Notes } }, @{ N = "IP Address(es)"; E = { $nic = $_ $ips = ($vm.Guest.Nics | Where-Object { $_.Device.Name -eq $nic.Name }).IPAddress ($ips | Where-Object { ([ipaddress]$_).AddressFamily -eq 'InterNetwork' }) -join ', ' } } | Export-Csv @paramExportCsv

 

The problem with the above IP addresses column is that it creates additional rows when the VMs have multiple vNICs or just attached vNIC without any IP address assigned.

Thank you in advance.

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Can you try something like this?

$paramExportCsv = @{    
    Path              = 'C:\TEMP\Online-VMs-VCenterName.csv'    
    NoTypeInformation = $true
    }
    
Get-VMHost |
Get-VM -PipelineVariable vm |
Where-Object { $vm.PowerState -eq 'PoweredOn' } |
ForEach-Object -Process {
    '' | Select-Object @{ N = 'VMHost'; E = { $vm.VMHost.Name } },
            @{ N = 'vCPU'; E = { $vm.NumCpu } },
            @{ N = 'vRAM'; E = { $vm.MemoryGB } },
            @{ N = 'VM Name'; E = { $vm.Name } },
            @{ N = 'Notes'; E = { $vm.Notes } },
            @{ N = "IP Address(es)"; E = {
                (Get-NetworkAdapter -VM $vm -PipelineVariable nic |
                ForEach-Object -Process {
                    $ips = ($vm.Guest.Nics | Where-Object { $_.Device.Name -eq $nic.Name }).IPAddress
                    $ips | Where-Object { ([ipaddress]$_).AddressFamily -eq 'InterNetwork' }
                }) -join ','
            }} 
    } | Export-Csv @paramExportCsv
    


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

View solution in original post

2 Replies
LucD
Leadership
Leadership

Can you try something like this?

$paramExportCsv = @{    
    Path              = 'C:\TEMP\Online-VMs-VCenterName.csv'    
    NoTypeInformation = $true
    }
    
Get-VMHost |
Get-VM -PipelineVariable vm |
Where-Object { $vm.PowerState -eq 'PoweredOn' } |
ForEach-Object -Process {
    '' | Select-Object @{ N = 'VMHost'; E = { $vm.VMHost.Name } },
            @{ N = 'vCPU'; E = { $vm.NumCpu } },
            @{ N = 'vRAM'; E = { $vm.MemoryGB } },
            @{ N = 'VM Name'; E = { $vm.Name } },
            @{ N = 'Notes'; E = { $vm.Notes } },
            @{ N = "IP Address(es)"; E = {
                (Get-NetworkAdapter -VM $vm -PipelineVariable nic |
                ForEach-Object -Process {
                    $ips = ($vm.Guest.Nics | Where-Object { $_.Device.Name -eq $nic.Name }).IPAddress
                    $ips | Where-Object { ([ipaddress]$_).AddressFamily -eq 'InterNetwork' }
                }) -join ','
            }} 
    } | Export-Csv @paramExportCsv
    


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

AlbertWT
Virtuoso
Virtuoso
Yes, it works great as expected, many thanks @LucD
/* Any kind of comment or input would be greatly appreciated */
0 Kudos