tag:blogger.com,1999:blog-2269516327950490847.post2843012040934728440..comments2023-03-28T08:39:06.775-04:00Comments on vNugglets: Setting MAC Address for VM NICs using PowerShellAC (@allen_crawford)http://www.blogger.com/profile/05360032132878174436noreply@blogger.comBlogger12125tag:blogger.com,1999:blog-2269516327950490847.post-19941726337313790532015-03-05T11:20:25.166-05:002015-03-05T11:20:25.166-05:00I found the solution in order to use it on multipl...I found the solution in order to use it on multiple NICs.<br /><br />Here is the modification of the script :<br /><br />$fromVMname is the VM that I just have cloned<br />$newVMname is the cloned VM<br /><br />$NewMACAddr = Get-NetworkAdapter $fromVMname<br /> # get the .NET view object of the VM<br /> $viewTargetVM = <br /> Get-View -ViewType VirtualMachine -Property Name,Config.Hardware.Device -Filter @{"Name" = "^${newVMname}$"}<br /> $deviceNIC = $viewTargetVM.Config.Hardware.Device | Where-Object {$_ -is [VMware.Vim.VirtualEthernetCard]}<br /> $cardnumber = $NewMACAddr.MacAddress.Count<br /> for ($i=1; $i -le $cardnumber; $i++) {<br /> $j=$i-1<br /> # get the NIC device (further operations assume that this VM has only one NIC)<br /> $deviceNIC[$j].MacAddress = $NewMACAddr[$j].MacAddress<br /> #set the MAC address type to manual<br /> $deviceNIC[$j].addressType = "Manual"<br /> # create the new VMConfigSpec<br /> $specNewVMConfig = New-Object VMware.Vim.VirtualMachineConfigSpec -Property @{<br /> # setup the deviceChange object<br /> deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec -Property @{<br /> # the kind of operation, from the given enumeration<br /> operation = "edit"<br /> # the device to change, with the desired settings<br /> device = $deviceNIC[$j]<br /> } # end New-Object<br /> } # end New-Object<br /> # Reconfiguration de la VM clone pour prendre en compte les nouveaux paramètres<br /> $viewTargetVM.ReconfigVM($specNewVMConfig)<br /> }<br /><br />I try it successfuly on a VM which have 1 NIC, 2 NICs and 3 NICs.Romshttps://www.blogger.com/profile/04754736473900250680noreply@blogger.comtag:blogger.com,1999:blog-2269516327950490847.post-48603852182598366312014-01-27T23:24:04.847-05:002014-01-27T23:24:04.847-05:00Just used this for a CentOS migration where mass g...Just used this for a CentOS migration where mass guest access wasn't viable and the interface configs were tied to the MAC. Much appreciated.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2269516327950490847.post-48359645451120381282013-12-19T23:55:07.393-05:002013-12-19T23:55:07.393-05:00Being new to Powershell I got confused as to how t...Being new to Powershell I got confused as to how to modify this script in order to use it on multiple NICs. Any chance of showing how this script can be modified to enable this?<br /><br />ThanksAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-2269516327950490847.post-2035289482785865792013-12-19T23:36:48.516-05:002013-12-19T23:36:48.516-05:00Ignore my question, I didn't spot that is has ...Ignore my question, I didn't spot that is has already been mentioned above - Adding " | ? {$_.MacAddress -eq "$strNewMACAddr"}" to line 12 will make the scripts compatible with VMs with multiple NICs.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2269516327950490847.post-34099229690207941932013-12-19T23:35:02.529-05:002013-12-19T23:35:02.529-05:00What is needed to be changed/added in order to use...What is needed to be changed/added in order to use this script to change the MAC address for the "Network Adapter 1" on a VM which has more then one NIC?<br /><br />Thanks<br /> Mayur Patelnoreply@blogger.comtag:blogger.com,1999:blog-2269516327950490847.post-61874917946021515392013-10-09T06:03:59.121-04:002013-10-09T06:03:59.121-04:00Never used this trick to change MAC Address but it...Never used this trick to change MAC Address but it sounds very interesting.Would like to use it for sure.<br /><br />Thanks<br />Silvester Norman<br /><br /><a href="http://sdadapters.com/faq.htm" rel="nofollow">Changing MAC Address</a><br />Anonymoushttps://www.blogger.com/profile/00527553881509979555noreply@blogger.comtag:blogger.com,1999:blog-2269516327950490847.post-16362821863102483892012-01-11T22:55:52.766-05:002012-01-11T22:55:52.766-05:00Thanks, again, Joep Piscaer (virtuallifestyle.nl),...Thanks, again, Joep Piscaer (virtuallifestyle.nl), for that suggestion. While the exact Where-Object clause that you suggested will not solve the "VM with multiple NICs" situation (since it will return zero NICs, since none of the VM's NICs should have the _new_ MAC address already), the general idea of adding a Where filter would definitely be a way to go.<br /><br />Example: use a Where-Object filter that matches on the VirtualNetworkCard device's display name or _current_ MAC address.<br /><br />Thanks for the tip for everyone.MattBoren (@mtboren)https://www.blogger.com/profile/08371304528581535245noreply@blogger.comtag:blogger.com,1999:blog-2269516327950490847.post-3021680542041740552012-01-09T09:41:52.122-05:002012-01-09T09:41:52.122-05:00Adding " | ? {$_.MacAddress -eq "$strNew...Adding " | ? {$_.MacAddress -eq "$strNewMACAddr"}" to line 12 will make the scripts compatible with VMs with multiple NICs.Joep Piscaerhttp://www.virtuallifestyle.nlnoreply@blogger.comtag:blogger.com,1999:blog-2269516327950490847.post-71523388091277541712011-12-31T13:36:18.843-05:002011-12-31T13:36:18.843-05:00Thanks, Joep Piscaer (virtuallifestyle.nl), for po...Thanks, Joep Piscaer (virtuallifestyle.nl), for pointing out the problem of multiple View objects being returned. I have now updated the code to use a regular expression for the -Filter such that it will match on the entire string only (by using the "^" and "$" anchor characters). Enjoy!MattBoren (@mtboren)https://www.blogger.com/profile/08371304528581535245noreply@blogger.comtag:blogger.com,1999:blog-2269516327950490847.post-17264839633374995222011-12-29T13:44:27.922-05:002011-12-29T13:44:27.922-05:00## get the .NET view object of the VM
10
$viewTarg...## get the .NET view object of the VM<br />10<br />$viewTargetVM = Get-View -ViewType VirtualMachine -Property Name,Config.Hardware.Device -Filter @{"Name" = $strTargetVMName}<br /><br />This line returns multiple objects of the VMName occurs more than once, i.e. if you have two VM's containing the name 'database'...Joep Piscaerhttps://www.virtuallifestyle.nlnoreply@blogger.comtag:blogger.com,1999:blog-2269516327950490847.post-89983413864735631852011-11-25T19:58:12.643-05:002011-11-25T19:58:12.643-05:00Thanks for bringing that up, "Anonymous"...Thanks for bringing that up, "Anonymous". I have updated the post with a note about this point.MattBoren (@mtboren)https://www.blogger.com/profile/08371304528581535245noreply@blogger.comtag:blogger.com,1999:blog-2269516327950490847.post-9325001480142276992011-11-25T07:38:43.374-05:002011-11-25T07:38:43.374-05:00Thanks for the script. I've got the following ...Thanks for the script. I've got the following message when i look in the NIC properties of the VM after i changed the MAC address:<br />The MAC address entered is not in the valid range.....Anonymousnoreply@blogger.com