背景
需求:
- 批量修改虚拟机的分布式端口组
解决方式一:
- 三条命令解决:先获取目标虚拟机、获取目标端口组、修改虚拟机端口组、检查虚拟机状态。
$vm = Get-VM -Name <虚拟机名称>
$portGroup = Get-VirtualPortGroup -Name <端口组名称>
Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $portGroup
Get-NetworkAdapter -VM $vm
解决方式二:
- 使用管道符连接多条命令:获得具体某个数据中心的名叫10.42.40.x的虚拟机、获取这些虚拟机的网卡、修改这些网卡为新的配置。
Get-VM -Location Datacenter-xxx-xxx -name 10.42.40.*|Get-NetworkAdapter|Set-NetworkAdapter -NetworkName "DPortGroup-xxxxxx-10.42.40.x (1)"|ft -au
实际操作流程(方式一)
实操环境
因为调整物理机资源,部分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
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> get-vm -name *K8ScONSUL*|Get-NetworkAdapterName Type NetworkName MacAddress WakeOnLanEnabled
---- ---- ----------- ---------- ---------
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 TruePS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> $vm = Get-VM -Name *k8sconsul-c*
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> $vmName 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
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> Get-VirtualPortGroup|ft -auName Key VLanId PortBinding NumPorts
---- --- ------ ----------- --------DPortGroup-xxxx-10.43.10.x (1) dvportgroup-15232 Static 48
DPortGroup-xxxx-10.43.10.x dvportgroup-285 Static 48PS 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> $portGroupName Key VLanId PortBinding NumPorts
---- --- ------ ----------- --------
DPortGroup-xxxx... dvportgroup-15232 Static 48
3.修改虚拟机配置:
以先前两个变量($vm
和$portGroup
)为命令操作对象,将目标虚拟机网络适配器,修改改为使用目标端口组。
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> $portGroupName Key VLanId PortBinding NumPorts
---- --- ------ ----------- --------
DPortGroup-xxxx... dvportgroup-15232 Static 48
执行以下命令修改虚拟机的配置,修改端口组。
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 WakeOnLanEnabled
---- ---- ----------- ---------- ---------
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.检查虚拟机状态
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> Get-NetworkAdapter -VM $vmName Type NetworkName MacAddress WakeOnLanEnabled
---- ---- ----------- ---------- ---------
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 TruePS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> Get-NetworkAdapter -VM $vm|ft -auName 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
分布式端口组修改成功。
参考截图
实际操作流程(方式二)
若觉得设置变量太麻烦,也可直接通过管道符操作,将多条命令连续执行进行操作。
此处不需要以变量为对象进行操作。
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> Get-VM -Location Datacenter-xxx-xxx -name 10.42.40.*|Get-NetworkAdapter|ft -au
执行结果参考如下:
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> Get-VM -Location Datacenter-xxx-xxx -name 10.42.40.*|Get-NetworkAdapter|ft -auName Type NetworkName MacAddress WakeOnLanEnabled
---- ---- ----------- ---------- ----------------
Network adapter 1 Vmxnet3 DPortGroup-xxxx-10.42.40.x (1) 00:50:56:b6:xx:1c True
Network adapter 2 Vmxnet3 DPortGroup-xxxx-10.42.40.x (1) 00:50:56:b6:xx:2b True
Network adapter 1 Vmxnet3 dvportgroup-269 00:50:56:b6:60:76 True
Network adapter 1 Vmxnet3 DPortGroup-xxxx-10.42.40.x (1) 00:50:56:b6:xx:80 True
Network adapter 1 Vmxnet3 dvportgroup-269 00:50:56:b6:d5:67 True
Network adapter 1 Vmxnet3 DPortGroup-xxxx-10.42.40.x (1) 00:50:56:b6:xx:22 True
Network adapter 1 Vmxnet3 DPortGroup-xxxx-10.42.40.x (1) 00:50:56:b6:xx:6e True
Network adapter 1 Vmxnet3 dvportgroup-269 00:50:56:b6:c3:4f True
Network adapter 1 Vmxnet3 DPortGroup-xxxx-10.42.40.x (1) 00:50:56:b6:xx:ae True
Network adapter 1 Vmxnet3 dvportgroup-269 00:50:56:b6:d2:4e True
Network adapter 1 Vmxnet3 DPortGroup-xxxx-10.42.40.x (1) 00:50:56:b6:xx:7a True
Network adapter 1 Vmxnet3 dvportgroup-269 00:50:56:b6:a4:e3 True
Network adapter 1 Vmxnet3 dvportgroup-269 00:50:56:b6:95:f5 True
Network adapter 1 Vmxnet3 dvportgroup-269 00:50:56:b6:2b:fc True
Network adapter 1 Vmxnet3 dvportgroup-269 00:50:56:b6:18:39 True
Network adapter 1 Vmxnet3 dvportgroup-269 00:50:56:b6:3c:09 True
Network adapter 1 Vmxnet3 DPortGroup-xxxx-10.42.40.x (1) 00:50:56:b6:xx:89 True
Network adapter 1 Vmxnet3 DPortGroup-xxxx-10.42.40.x (1) 00:50:56:b6:xx:a3 True
Network adapter 1 Vmxnet3 dvportgroup-269 00:50:56:b6:68:91 True
Network adapter 1 Vmxnet3 DPortGroup-xxxx-10.42.40.x (1) 00:50:56:8a:xx:59 True
PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> Get-VM -Location Datacenter-xxx-xxx -name 10.42.40.*|Get-NetworkAdapter|Set-NetworkAdapter -NetworkName "DPortGroup-xxxxxx-10.42.40.x (1)"|ft -au
确认
是否确实要执行此操作?
正在目标“Network adapter 1”上执行操作“Setting NetworkName: DPortGroup-xxxxx-10.42.40.x (1)”。
[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 暂停(S) [?] 帮助 (默认值为“Y”): aName Type NetworkName MacAddress WakeOnLanEnabled
---- ---- ----------- ---------- ----------------
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:1c True
Network adapter 2 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:2b True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:76 True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:80 True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:67 True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:22 True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:6e True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:4f True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:ae True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:4e True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:7a True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:e3 True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:f5 True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:fc True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:39 True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:09 True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:89 True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:a3 True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:b6:xx:91 True
Network adapter 1 Vmxnet3 DPortGroup-xxxxx-10.42.40.x (1) 00:50:56:8a:xx:59 True