本文主要是介绍Powercli批量修改分布式交换机端口组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
背景
需求:
- 批量修改虚拟机的分布式端口组
解决:
- 三条命令解决:先获取目标虚拟机、获取目标端口组、修改虚拟机端口组、检查虚拟机状态。
$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
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
分布式端口组修改成功。
参考截图
完成
这篇关于Powercli批量修改分布式交换机端口组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!