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
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:
Message was edited by mattboren: added note about removing "-WhatIf" to actually set the NICs' MAC addresses
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).
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
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?
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:
Message was edited by mattboren: added note about removing "-WhatIf" to actually set the NICs' MAC addresses
Many Thanks Mattboren, that's exactly what I require.