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
# 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 )
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
}
}
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
}
}
Got it to work - one more thing if you please 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!
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
}
}