I am trying to migrate all of the vnics of all of the VMs on a particular host from a DVS backed portgroup to a SVS backed portgroup of the same name. I've already found similar snippets of code, but after putting it together, it doesn't work, and I'm getting an error.
Here's my script:
$nics = Get-VMHost -Name "myhostname" | Get-VM | Get-NetworkAdapter
foreach ($nic in $nics)
{
$name = $nic.NetworkName
Write-Host $name
$vdSwitch = Get-VDSwitch -Name 'DVSname'
$vdPortGroup = Get-VDPortGroup -VDSwitch $vdSwitch -Name $name
$virtualSwitch = Get-VirtualSwitch -Name 'SVSname'
$vmsPortGroup = $virtualSwitch | Get-VirtualPortGroup -Name $name
Set-NetworkAdapter -NetworkAdapter $nic -PortGroup $vmsPortGroup
}
Any suggestions?
Figured it out!
My mistake was I didn't specify the standard vswitch by hostname.
Corrected final script, with debug outputs removed:
$nics = Get-VMHost -Name "myhostname" | Get-VM | Get-NetworkAdapter
foreach ($nic in $nics)
{
$name = $nic.NetworkName
$virtualSwitch = Get-VirtualSwitch -Name "myswitchname" -VMHost "myhostname"
$vmsPortGroup = $virtualSwitch | Get-VirtualPortGroup -Name $name
Set-NetworkAdapter -NetworkAdapter $nic -PortGroup $vmsPortGroup -Confirm:$false
}
What errors are you getting?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I made a few changes, but it still doesn't work correctly:
$nics = Get-VMHost -Name "myhostname" | Get-VM | Get-NetworkAdapter
foreach ($nic in $nics)
{
$name = $nic.NetworkName
Write-Host $name
$virtualSwitch = Get-VirtualSwitch -Name 'SvSwitch100'
$vmsPortGroup = $virtualSwitch | Get-VirtualPortGroup -Name $name
Write-Host $vmsPortGroup.NetworkName
Set-NetworkAdapter -NetworkAdapter $nic -NetworkName "$name" -Confirm:$false
}
Here's my output:
PowerCLI C:\users\wevcar02\documents\scripts> .\MovevNics.ps1
INT-0115-TNA-Internal
Name Type NetworkName MacAddress WakeOnLan
Enabled
---- ---- ----------- ---------- ---------
Network adapter 1 Vmxnet3 INT-0115-... 00:50:56:a1:16:21 True
PowerCLI C:\users\wevcar02\documents\scripts>
So it works, but not correctly. The issue is this. I have two portgroups with the same name: one on a standard vSwitch names SvSwitch100 and another on a Distributed vSwitch named IBM-PRIV. Using the above script, it always connects the vnic to the one associated with the Distributed vSwitch, which was created before the standard one.
First I tested it when the nic was connected to the DVS portgroup, and it works but assiigns it to the same portgroup.
Next I tested it with the nic connected to the SVS portgroup, and it changed it to the DVS portgroup
Incidentally, I changed the parameter for the Set-NetworkAdapter from -PortGroup to -NetworkName thinking the first is to be used only for DVSes.
I thought also maybe that the $name when its attached to the DVS included, as it appears through the GUI, the name of the DVS in parenthesis, but as shown in the output, it does not.
Did you already try by explicitly specifying a VSS?
Like this
$virtualSwitch = Get-VirtualSwitch -Name 'SVSname' -Standard
$vmsPortGroup = $virtualSwitch | Get-VirtualPortGroup -Name $name
Set-NetworkAdapter -NetworkAdapter $nic -Portgroup $vmsPortgroup -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Figured it out!
My mistake was I didn't specify the standard vswitch by hostname.
Corrected final script, with debug outputs removed:
$nics = Get-VMHost -Name "myhostname" | Get-VM | Get-NetworkAdapter
foreach ($nic in $nics)
{
$name = $nic.NetworkName
$virtualSwitch = Get-VirtualSwitch -Name "myswitchname" -VMHost "myhostname"
$vmsPortGroup = $virtualSwitch | Get-VirtualPortGroup -Name $name
Set-NetworkAdapter -NetworkAdapter $nic -PortGroup $vmsPortGroup -Confirm:$false
}
Hi there, I'm trying to use the script above that you guys came up with but run into the error below. Please help and let me know what I did wrong. Thanks very much!
Get-VMHost : 1/25/2019 12:10:37 PM Get-VMHost VMHost with name 'MyHost' was not found using the
specified filter(s).
At C:\files\move-vm.ps1:1 char:9
+ $nics = Get-VMHost -Name "MyHost" | Get-VM | Get-Ne ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-VMHost], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVMH
ost
Do you actually have an ESXi node with the name "MyHost"?
You have to adapt the name for your environment.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks very much Luc! I replaced my real host name with MyHost when I posted the error. When I got that error, I checked the host name a few times and found no typo or issue with it. Your question got me thinking again. So I went back and double checked. It turned out I connected to a wrong vCenter with a very similar name that only has one later different. All is working great now. Thanks a lot Luc! You always rock as usual!