Automation

 View Only
Expand all | Collapse all

Get List of IP addresses for each VM

antoniogemelli

antoniogemelliAug 25, 2017 10:01 AM

  • 1.  Get List of IP addresses for each VM

    Posted Aug 25, 2017 08:00 AM

    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



  • 2.  RE: Get List of IP addresses for each VM
    Best Answer

    Posted Aug 25, 2017 09:06 AM

    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



  • 3.  RE: Get List of IP addresses for each VM

    Posted Aug 25, 2017 10:01 AM

    Thanks a lot LucD



  • 4.  RE: Get List of IP addresses for each VM

    Posted Nov 10, 2017 10:34 AM

    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



  • 5.  RE: Get List of IP addresses for each VM

    Posted Nov 10, 2017 10:38 PM

    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



  • 6.  RE: Get List of IP addresses for each VM

    Posted Nov 13, 2017 08:46 AM

    This give me only an empty column, no result.



  • 7.  RE: Get List of IP addresses for each VM

    Posted Nov 13, 2017 01:07 PM

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



  • 8.  RE: Get List of IP addresses for each VM

    Posted Jul 13, 2018 01:00 PM

    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!



  • 9.  RE: Get List of IP addresses for each VM

    Posted Jul 13, 2018 01:11 PM

    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



  • 10.  RE: Get List of IP addresses for each VM

    Posted Jun 17, 2019 02:02 PM

    @LucD - very helpful! Thanks!



  • 11.  RE: Get List of IP addresses for each VM

    Posted Sep 29, 2021 07:42 PM

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

    Thank you



  • 12.  RE: Get List of IP addresses for each VM

    Posted Sep 29, 2021 08:11 PM

    Yes, just add a calculated property

     



  • 13.  RE: Get List of IP addresses for each VM

    Posted Sep 29, 2021 09:18 PM

    Tried the below script but got an empty MAC column. Am I missing something here?

    Get-VM | Select Name,VMHost, @{N='MAC';E={$_.MacAddress}}, @{N="IP Address";E={@($_.guest.IPAddress[0])}} |
    Export-Csv -NoTypeInformation C:\PS\1machine1_ip.csv

    Also, is it possible to get every IP/MAC assigned to the farm machines?

    Thank you,



  • 14.  RE: Get List of IP addresses for each VM

    Posted Sep 29, 2021 09:33 PM

    I was referring to this script



  • 15.  RE: Get List of IP addresses for each VM

    Posted Sep 30, 2021 09:41 AM

    This one worked just fine! Thanks a lot, once again!



  • 16.  RE: Get List of IP addresses for each VM

    Posted Oct 19, 2022 05:30 PM

    I know this was made awhile back but i was hoping you could help out again. Could you could remove the host column, add the Guest OS Type, and Powered on State? The rest is working wonderfully but I need those columns for a report I'm building.  Your original script that added the IP's into their own columns was almost perfect for what I needed.

    $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 D:\Powershell\Logs\TestIP_1.csv -NoTypeInformation -UseCulture



  • 17.  RE: Get List of IP addresses for each VM

    Posted Oct 19, 2022 05:48 PM

    Try like this



  • 18.  RE: Get List of IP addresses for each VM

    Posted Oct 19, 2022 05:54 PM

    That is awesome. I was looking all over for those 2 commands. Is it possible for guest name to be the FQDN?



  • 19.  RE: Get List of IP addresses for each VM

    Posted Oct 19, 2022 06:31 PM

    Sure, try like this



  • 20.  RE: Get List of IP addresses for each VM

    Posted Oct 19, 2022 06:39 PM

    Thank you Very much!!



  • 21.  RE: Get List of IP addresses for each VM

    Posted Jun 30, 2020 02:08 PM
    How do I modify this script to list VM tag name for a certain Category?


  • 22.  RE: Get List of IP addresses for each VM

    Posted Jun 30, 2020 03:09 PM

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

    Note that this property will be repeated for each IP address



  • 23.  RE: Get List of IP addresses for each VM

    Posted Jun 30, 2020 03:39 PM

    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



  • 24.  RE: Get List of IP addresses for each VM

    Posted Jun 30, 2020 03:53 PM

    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



  • 25.  RE: Get List of IP addresses for each VM

    Posted Jun 30, 2020 04:30 PM

    Exactly what I needed, thanks LucD@



  • 26.  RE: Get List of IP addresses for each VM

    Posted Aug 13, 2020 04:06 PM
    What should be added to this script in order to get the network the VM is attached to?


  • 27.  RE: Get List of IP addresses for each VM

    Posted Aug 13, 2020 04:35 PM

    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



  • 28.  RE: Get List of IP addresses for each VM

    Posted Aug 13, 2020 04:53 PM

    Absolutely brilliant, thank you!



  • 29.  RE: Get List of IP addresses for each VM

    Posted Aug 13, 2020 09:55 PM

    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



  • 30.  RE: Get List of IP addresses for each VM

    Posted May 22, 2023 01:43 PM

    hi  
    I tried:

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

    and all other programs given below but none seems to be working, probably because Guest field is empty for my VM output.
    I have to take these files and further I can export them to csv. Exporting to CSV, I know how it's done but all these vm files I am getting only name but No IP Address.

    Sample Output or Get-Vm | Format-List in Powershell ISE
    Name : VM_5360
    PowerState : PoweredOn
    Notes :
    Guest :
    NumCpu : 1
    CoresPerSocket : 1
    MemoryMB : 4096
    MemoryGB : 4
    VMHostId :
    VMHost :
    VApp :
    FolderId :
    Folder :
    ResourcePoolId :
    ResourcePool :
    HARestartPriority : ClusterRestartPriority
    HAIsolationResponse : AsSpecifiedByCluster
    DrsAutomationLevel : AsSpecifiedByCluster
    VMSwapfilePolicy :
    VMResourceConfiguration : CpuShares:Normal/1000 MemShares:Normal/40960
    Version :
    PersistentId :
    GuestId :
    UsedSpaceGB :
    ProvisionedSpaceGB :
    DatastoreIdList :
    ExtensionData : VMware.Vim.VirtualMachine
    CustomFields : {}
    Id : VirtualMachine-vm-1893
    Uid : /VIServer=vsphere.local\administrator@100.100.10.97:443/VirtualMachine=VirtualMachine-vm-1893/


    Powershell Version: 

    PS C:\Users\Administrator> $PSVersionTable

    Name Value
    ---- -----
    PSVersion 5.1.20348.1366
    PSEdition Desktop
    PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
    BuildVersion 10.0.20348.1366
    CLRVersion 4.0.30319.42000
    WSManStackVersion 3.0
    PSRemotingProtocolVersion 2.3
    SerializationVersion 1.1.0.1

    And PowerCLI version:

    PS C:\Users\Administrator> gET-pOWERclivERSION
    WARNING: The cmdlet "Get-PowerCLIVersion" is deprecated. Please use the 'Get-Module' cmdlet instead.

    PowerCLI Version
    ----------------
    VMware PowerCLI 10.0.0 build 7895300
    ---------------
    Component Versions
    ---------------
    VMware Cis Core PowerCLI Component PowerCLI Component 10.0 build 7893915
    VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 10.0 build 7893909

     


    PS C:\Users\Administrator> $Global:DefaultVIServers | select Name, Version, Build

    Name Version Build
    ---- ------- -----
    100.100.10.97 8.0.1 21201873


    ESXi Being Used on which VM are hosted: 7.0.3 (1e)


     



  • 31.  RE: Get List of IP addresses for each VM

    Posted May 22, 2023 01:50 PM

    If the VMware Tools are not installed there will be no info under the Guest property.



  • 32.  RE: Get List of IP addresses for each VM

    Posted May 24, 2023 10:03 AM

    Thanks  ,
    I updated VmWareTools and it now works fine for me now.
    Keep shining