VMware Cloud Community
AlbertWT
Virtuoso
Virtuoso
Jump to solution

Displaying VM IP addresses in single row

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.

/* Please feel free to provide any comments or input you may have. */
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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
Jump to solution

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
Jump to solution

Yes, it works great as expected, many thanks @LucD
/* Please feel free to provide any comments or input you may have. */
Reply
0 Kudos