• Powercli批量修改分布式交换机端口组


    背景

    需求:

    • 批量修改虚拟机的分布式端口组

    解决方式一:

    • 三条命令解决:先获取目标虚拟机、获取目标端口组、修改虚拟机端口组、检查虚拟机状态。
    $vm = Get-VM -Name <虚拟机名称>
    $portGroup = Get-VirtualPortGroup -Name <端口组名称>
    Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $portGroup
    Get-NetworkAdapter -VM $vm
    
    • 1
    • 2
    • 3
    • 4

    解决方式二:

    • 使用管道符连接多条命令:获得具体某个数据中心的名叫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
    
    • 1

    实际操作流程(方式一)

    实操环境
    因为调整物理机资源,部分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-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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2.获取目标端口组

    获取目标端口组,设为变量$portGroup

    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                                                                                                                                   
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    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> $portGroup
    
    Name                      Key                            VLanId PortBinding NumPorts
    ----                      ---                            ------ ----------- --------
    DPortGroup-xxxx... dvportgroup-15232                     Static      48
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    执行以下命令修改虚拟机的配置,修改端口组。

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4.检查虚拟机状态

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    分布式端口组修改成功。

    参考截图

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    实际操作流程(方式二)

    若觉得设置变量太麻烦,也可直接通过管道符操作,将多条命令连续执行进行操作。
    此处不需要以变量为对象进行操作。
    示例命令:

    Get-VM -Location Datacenter-xxx-xxx -name 10.42.40.*|Get-NetworkAdapter|Set-NetworkAdapter -NetworkName "DPortGroup-xxxxxx-10.42.40.x (1)"|ft -au
    
    • 1

    按管道符分割解释一下各部分就是:

    获取"Datacenter-xxx-xxx"中的名称前缀为"10.42.40.x"的虚拟机|
    获取这些虚拟机的网卡|
    将这些网卡设置名叫"DPortGroup-xxxxxx-10.42.40.x (1)"新的分布式端口组|
    将返回的结果调整为完整显示并自适应宽度
    
    • 1
    • 2
    • 3
    • 4

    执行结果参考如下:

    PS C:\Users\Czm\AppData\Local\Microsoft\WindowsApps> Get-VM -Location Datacenter-xxx-xxx -name 10.42.40.*|Get-NetworkAdapter|ft -au
    
    Name              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:xx: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:xx: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:xx: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:xx: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:xx:e3             True
    Network adapter 1 Vmxnet3 dvportgroup-269                     00:50:56:b6:xx:f5             True
    Network adapter 1 Vmxnet3 dvportgroup-269                     00:50:56:b6:xx:fc             True
    Network adapter 1 Vmxnet3 dvportgroup-269                     00:50:56:b6:xx:39             True
    Network adapter 1 Vmxnet3 dvportgroup-269                     00:50:56:b6:xx: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:xx:91             True
    Network adapter 1 Vmxnet3 DPortGroup-xxxx-10.42.40.x (1) 	  00:50:56:8a:xx:59             True                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    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
    
    • 1
    确认
    是否确实要执行此操作?
    正在目标“Network adapter 1”上执行操作“Setting NetworkName: DPortGroup-xxxxx-10.42.40.x (1)”。
    [Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“Y”): a
    
    Name              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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    完成

  • 相关阅读:
    三元数异或(秋季每日一题 4)
    学习Java的常用开发工具
    YOLOv8改进 | EIoU、SIoU、WIoU、DIoU、FocusIoU等二十余种损失函数
    【Qt之QtConcurrent】描述及使用
    SWAT模型应用
    canvas 轮询http接口让小车实时运动
    Git分布式版本控制工具
    尚硅谷Vue系列教程学习笔记(14)
    芯昇XS9950,单通道多合一同轴高清解码芯片方案,一路模拟高清,一路AHD
    【Web UI自动化测试】Web UI自动化测试之日志收集篇(全网最全)
  • 原文地址:https://blog.csdn.net/weixin_43092290/article/details/134154430