VMware Cloud Community
CCSL
Contributor
Contributor
Jump to solution

Script to set static Mac address

I have a VIrtual Infrastructure with about 200 vm's all with multiple vNics and automatic Mac address's. I need to assign static Mac's for all vNics and want to achieve this with a powershell script and a csv input file. I have the csv input file that looks something like this :  Vm,portgroup,Mac Vm1,pg1, 00:11:22:33 Vm1,pg2,00:11:22:44 Vm2,pg1,00:11:44:44  (I know the mac's above aren't valid)  Can anyone help with the powershell code to automate setting the static mac's?  Thanks in advance

0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, CCSL-

You should be able to use the following to achieve this:

## import the info from the CSV
$colNICInfo = Import-Csv c:\vmNICInfo.csv
$colNICInfo | %{
   
$oSingleNICInfo = $_
   
## get the VM, get its NetworkAdapters and for the one whose NetworkName matches that from the CSV, set its MAC address
    Get-VM $oSingleNICInfo.VMName | Get-NetworkAdapter | ?{$_.NetworkName -eq $oSingleNICInfo.NetwkName} | Set-NetworkAdapter -MacAddress $oSingleNICInfo.MACAddr -Confirm:$false -WhatIf
}
## end foreach-object

A few things to note:

  • I left the "-WhatIf" parameter on the Set-NetworkAdapter portion, so that you can run this first (maybe with a small sample set in the CSV file) to check that things look like they will work.  Remove this parameter to actually set the MAC addresses
  • The code expects the CSV file to be of this format:
    VMName,NetwkName,MACAddr
    someVM,VLAN128,00:50:56:00:00:0F

  • The Set-NetworkAdapter cmdlet requires the new MAC address to be in the range 00:50:56:00:00:00 - 00:50:56:3F:FF:FF.  This is not the full range that VMware may use, just a subset.  Seems to be a limitation of the cmdlet (likely intentional).

Message was edited by mattboren:  added note about removing "-WhatIf" to actually set the NICs' MAC addresses

      View solution in original post

      0 Kudos
      5 Replies
      mcowger
      Immortal
      Immortal
      Jump to solution

      Why?  Once the MAC for the VM has been created, it doesn't change over time....You also wont be able to set a MAC outside of VMware's range (00:50:56).

      --Matt VCDX #52 blog.cowger.us
      0 Kudos
      CCSL
      Contributor
      Contributor
      Jump to solution

      I don't want to get into a discussion over why Matt and yes I"m aware of the vmware range for the actual Mac address's I will be using.  Thanks

      0 Kudos
      Hosted201110141
      Enthusiast
      Enthusiast
      Jump to solution

      I think this is doable but I have a couple of questions.

      What is the portgroup information used for?  Do you want them assigned to a new portgroup after you assign the mac?

      Do any of the VMs have multiple nics?

      0 Kudos
      mattboren
      Expert
      Expert
      Jump to solution

      Hello, CCSL-

      You should be able to use the following to achieve this:

      ## import the info from the CSV
      $colNICInfo = Import-Csv c:\vmNICInfo.csv
      $colNICInfo | %{
         
      $oSingleNICInfo = $_
         
      ## get the VM, get its NetworkAdapters and for the one whose NetworkName matches that from the CSV, set its MAC address
          Get-VM $oSingleNICInfo.VMName | Get-NetworkAdapter | ?{$_.NetworkName -eq $oSingleNICInfo.NetwkName} | Set-NetworkAdapter -MacAddress $oSingleNICInfo.MACAddr -Confirm:$false -WhatIf
      }
      ## end foreach-object

      A few things to note:

      • I left the "-WhatIf" parameter on the Set-NetworkAdapter portion, so that you can run this first (maybe with a small sample set in the CSV file) to check that things look like they will work.  Remove this parameter to actually set the MAC addresses
      • The code expects the CSV file to be of this format:
        VMName,NetwkName,MACAddr
        someVM,VLAN128,00:50:56:00:00:0F

      • The Set-NetworkAdapter cmdlet requires the new MAC address to be in the range 00:50:56:00:00:00 - 00:50:56:3F:FF:FF.  This is not the full range that VMware may use, just a subset.  Seems to be a limitation of the cmdlet (likely intentional).

      Message was edited by mattboren:  added note about removing "-WhatIf" to actually set the NICs' MAC addresses

          0 Kudos
          CCSL
          Contributor
          Contributor
          Jump to solution

          Many Thanks Mattboren, that's exactly what I require.

          0 Kudos