VMware Cloud Community
antoniogemelli
Hot Shot
Hot Shot
Jump to solution

Get List of IP addresses for each VM

Hello,

I have a script to get Ip address and vm name:

Get-VM | Select Name,VMHost, @{N="IP Address";E={@($_.guest.IPAddress[0])}} |

Export-Csv -NoTypeInformation C:\Users\gemela\Desktop\machine_ip.csv

I get only one IP, I want to get also management ip and backup ip.

Is possible? ideally will be nice to get all IP for each machine.

Thanks

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can join the elements of the array together in one string.

Like this

Get-VM |

Select Name,VMHost, @{N="IP Address";E={@($_.guest.IPAddress -join '|')}} |

Export-Csv -NoTypeInformation C:\Users\gemela\Desktop\machine_ip.csv


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

View solution in original post

31 Replies
LucD
Leadership
Leadership
Jump to solution

You can join the elements of the array together in one string.

Like this

Get-VM |

Select Name,VMHost, @{N="IP Address";E={@($_.guest.IPAddress -join '|')}} |

Export-Csv -NoTypeInformation C:\Users\gemela\Desktop\machine_ip.csv


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

antoniogemelli
Hot Shot
Hot Shot
Jump to solution

Thanks a lot LucD

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot
Jump to solution

Hi LucD

With this script I got all IP for each VM,

But when generating csv all of them are in one line (separated with pipe)

Is possible to configure the script to get 3 different column?

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

$report = foreach($vm in Get-VM){

    $obj = [ordered]@{

        Name = $vm.Name

        Host = $vm.VMHost.Name

    }

    $i = 1

    $vm.Guest.IPAddress | %{

        $obj.Add("IP$($i)",$_)

        $i++

    }

    New-Object PSObject -Property $obj

}

$report | Sort-Object -Property {($_ | Get-Member -MemberType Properties).Count} -Descending |

Export-Csv C:\Users\gemela\Desktop\machine_ip.csv -NoTypeInformation -UseCulture


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

antoniogemelli
Hot Shot
Hot Shot
Jump to solution

This give me only an empty column, no result.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There were some typos in there, I corrected the code above.


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

PDpd
Contributor
Contributor
Jump to solution

Hi

Can you also suggest getting all IPs in separate rows and not columns? If a server has 3 IPs,  I will ike to see a report with 3 rows and not 3 columns. 

Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, try like this

$report = foreach($vm in Get-VM){

    foreach($ip in $vm.Guest.IPAddress){

        $obj = [ordered]@{

            Name = $vm.Name

            Host = $vm.VMHost.Name

            IP = $ip

        }

        New-Object PSObject -Property $obj

    }

}

$report | Sort-Object -Property {($_ | Get-Member -MemberType Properties).Count} -Descending |

Export-Csv C:\Users\gemela\Desktop\machine_ip.csv -NoTypeInformation -UseCulture


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

wowitsdave
Enthusiast
Enthusiast
Jump to solution

@LucD - very helpful! Thanks!

Reply
0 Kudos
mcity
Contributor
Contributor
Jump to solution

How do I modify this script to list VM tag name for a certain Category?
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Add a property with the result of the Get-TagAssignment cmdlet.

Note that this property will be repeated for each IP address


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

Reply
0 Kudos
mcity
Contributor
Contributor
Jump to solution

I am using the script below, where do I add the Get-Assignment for tag Category Backup, for example?

Get-VM |

Select Name,VMHost, @{N="IP Address";E={@($_.guest.IPAddress -join '|')}} |

Export-Csv -NoTypeInformation C:\Users\gemela\Desktop\machine_ip.csv

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this

Get-VM |

Select Name,VMHost,

    @{N="IP Address";E={@($_.guest.IPAddress -join '|')}},

    @{N='Tag';E={(Get-TagAssignment -Entity $_ -Category 'Backup').Tag.Name}} |

Export-Csv -NoTypeInformation C:\Users\gemela\Desktop\machine_ip.csv


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

Reply
0 Kudos
mcity
Contributor
Contributor
Jump to solution

Exactly what I needed, thanks LucD@

Reply
0 Kudos
ad_astra_777
Contributor
Contributor
Jump to solution

What should be added to this script in order to get the network the VM is attached to?
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this

Get-VM -PipelineVariable VM | ForEach-Object -Process {

    $vm.Guest.Nics |

    Select @{N='VM';E={$vm.Name}},

        @{N='VMHost';E={$vm.VMHost.Name}},

        @{N='Network';E={$_.NetworkName}},

        @{N="IP Address";E={$_.IPAddress -join '|'}}

} | Export-Csv -NoTypeInformation C:\Users\gemela\Desktop\machine_ip.csv


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

Reply
0 Kudos
ad_astra_777
Contributor
Contributor
Jump to solution

Absolutely brilliant, thank you!

Reply
0 Kudos
erojasmu
Contributor
Contributor
Jump to solution

Hi. 

Thank you for your code. It works like a charm. I was wondering if you can try to convert it from the "Get-VM" cmdlet to "Get-View". I was trying but I got stuck at some point.

Thank you in advance for your suggestions.

Hernan

Reply
0 Kudos
murugan071
Contributor
Contributor
Jump to solution

Is it possible to get MAC address too with IP details here?

Thank you

Tags (1)
Reply
0 Kudos