VMware Cloud Community
MehdiF
Enthusiast
Enthusiast
Jump to solution

Split Network Adapter and Mac Address in list

$vms= Get-VM 

foreach ($vm in $vms) {
    $vm |  
    foreach -Process {

        $vmNet = Get-NetworkAdapter -VM $vm
        ''| Select @{N="VM";E={$vm.Name}},
         
        @{N='Adapter';E={$vmNet.Type}},
        @{N="MAC Address:";E={$vm.Guest.Nics.MacAddress.Split("`n")[0]}},
        @{N='Guest OS' ; E={$vm.ExtensionData.Guest.GuestFullName}},
        @{N='Guest OS Version' ; E={$vm.ExtensionData.Config.GuestFullName}},
        @{N='Hardware Version' ; E={$vm.ExtensionData.Guest.HWversion}},
        @{N='VMware Tools State';E={$vm.ExtensionData.Guest.ToolsStatus}},
        @{N='VMware Tools Version';E={$vm.ExtensionData.Guest.ToolsVersion}},
        @{N='Managed By';E={$vm.CustomFields.Item('ManagedBy')}} | Format-Table -AutoSize

    }

}

 

I don't know split in columns values of Mac Address and Adapter like 

I tried several code with foreach or .Split but no success

VM | Adapter 1 | Adapter 2 | Adapter 3 | Mac Address 1 |Mac Address 2 | ......

Anyone knows this ? 

Thank you 🙂 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

This should skip the Net info for VMs that do not have VMware Tools installed.

 

$Report = New-Object System.Collections.ArrayList
Get-View -ViewType VirtualMachine -SearchRoot (Get-Cluster "MyCluster").id -PipelineVariable vm |
    ForEach-Object -Process {
        $VMReport = New-Object PSObject
        Add-Member -InputObject $VMReport -MemberType NoteProperty -Name Guest -Value $VM.Name
        Add-Member -InputObject $VMReport -MemberType NoteProperty -Name OSName -Value $($VM.Guest.GuestFullName)
        Add-Member -InputObject $VMReport -MemberType NoteProperty -Name MemoryMB -Value $VM.summary.config.MemorySizeMB
        Add-Member -InputObject $VMReport -MemberType NoteProperty -Name VmDatastore -Value $VM.summary.Config.VmPathName.split()[0]
        if ($vm.Guest -ne $null -and $vm.Guest.Net -ne $null) {
            $i = 0
            $vm.Guest.Net | ForEach-Object -Process {
                Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $_.Network
                Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $_.Macaddress
                Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value ($_.IpAddress.Where{ $_ -like "*.*" } -join '|')
                $i++
            }
        }
        $Report.add($VMReport) | Out-Null
    }

$Report | Sort-Object -Property { ($_ | Get-Member -MemberType Property).Count } -Descending |
    Export-Csv -Path c:\Scripts\Output\vmreport.csv -UseCulture -NoTypeInformation

 


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

View solution in original post

0 Kudos
24 Replies
LucD
Leadership
Leadership
Jump to solution

You could try something like Re: vm report on vnics, ips, & vnic macs - VMware Technology Network VMTN or Solved: Re: Displaying VM IP addresses in single row - VMware Technology Network VMTN
The 1st one creates a separate column for each MAC and vNIC name, the 2nd joins the values for MAC in 1 column.


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

0 Kudos
wetnose88
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

Tried the https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/vm-report-on-vnics-ips-vnic-macs/m-p/8... the script run OK, but all the split columns are empty without any IP address reported.

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

To make sure, which code do you mean when you say "the split columns"?
Are these columns blank in the CSV or also in the $report variable?


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

0 Kudos
wetnose88
Enthusiast
Enthusiast
Jump to solution

$Report = New-Object System.Collections.ArrayList 
$VMS = Get-View -ViewType VirtualMachine -SearchRoot (get-cluster "MyCluster").id |
 Sort-Object -Property {
  $_.Config.Hardware.Device |
  where {$_ -is [VMware.Vim.VirtualEthernetCard]} |
  Measure-Object | select -ExpandProperty Count} -Descending 

foreach($VM in $VMS){
  $VMReport = New-Object PSObject
  Add-Member -InputObject $VMReport -MemberType NoteProperty -Name Guest -Value $VM.Name
  Add-Member -InputObject $VMReport -MemberType NoteProperty -Name OSName -Value $($VM.Guest.GuestFullName)
  Add-Member -InputObject $VMReport -MemberType NoteProperty -Name MemoryMB -Value $VM.summary.config.MemorySizeMB
  Add-Member -InputObject $VMReport -MemberType NoteProperty -Name VmDatastore -Value $VM.summary.Config.VmPathName.split()[0]
  $networkcards = &{  
    $vm.Config.Hardware.Device |
    where {$_ -is [VMware.Vim.VirtualEthernetCard]} |
    Measure-Object | select -ExpandProperty Count
  }
  for($i=0;$i -lt $networkcards;$i++){
    Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $ntwkcard.Network
    Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $ntwkcard.Macaddress
    Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value $($ntwkcard.IpAddress|?{$_-like "*.*"})
    Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.Device" -Value (&{
      $nic = $VM.config.hardware.device | Where{$_.key -eq $($ntwkcard.DeviceConfigId)}
      if($nic){
        $nic.GetType().Name
      }
    })
  }
  $Report.add($VMReport)|Out-Null
}
$Report | Export-Csv c:\Scripts\Output\vmreport.csv 

The code I used are as above from one of your post, both $Report and csv file the 

"Guest","OSName","MemoryMB","VmDatastore","networkcard0.Network","networkcard0.MacAddress","networkcard0.IpAddress","networkcard0.Device","networkcard1.Network","networkcard1.MacAddress","networkcard1.IpAddress","networkcard1.Device","networkcard2.Network","networkcard2.MacAddress","networkcard2.IpAddress","networkcard2.Device","networkcard3.Network","networkcard3.MacAddress","networkcard3.IpAddress","networkcard3.Device"
"test01","Red Hat Enterprise Linux 7 (64-bit)","32768","[PR01_VDI_UCS_VMFS7_nobackup]",,,,,,,,,,,,,,,,
"test02","VMware Photon OS (64-bit)","19456","[PR01_VDI_UCS_VMFS2]",,,,,,,,,,,,,,,,

......

 

0 Kudos
wetnose88
Enthusiast
Enthusiast
Jump to solution

The following columns are all normal and correct

GuestOSNameMemoryMBVmDatastore

The following columns are empty

networkcard0.Networknetworkcard0.MacAddressnetworkcard0.IpAddressnetworkcard0.Devicenetworkcard1.Networknetworkcard1.MacAddressnetworkcard1.IpAddressnetworkcard1.Devicenetworkcard2.Networknetworkcard2.MacAddressnetworkcard2.IpAddressnetworkcard2.Devicenetworkcard3.Networknetworkcard3.MacAddressnetworkcard3.IpAddressnetworkcard3.Device
0 Kudos
MehdiF
Enthusiast
Enthusiast
Jump to solution

Hello, 

I confirm that columns "MacAdresse, IPAddress..." are empty.

however in my script i have a issue when i do Export-csv. There is only one line in my csv? But when I display in powershell I get all the lines

 

Thank you 🙂 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try this updated version.

$Report = New-Object System.Collections.ArrayList
Get-View -ViewType VirtualMachine -SearchRoot (Get-Cluster "MyCluster").id -PipelineVariable vm |
Sort-Object -Property {
	$_.Config.Hardware.Device |
	Where-Object { $_ -is [VMware.Vim.VirtualEthernetCard] } |
	Measure-Object | Select-Object -ExpandProperty Count } -Descending |
ForEach-Object -Process {
	$VMReport = New-Object PSObject
	Add-Member -InputObject $VMReport -MemberType NoteProperty -Name Guest -Value $VM.Name
	Add-Member -InputObject $VMReport -MemberType NoteProperty -Name OSName -Value $($VM.Guest.GuestFullName)
	Add-Member -InputObject $VMReport -MemberType NoteProperty -Name MemoryMB -Value $VM.summary.config.MemorySizeMB
	Add-Member -InputObject $VMReport -MemberType NoteProperty -Name VmDatastore -Value $VM.summary.Config.VmPathName.split()[0]
	$networkcards = $vm.Config.Hardware.Device | Where-Object { $_ -is [VMware.Vim.VirtualEthernetCard]}
		for ($i = 0; $i -lt $networkcards.Count; $i++) {
		$guestNic = $vm.Guest.Net | Where-Object { $_.MacAddress -eq $networkcards[$i].Macaddress }
		Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $guestNic.Network
		Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $guestNic.Macaddress
			Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value ($guestNic.IpAddress.Where{$_ -like "*.*"} -join'|')
			Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.Device" -Value $networkcards[$i].GetType().Name.Replace("Virtual",'')
		}
		$Report.add($VMReport) | Out-Null
	}

$Report | Export-Csv c:\Scripts\Output\vmreport.csv


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

0 Kudos
wetnose88
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

It work differently, the whole content is one VM's information

"Guest","OSName","MemoryMB","VmDatastore","networkcard0.Network","networkcard0.MacAddress","networkcard0.IpAddress","networkcard0.Device"
"MSW10-QTP43","Microsoft Windows 10 (64-bit)","8192","[T23PR01_VDI_UCS_VMFS7_nobackup]","mylab-VDI_172-23-162-1_24","00:50:56:99:40:fb","192.23.162.123","Vmxnet3"
"MSW10-QTP43","Microsoft Windows 10 (64-bit)","8192","[T23PR01_VDI_UCS_VMFS7_nobackup]","mylab-VDI_172-23-162-1_24","00:50:56:99:40:fb","192.23.162.123","Vmxnet3"
"MSW10-QTP43","Microsoft Windows 10 (64-bit)","8192","[T23PR01_VDI_UCS_VMFS7_nobackup]","mylab-VDI_172-23-162-1_24","00:50:56:99:40:fb","192.23.162.123","Vmxnet3"
.....

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Wasn't that the idea behind the original question?


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

0 Kudos
MehdiF
Enthusiast
Enthusiast
Jump to solution

Thank you for your feed back 🙂 

The original idea is : 

list all vms with all Mac Address and Adapter type in differents columns for this.

 

VM | Adapter 1 |Adpater 2 |Mac Address 1 |Mac Adress 2 |Guest OS | Guest OS version | HArdware version | VMware tools State | ....

 

With the script I started with, I can get what I want, but I'm faced with two problems

1st, the "Adapter type" and "MAc address" information are in a single column

the second is that when I use | export-csv, it returns a single line of information in the csv.

 

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you check what the Delimiter for a CSV is?
There could be a difference between what Export-Csv uses and what Excel uses.
Did you use the -UseCulture switch on the Export-Csv cmdlet?


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

0 Kudos
MehdiF
Enthusiast
Enthusiast
Jump to solution

Yes i used option -Useculture but only one line in result in csv

i set the option here 

$vms= Get-VM 

foreach ($vm in $vms) {
    $vm |  
    foreach -Process {

        $vmNet = Get-NetworkAdapter -VM $vm
        ''| Select @{N="VM";E={$vm.Name}},
         
        @{N='Adapter';E={$vmNet.Type}},
        @{N="MAC Address:";E={$vm.Guest.Nics.MacAddress}},
        @{N='Guest OS' ; E={$vm.ExtensionData.Guest.GuestFullName}},
        @{N='Guest OS Version' ; E={$vm.ExtensionData.Config.GuestFullName}},
        @{N='Hardware Version' ; E={$vm.ExtensionData.Guest.HWversion}},
        @{N='VMware Tools State';E={$vm.ExtensionData.Guest.ToolsStatus}},
        @{N='VMware Tools Version';E={$vm.ExtensionData.Guest.ToolsVersion}},
        @{N='Managed By';E={$vm.CustomFields.Item('ManagedBy')}} | Export-Csv -Path <path> -UseCulture

    }  

} 

 

I tried to set the parameter elsewhere but without success.

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

By placing the Export-Csv inside the loop, you are overwriting the CSV on each run through the loop.

Btw, that is not the code I posted yesterday


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

0 Kudos
MehdiF
Enthusiast
Enthusiast
Jump to solution

in the script that you shared yesterday, value of "Guest" is vcenter name. so i don't see information bt VM

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That sounds highly unlikely since I do seem to get the results.
What code are you actually running?
And how are you running that code?


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

0 Kudos
MehdiF
Enthusiast
Enthusiast
Jump to solution

The last code that you posted 

 

$Report = New-Object System.Collections.ArrayList
Get-View -ViewType VirtualMachine -SearchRoot (Get-Cluster "MyCluster").id -PipelineVariable vm |
Sort-Object -Property {
	$_.Config.Hardware.Device |
	Where-Object { $_ -is [VMware.Vim.VirtualEthernetCard] } |
	Measure-Object | Select-Object -ExpandProperty Count } -Descending |
ForEach-Object -Process {
	$VMReport = New-Object PSObject
	Add-Member -InputObject $VMReport -MemberType NoteProperty -Name Guest -Value $VM.Name
	Add-Member -InputObject $VMReport -MemberType NoteProperty -Name OSName -Value $($VM.Guest.GuestFullName)
	Add-Member -InputObject $VMReport -MemberType NoteProperty -Name MemoryMB -Value $VM.summary.config.MemorySizeMB
	Add-Member -InputObject $VMReport -MemberType NoteProperty -Name VmDatastore -Value $VM.summary.Config.VmPathName.split()[0]
	$networkcards = $vm.Config.Hardware.Device | Where-Object { $_ -is [VMware.Vim.VirtualEthernetCard]}
		for ($i = 0; $i -lt $networkcards.Count; $i++) {
		$guestNic = $vm.Guest.Net | Where-Object { $_.MacAddress -eq $networkcards[$i].Macaddress }
		Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $guestNic.Network
		Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $guestNic.Macaddress
			Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value ($guestNic.IpAddress.Where{$_ -like "*.*"} -join'|')
			Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.Device" -Value $networkcards[$i].GetType().Name.Replace("Virtual",'')
		}
		$Report.add($VMReport) | Out-Null
	}

$Report | Export-Csv c:\Scripts\Output\vmreport.csv

 

i copy / past this code in powershell ise and i execute this with powershell. 

i always do that 🙂 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, can you try the following variation?

$Report = New-Object System.Collections.ArrayList
Get-View -ViewType VirtualMachine -SearchRoot (Get-Cluster "MyCluster").id -PipelineVariable vm |
ForEach-Object -Process {
	$VMReport = New-Object PSObject
	Add-Member -InputObject $VMReport -MemberType NoteProperty -Name Guest -Value $VM.Name
	Add-Member -InputObject $VMReport -MemberType NoteProperty -Name OSName -Value $($VM.Guest.GuestFullName)
	Add-Member -InputObject $VMReport -MemberType NoteProperty -Name MemoryMB -Value $VM.summary.config.MemorySizeMB
	Add-Member -InputObject $VMReport -MemberType NoteProperty -Name VmDatastore -Value $VM.summary.Config.VmPathName.split()[0]
	$i = 0
	$vm.Guest.Net | ForEach-Object -Process {
		Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $_.Network
		Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $_.Macaddress
		Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value ($_.IpAddress.Where{ $_ -like "*.*" } -join '|')
		Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.Device" -Value $networkcards[$i].GetType().Name.Replace("Virtual", '')
		$i++
	}
	$Report.add($VMReport) | Out-Null
}

$Report | Sort-Object -Property {($_ | Get-Member -MemberType Property).Count} -Descending |
Export-Csv -Path c:\Scripts\Output\vmreport.csv -UseCulture -NoTypeInformation


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

0 Kudos
MehdiF
Enthusiast
Enthusiast
Jump to solution

there is an error when i lauch script : 

MehdiF_0-1686920454823.png

if i comment the line 14, i don't have error message but i don't have 'Adapter type'.

I'm still trying to find a solution

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you have VMs with no vNIC?
Or VMs that do not have the VMware Tools installed?


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

0 Kudos