背景
需求:
- 批量修改虚拟机的分布式端口组
解决:
- 三条命令解决:先获取目标虚拟机、获取目标端口组、修改虚拟机端口组、检查虚拟机状态。
powershell
$vm = Get-VM -Name <虚拟机名称>
$portGroup = Get-VirtualPortGroup -Name <端口组名称>
Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $portGroup
Get-NetworkAdapter -VM $vm
实际操作流程
实操环境
因为调整物理机资源,部分esxi主机加入到新的vcenter中,导入分布式交换机后出现一些问题,需要调整部分虚拟机网络:
三台名叫10.43.10.78-K8SConsul-Czm01
的虚拟机需要修改分布式交换机端口组,
修改前:DPortGroup-xxxx-10.43.10.x dvportgroup-285
修改后:DPortGroup-xxxx-10.43.10.x (1) dvportgroup-15232
1.获取目标虚拟机
获取目标虚拟机,设为变量$vm
bash
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> get-vm -name *K8ScONSUL*|Get-NetworkAdapter
Name Type NetworkName MacAddress WakeOnLan
Enabled
---- ---- ----------- ---------- ---------
Network adapter 1 Vmxnet3 dvportgroup-285 00:50:56:b6:xx:ee True
Network adapter 1 Vmxnet3 dvportgroup-285 00:50:56:b6:xx:76 True
Network adapter 1 Vmxnet3 dvportgroup-285 00:50:56:b6:xx:ba True
Network adapter 1 Vmxnet3 dvportgroup-285 00:50:56:b6:xx:3f True
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> $vm = Get-VM -Name *k8sconsul-c*
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> $vm
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
10.43.10.79-K8SCo... PoweredOn 4 8.000
10.43.10.78-K8SCo... PoweredOn 4 8.000
10.43.10.80-K8SCo... PoweredOn 4 8.000
2.获取目标端口组
获取目标端口组,设为变量$portGroup
bash
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> Get-VirtualPortGroup|ft -au
Name Key VLanId PortBinding NumPorts
---- --- ------ ----------- --------
DPortGroup-xxxx-10.43.10.x (1) dvportgroup-15232 Static 48
DPortGroup-xxxx-10.43.10.x dvportgroup-285 Static 48
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> $portGroup = Get-VirtualPortGroup -name "DPortGroup-xxxx-10.43.10.x (1)"
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> $portGroup
Name Key VLanId PortBinding NumPorts
---- --- ------ ----------- --------
DPortGroup-xxxx... dvportgroup-15232 Static 48
3.修改虚拟机配置:
以先前两个变量($vm
和$portGroup
)为命令操作对象,将目标虚拟机网络适配器,修改改为使用目标端口组。
bash
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> $vm Name PowerState Num CPUs MemoryGB ---- ---------- -------- -------- 10.43.10.79-K8SCo... PoweredOn 4 8.000 10.43.10.78-K8SCo... PoweredOn 4 8.000 10.43.10.80-K8SCo... PoweredOn 4 8.000
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> $portGroup
Name Key VLanId PortBinding NumPorts
---- --- ------ ----------- --------
DPortGroup-xxxx... dvportgroup-15232 Static 48
执行以下命令修改虚拟机的配置,修改端口组。
powershell
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $portGroup
确认
是否确实要执行此操作?
正在目标"Network adapter 1"上执行操作"Setting NetworkName: DPortGroup-VID_310-TEST.测试区-10.43.10.x (1)"。
[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 暂停(S) [?] 帮助 (默认值为"Y"): a
警告: Specifying a distributed port group name as network name is no longer supported. Use the -Portgroup parameter.
Name Type NetworkName MacAddress WakeOnLan
Enabled
---- ---- ----------- ---------- ---------
Network adapter 1 Vmxnet3 DPortGroup-x... 00:50:56:b6:xx:ee True
Network adapter 1 Vmxnet3 DPortGroup-x... 00:50:56:b6:xx:3f True
Network adapter 1 Vmxnet3 DPortGroup-x... 00:50:56:b6:xx:76 True
4.检查虚拟机状态
powershell
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> Get-NetworkAdapter -VM $vm
Name Type NetworkName MacAddress WakeOnLan
Enabled
---- ---- ----------- ---------- ---------
Network adapter 1 Vmxnet3 DPortGroup-x... 00:50:56:b6:xx:ee True
Network adapter 1 Vmxnet3 DPortGroup-x... 00:50:56:b6:xx:3f True
Network adapter 1 Vmxnet3 DPortGroup-x... 00:50:56:b6:xx:76 True
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> Get-NetworkAdapter -VM $vm|ft -au
Name Type NetworkName MacAddress WakeOnLanEnabled
---- ---- ----------- ---------- ----------------
Network adapter 1 Vmxnet3 DPortGroup-xxxx-10.43.10.x (1) 00:50:56:b6:xx:ee True
Network adapter 1 Vmxnet3 DPortGroup-xxxx-10.43.10.x (1) 00:50:56:b6:xx:3f True
Network adapter 1 Vmxnet3 DPortGroup-xxxx-10.43.10.x (1) 00:50:56:b6:xx:76 True
分布式端口组修改成功。
参考截图