<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to get Mac Address in VMware PowerCLI Discussions</title>
    <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2959898#M111277</link>
    <description>&lt;P&gt;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.&lt;BR /&gt;Check what is in&amp;nbsp;&lt;STRONG&gt;$vm.guest.nics&lt;/STRONG&gt; for some of the VMs in your file.&lt;/P&gt;</description>
    <pubDate>Sun, 19 Mar 2023 12:05:28 GMT</pubDate>
    <dc:creator>LucD</dc:creator>
    <dc:date>2023-03-19T12:05:28Z</dc:date>
    <item>
      <title>How to get Mac Address</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2959857#M111264</link>
      <description>&lt;P&gt;Hi, I am trying to receive Mac Address but I cannot find a way to get the correct mac address for the right NIC...&lt;/P&gt;&lt;P&gt;Here is the script I am currently working on&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;$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 '|'}}

} &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Mar 2023 11:51:18 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2959857#M111264</guid>
      <dc:creator>dbc1</dc:creator>
      <dc:date>2023-03-19T11:51:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to get Mac Address</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2959861#M111267</link>
      <description>&lt;P&gt;To use the Guest property you have to have an object as returned by Get-VM,&lt;BR /&gt;Try like this&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;$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 '|' } }
}
&lt;/LI-CODE&gt;</description>
      <pubDate>Sat, 18 Mar 2023 21:44:40 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2959861#M111267</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2023-03-18T21:44:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to get Mac Address</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2959865#M111268</link>
      <description>&lt;P&gt;Thank you for the reply&amp;nbsp;&lt;a href="https://communities.vmware.com/t5/user/viewprofilepage/user-id/256147"&gt;@LucD&lt;/a&gt;&amp;nbsp; 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:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Server01 Adapter1&amp;nbsp; &amp;nbsp;1234&amp;nbsp; &amp;nbsp; &amp;nbsp; 00:50:56:bd:ff:ec|00:50:56:bd:56:70&lt;BR /&gt;Server01 Adapter2&amp;nbsp; &amp;nbsp;2000&amp;nbsp; &amp;nbsp;00:50:56:bd:ff:ec|00:50:56:bd:56:70&lt;BR /&gt;Server02 Adapter1&amp;nbsp; &amp;nbsp;4321&amp;nbsp; &amp;nbsp; &amp;nbsp;00:50:56:bd:d5:09|00:50:56:bd:4b:a9&lt;BR /&gt;Server02 Adapter2&amp;nbsp; &amp;nbsp;1000&amp;nbsp; &amp;nbsp;00:50:56:bd:d5:09|00:50:56:bd:4b:a9&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;something like it's what I want to receive&lt;/P&gt;&lt;P&gt;Server01 Adapter1&amp;nbsp; &amp;nbsp;1234&amp;nbsp; &amp;nbsp; &amp;nbsp; 00:50:56:bd:56:70&lt;BR /&gt;Server01 Adapter2&amp;nbsp; &amp;nbsp;2000&amp;nbsp; &amp;nbsp;00:50:56:bd:ff:ec&lt;BR /&gt;Server02 Adapter1&amp;nbsp; &amp;nbsp;4321&amp;nbsp; &amp;nbsp; &amp;nbsp;00:50:56:bd:d5:09&lt;BR /&gt;Server02 Adapter2&amp;nbsp; &amp;nbsp;1000&amp;nbsp; &amp;nbsp;00:50:56:bd:4b:a9&lt;/P&gt;</description>
      <pubDate>Sat, 18 Mar 2023 22:50:27 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2959865#M111268</guid>
      <dc:creator>dbc1</dc:creator>
      <dc:date>2023-03-18T22:50:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to get Mac Address</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2959883#M111274</link>
      <description>&lt;P&gt;That requires some changes to your code.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;$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 '|' } }
}
&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 19 Mar 2023 08:29:27 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2959883#M111274</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2023-03-19T08:29:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to get Mac Address</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2959896#M111276</link>
      <description>&lt;P&gt;Now doesn't show the MAC at all &lt;img class="lia-deferred-image lia-image-emoji" src="https://communities.vmware.com/html/@175698D61412968B624AD0FDF51E2E6D/emoticons/1f604.png" alt=":grinning_face_with_smiling_eyes:" title=":grinning_face_with_smiling_eyes:" /&gt; . But it's ok, I will see what I can do with previous command.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Mar 2023 11:59:37 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2959896#M111276</guid>
      <dc:creator>dbc1</dc:creator>
      <dc:date>2023-03-19T11:59:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to get Mac Address</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2959898#M111277</link>
      <description>&lt;P&gt;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.&lt;BR /&gt;Check what is in&amp;nbsp;&lt;STRONG&gt;$vm.guest.nics&lt;/STRONG&gt; for some of the VMs in your file.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Mar 2023 12:05:28 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2959898#M111277</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2023-03-19T12:05:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to get Mac Address</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2961363#M111402</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;This one is years old and not the fastest but i was always to lazy to optimize it &lt;img class="lia-deferred-image lia-image-emoji" src="https://communities.vmware.com/html/@DCF4E2F7991292CEECF250394DB2C2BC/emoticons/1f642.png" alt=":slightly_smiling_face:" title=":slightly_smiling_face:" /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;$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

	}

}
&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 28 Mar 2023 14:25:04 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2961363#M111402</guid>
      <dc:creator>rwk1982</dc:creator>
      <dc:date>2023-03-28T14:25:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to get Mac Address</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2961380#M111404</link>
      <description>&lt;P&gt;Nice, but you do realise that your code will not work for a Private VLAN or when VLAN Trunking is used.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2023 16:14:14 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-get-Mac-Address/m-p/2961380#M111404</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2023-03-28T16:14:14Z</dc:date>
    </item>
  </channel>
</rss>

