VM MAC Management

Jan 26, 2010 at 11:06 PM

I'm curious if anyone has already found a way to programatically check the "Static" option for all the NICS of Every VM.

If someone knows how or already has some code for it, i would love to see it.

Thanks,

Josh

Jan 27, 2010 at 12:42 AM

Interesting question.  I've got a process in mind to do this, which would be adapted for both new VMs and switching current ones. 

 

 

Jan 27, 2010 at 6:00 PM

Well i would love any help that can be offered. I'm currently wading through scripts i only marginally understand in hopes of being able to adapt it to my purpose.   :)

Jan 27, 2010 at 7:47 PM
This is a basic brute force method, which shuts off all VMs regardless of state, takes the existing MAC and then sets it unless it's already set to static.  This can be adjusted to set just legacy or vmbus nics and can be adapted for get-vm status options if needed.  

stop-vm -server $server -force -wait

foreach ($_ in(get-vmnic -server $server)){ if ($_.staticMacAddress -eq $False){ set-vmnicaddress -nic $_.ElementName -mac $_.address } }
start-vm -server $server

 

 

 

 

 

Jan 27, 2010 at 11:23 PM

This isn't giving me any error and is not producing the desired result of checking the Static option. This is a good starting point though, i very much appreciate it.

Jan 28, 2010 at 10:10 PM

My apologies. This is exactly what i was looking for. I was testing with a machine that had a Legacy nic and this script works on the normal NIC. For some reason i thought it already encompassed both.

 

Again thank you very much, this is going to save us loads of time.

Jan 28, 2010 at 10:25 PM

Err maybe i tricked my self. Heh. Not sure if it worked. Doesn't seem to be working now.

Jan 28, 2010 at 11:45 PM

My mistake.  I know why this failed:  set-vmnicaddress expects an object and cannot .  I didn't have a machine to test this on at the time.  Sorry about that. 

It should work on all nics with the get-vmnic line; add -legacy or -vmbus. 

I am looking into this. 

 

 

 

Jan 28, 2010 at 11:53 PM

Ok, got it.  I tripped up in two places, once with the stop-vm line (expected a var for -vm) and wrong type for the set-vmnicaddress (passed a string instead of an object).

Thanks for your patience.  I am unlearning my old tricks from years of scripting.

 

$server  = "server01"

get-vm -server $server |stop-vm -force -wait
foreach ($_ in(get-vmnic -server $server)){
    if ($_.staticMacAddress -eq $False){
        set-vmnicaddress -nic $_ -mac $_.address 
    }
}

start-vm -server $server

 

Jan 29, 2010 at 8:16 PM

Right on. Thank you very much.