VMware Cloud Community
dbc1
Contributor
Contributor

How to get Mac Address

Hi, I am trying to receive Mac Address but I cannot find a way to get the correct mac address for the right NIC...

Here is the script I am currently working on

 

 

 

$VMS = gc C:\temp\servers2.txt
foreach($vm in $vms) {
Get-VirtualPortGroup -vm $vm|

Select @{n="VMName";e={$vm}},Name,

    @{N='VlanId';E={
        if($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]){
            if($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]){
                $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
            }
            elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]){
                if($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]){
                    [string]::Join(',',($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | %{"$($_.Start)-$($_.End)"}))
                }
                else{
                    $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
                }
            }
        }
        else{$_.VlanId}}},
        
    @{N='MAC';E={$_.Guest.Nics.Macaddress -join '|'}}

} 

 

 

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership

To use the Guest property you have to have an object as returned by Get-VM,
Try like this

$VMS = Get-Content C:\temp\servers2.txt

Get-VM -Name $vms -PipelineVariable vm |
ForEach-Object -Process {
  Get-VirtualPortGroup -VM $vm |
  Select-Object @{n = "VMName"; e = { $vm } }, Name,
  @{N = 'VlanId'; E = {
      if ($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
        if ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]) {
          $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
        } elseif ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]) {
          if ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]) {
            [string]::Join(',', ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | ForEach-Object { "$($_.Start)-$($_.End)" }))
          } else {
            $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
          }
        }
      } else { $_.VlanId } }
  },
  @{N = 'MAC'; E = { $vm.Guest.Nics.Macaddress -join '|' } }
}


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

dbc1
Contributor
Contributor

Thank you for the reply @LucD  right now it returns to me all the MAC Addressess, is it posible to return only the MAC Address that it's assigned to the specific Adapter? This is what I get right now:


Server01 Adapter1   1234      00:50:56:bd:ff:ec|00:50:56:bd:56:70
Server01 Adapter2   2000   00:50:56:bd:ff:ec|00:50:56:bd:56:70
Server02 Adapter1   4321     00:50:56:bd:d5:09|00:50:56:bd:4b:a9
Server02 Adapter2   1000   00:50:56:bd:d5:09|00:50:56:bd:4b:a9

 

something like it's what I want to receive

Server01 Adapter1   1234      00:50:56:bd:56:70
Server01 Adapter2   2000   00:50:56:bd:ff:ec
Server02 Adapter1   4321     00:50:56:bd:d5:09
Server02 Adapter2   1000   00:50:56:bd:4b:a9

Reply
0 Kudos
LucD
Leadership
Leadership

That requires some changes to your code.

$VMS = Get-Content C:\temp\servers2.txt

Get-VM -Name $vms -PipelineVariable vm |
ForEach-Object -Process {
  Get-VirtualPortGroup -VM $vm -PipelineVariable pg |
  Select-Object @{n = "VMName"; e = { $vm } }, Name,
  @{N = 'VlanId'; E = {
      if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
        if ($pg.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]) {
          $pg.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
        } elseif ($pg.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]) {
          if ($pg.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]) {
            [string]::Join(',', ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | ForEach-Object { "$($_.Start)-$($_.End)" }))
          } else {
            $pg.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
          }
        }
      } else { $pg.VlanId } }
  },
  @{N = 'MAC'; E = { ($vm.Guest.Nics | where{$_.NetworkName -eq $pg.Name}).Macaddress  -join '|' } }
}


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

dbc1
Contributor
Contributor

Now doesn't show the MAC at all 😄 . But it's ok, I will see what I can do with previous command.

Reply
0 Kudos
LucD
Leadership
Leadership

It works for me, but if the VMware Tools have not completed the NIC information, it could be that the NetworkName is not filled in.
Check what is in $vm.guest.nics for some of the VMs in your file.


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

Reply
0 Kudos
rwk1982
Enthusiast
Enthusiast

Hello!

This one is years old and not the fastest but i was always to lazy to optimize it 🙂

$vms = Get-Content c:\temp\vms.txt

Get-VM $vms | foreach {

	$vm = $_
	$vm_name = $vm.Name

	$vm | Get-NetworkAdapter | foreach {

		$vm_nic = $_
		$vm_nic_name = $vm_nic.Name
		$vm_nic_net_name = $vm_nic.NetworkName
		$vm_nic_mac = $vm_nic.MacAddress

		$vm_nic_vlanid = (Get-VDPortgroup -NetworkAdapter $vm_nic).VlanConfiguration.VlanId

		if (!$vm_nic_vlanid) {

			$vm_nic_vlanid = ($vm | Get-VirtualPortGroup -Name $vm_nic_net_name)[0].VLanId

		}


		$vm_name + "|" + $vm_nic_name + "|" + $vm_nic_net_name + "|" + $vm_nic_vlanid + "|" + $vm_nic_mac

	}

}
Drink coffee.. Do stupid things faster with more energy...
LucD
Leadership
Leadership

Nice, but you do realise that your code will not work for a Private VLAN or when VLAN Trunking is used.


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

Reply
0 Kudos