VMware Cloud Community
surfettekw
Contributor
Contributor
Jump to solution

Changing Vlans of a list of VMs and then forcing a reboot.

I am starting over with a script I found in this forum - I am new to this and just trying to wrap my head around this code.... so appologies if this is elementary for everyone else Smiley Happy

# VCenter you are connecting too

$vcserver = "MyVcenter.somewhere.local"

Connect-VIServer $vcserver

# Loop to make changes to Network Adapter from List of Servers that needs to be changed

$serverlist = Get-Content "C:\Scripts\vlan_Test\vms.txt"

ForEach ($server in $serverlist)

{

$NIC = Get-NetworkAdapter -VM $server

Set-NetworkAdapter -NetworkAdapter $NIC -NetworkName "MyNewVLan 1234"- confirm:$false

Do I just add restart to this line?

}

Thanks in advance - I really wanted to validate the vlan name first.. and if = abc rename to def and then reboot (just in case you are really in the mood to help me out Smiley Happy )

1 Solution

Accepted Solutions
rsoc
Enthusiast
Enthusiast
Jump to solution

Yes, you can simply add the VM reboot after you set the new network assignment which I have included in the script below and I've added the test to verify that you are only going to change NICs that have a specific VLAN already assigned. The script below assumes that you only want to reboot VMs where the network assignment has been modified. If you want to reboot all of the VMs on the list regardless of whether they need the change then move the restart-vmguest line out of the IF statement. Also, I want to add that a reboot is not required for this change.

# VCenter you are connecting too

$vcserver = "MyVcenter.somewhere.local"

Connect-VIServer $vcserver

# Loop to make changes to Network Adapter from List of Servers that needs to be changed

$serverlist = Get-Content "C:\Scripts\vlan_Test\vms.txt"

ForEach ($server in $serverlist){

     $NIC = Get-NetworkAdapter -VM $server

     If($NIC.NetworkName -like "MyOldVLan 1233"){

          Set-NetworkAdapter -NetworkAdapter $NIC -NetworkName "MyNewVLan 1234"- confirm:$false

          restart-VMGuest -VM $server

     }

}

View solution in original post

Reply
0 Kudos
3 Replies
rsoc
Enthusiast
Enthusiast
Jump to solution

Yes, you can simply add the VM reboot after you set the new network assignment which I have included in the script below and I've added the test to verify that you are only going to change NICs that have a specific VLAN already assigned. The script below assumes that you only want to reboot VMs where the network assignment has been modified. If you want to reboot all of the VMs on the list regardless of whether they need the change then move the restart-vmguest line out of the IF statement. Also, I want to add that a reboot is not required for this change.

# VCenter you are connecting too

$vcserver = "MyVcenter.somewhere.local"

Connect-VIServer $vcserver

# Loop to make changes to Network Adapter from List of Servers that needs to be changed

$serverlist = Get-Content "C:\Scripts\vlan_Test\vms.txt"

ForEach ($server in $serverlist){

     $NIC = Get-NetworkAdapter -VM $server

     If($NIC.NetworkName -like "MyOldVLan 1233"){

          Set-NetworkAdapter -NetworkAdapter $NIC -NetworkName "MyNewVLan 1234"- confirm:$false

          restart-VMGuest -VM $server

     }

}

Reply
0 Kudos
surfettekw
Contributor
Contributor
Jump to solution

Got it to work - one more thing if you please Smiley Happy  how can I add a state in the if statement for example:  If the name -like... and the powerstate = up

Thanks again!!! Sure appreciate it!

Reply
0 Kudos
rsoc
Enthusiast
Enthusiast
Jump to solution

I'm glad you got it to work. I've added the powerstate check below but I'm not positive I got all of the ()'s right so if it doesn't work let me know.


# VCenter you are connecting too

$vcserver = "MyVcenter.somewhere.local"

Connect-VIServer $vcserver

# Loop to make changes to Network Adapter from List of Servers that needs to be changed

$serverlist = Get-Content "C:\Scripts\vlan_Test\vms.txt"

ForEach ($server in $serverlist){

     $NIC = Get-NetworkAdapter -VM $server

     If((($NIC.NetworkName -like "MyOldVLan 1233") -and ((get-vm $server).powerstate -like "poweredon"))) {

          Set-NetworkAdapter -NetworkAdapter $NIC -NetworkName "MyNewVLan 1234"- confirm:$false

          restart-VMGuest -VM $server

     }

}