VMware Cloud Community
AGFlora
Enthusiast
Enthusiast
Jump to solution

Get VM IP and MAC

Hi

I'm trying to get the vm IP and MAC from one of my datacenters.

     When I run the following script two things happen:

1. I only see one vm in the csv file output.

2. When I send the output to the PowrCLI prompt I only see one mac and one IP for vm's that have to nics.

Script

---------

$VMs = Get-Datacenter $DC | get-vmhost | Get-VM

foreach ($VM in $VMs){

    $VMx = Get-View $VM.ID

     $HW = $VMx.guest.net

     foreach ($dev in $HW)

    {

        foreach ($ip in $dev.ipaddress)

        {

            $dev | select

            @{Name = "Name"; Expression = {$vm.name}},

            @{Name = "IP"; Expression = {$ip}},

            @{Name = "MAC"; Expression = {$dev.macaddress}}  |

            Export-CSV VM-IP-Info.csv -NoTypeInfo

           

 

        }

    }

}

Reply
0 Kudos
1 Solution

Accepted Solutions
EKardinal
Enthusiast
Enthusiast
Jump to solution

My console output, tested on one VM with two external nics and a internal 'dummy' interface

Name                               IP                                  MAC

----                                   --                                    ---

VM1                                192.168.1.1                     00:50:56:xx:xx:xx

VM1                                192.168.2.1                     00:50:56:xx:xx:xx

VM1                                192.x.x.xxx                     xx:xx:xx:xx:xx:xx

Do you see any of the expected addresses in the VM summary using vSphere Client?

Ah sorry, I forgot about the PS 3.0 requirement.

You could put every entry into an array and export the whole at the end of the script.

     $out = @()
     $VMs = Get-Datacenter $DC | get-vmhost | Get-VM
     foreach ($VM in $VMs) {
        
$VMx = Get-View $VM.ID
        
$HW = $VMx.guest.net
        
foreach ($dev in $HW)
         {
            
foreach ($ip in $dev.ipaddress)
             {
                
$out += $dev | select @{Name = "Name"; Expression = {$vm.name}}, @{Name = "IP"; Expression = {$ip}}, @{Name = "MAC"; Expression = {$dev.macaddress}}
             }
         }
     }

     $out | Export-Csv -NoTypeInformation -Path "VM-IP-Info.csv"

Regards

Emanuel

View solution in original post

Reply
0 Kudos
7 Replies
EKardinal
Enthusiast
Enthusiast
Jump to solution

Hi,

try it with the -Append param on Export-CSV

http://technet.microsoft.com/en-us/library/hh849932.aspx

I got the correct output, when I put your whole select into one single line. Did you forgot the single backtick on the first line $dev | select ` ?

Are you sure the other nic has an IP assigned and is enabled in the GuestOS? If it's not, it won't even run into the third foreach.

Regards

Emanuel

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Did the output that you received have multiple MACS and IP's?

My output only shows 1 mac and 1 IP address when I know for sure that some vm OS have 2 and 3 nics.

For some reason when I use the Out-File cmdlet I get all of the output...strange

I cannot use -Append for the export-csv cmdlet I'm using PS 2.0

Regards,


Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Try to move the piping to Export-CSV to the last line of the script.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

I tried that but got the "an empty pipe line is not allowed" error.

Thanks

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can solve the empty pipe by putting & { before the foreach and an extra } at the end before the pipe.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
EKardinal
Enthusiast
Enthusiast
Jump to solution

My console output, tested on one VM with two external nics and a internal 'dummy' interface

Name                               IP                                  MAC

----                                   --                                    ---

VM1                                192.168.1.1                     00:50:56:xx:xx:xx

VM1                                192.168.2.1                     00:50:56:xx:xx:xx

VM1                                192.x.x.xxx                     xx:xx:xx:xx:xx:xx

Do you see any of the expected addresses in the VM summary using vSphere Client?

Ah sorry, I forgot about the PS 3.0 requirement.

You could put every entry into an array and export the whole at the end of the script.

     $out = @()
     $VMs = Get-Datacenter $DC | get-vmhost | Get-VM
     foreach ($VM in $VMs) {
        
$VMx = Get-View $VM.ID
        
$HW = $VMx.guest.net
        
foreach ($dev in $HW)
         {
            
foreach ($ip in $dev.ipaddress)
             {
                
$out += $dev | select @{Name = "Name"; Expression = {$vm.name}}, @{Name = "IP"; Expression = {$ip}}, @{Name = "MAC"; Expression = {$dev.macaddress}}
             }
         }
     }

     $out | Export-Csv -NoTypeInformation -Path "VM-IP-Info.csv"

Regards

Emanuel

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

That worked...Thanks!

Reply
0 Kudos