为避免网络IP冲突,虚拟机模板的网卡设置是连接中断、开机连接中断的,在通过PowerCLI批量发布虚拟机后,本文尝试PowerCLI通过PowerCLI批量修改虚拟机网卡的连接状态及开机连接设置。
详见前文
VMWare vSphere 7.0.3环境通过PowerCLI批量发布openeuler22.03LTS系统虚拟机
如下示例文件:
Name
GZGL_192.168.122.6_euler
GZGL_192.168.122.7_euler
GZGL_192.168.122.8_euler
GZGL_192.168.122.9_euler
GZGL_192.168.122.10_euler
GZGL_192.168.122.17_euler
GZGL_192.168.122.18_euler
GZGL_192.168.122.19_euler
GZGL_192.168.122.20_euler
GZGL_192.168.122.26_euler
GZGL_192.168.122.27_euler
GZGL_192.168.122.28_euler
#filename:batcon-vms.ps1
##__author__='daigjianbing'
$vcenterip = "your vCenter Ip"
$vcenteruser = "[email protected]"
#获取密码,避免明文保存
$vcenterpw = Read-Host "Please enter your password of the vcenter: $vcenterip username: $vcenteruser"
#连接服务器
Write-Host "Connect-VIServer -Protocol https -User $vcenteruser -Password $vcenterpw -Server $vcenterip"
#测试下句前面加#,实作时去掉
Connect-VIServer -Protocol https -User $vcenteruser -Password $vcenterpw -Server $vcenterip
#虚拟机清单文件为脚本同目录下的“batcon-vms.csv”文件
$vms_listfile = ".\batcon-vms.csv"
$vms = import-csv $vms_listfile
foreach ($vm in $vms)
{
#读取配置文件中的相关配置
$vmname = $vm.Name
# 设置网络适配器的期望连接状态,$true表示连接,$false表示断开
$desiredState = $true
# 设置网络适配器的期望开机连接状态,$true表示开机连接,$false表示开机不连接
$desiredStartState = $true
# 获取当前虚拟机对象
$vm = Get-VM -Name $vmname
# 获取当前虚拟机的所有网络适配器
#Get-NetworkAdapter -VM $vm
# 获取当前虚拟机的'Network adapter 1'网络适配器对象
$networkAdapter = Get-NetworkAdapter -VM $vm | Where-Object { $_.Name -eq 'Network adapter 1' }
Write-Host "The Network adapter 1:$networkAdapter"
# 设置网络适配器的连接状态和开机连接状态
Set-NetworkAdapter -NetworkAdapter $networkAdapter -Connected:$true -StartConnected:$true -Confirm:$false
# 获取当前虚拟机的所有网络适配器
Get-NetworkAdapter -VM $vm
}
#断开vCenter连接
Disconnect-VIServer -Server $vcenterip -Confirm:$false
在powershell中执行.\batcon-vms.ps1
PS D:\vmware\批量创建虚拟机脚本> .\batcon-vms.ps1
Please enter your password of the vcenter: 192.168.188.82 username: administrat
[email protected]: mypasswd
Connect-VIServer -Protocol https -User [email protected] -Password mypasswd -Server 192.168.188.82
Name Port User
---- ---- ----
192.168.188.82 443 VSPHERE.LOCAL\Administrator
MacAddress : 00:50:56:ab:6d:13
WakeOnLanEnabled : True
NetworkName : VLAN_2183
Type : Vmxnet3
ParentId : VirtualMachine-vm-27255
Parent : GZGL_192.168.122.6_euler
Uid : /VIServer=vsphere.local\[email protected]:443/Vir
tualMachine=VirtualMachine-vm-27255/NetworkAdapter=4000/
ConnectionState : NotConnected, GuestControl, NoStartConnected
ExtensionData : VMware.Vim.VirtualVmxnet3
Id : VirtualMachine-vm-27255/4000
Name : Network adapter 1
The Network adapter 1:Network adapter 1
MacAddress : 00:50:56:ab:6d:13
WakeOnLanEnabled : True
NetworkName : VLAN_2183
Type : Vmxnet3
ParentId : VirtualMachine-vm-27255
Parent : GZGL_192.168.122.6_euler
Uid : /VIServer=vsphere.local\[email protected]:443/Vir
tualMachine=VirtualMachine-vm-27255/NetworkAdapter=4000/
ConnectionState : Connected, GuestControl, StartConnected
ExtensionData : VMware.Vim.VirtualVmxnet3
Id : VirtualMachine-vm-27255/4000
Name : Network adapter 1
MacAddress : 00:50:56:ab:6d:13
WakeOnLanEnabled : True
NetworkName : VLAN_2183
Type : Vmxnet3
ParentId : VirtualMachine-vm-27255
Parent : GZGL_192.168.122.6_euler
Uid : /VIServer=vsphere.local\[email protected]:443/Vir
tualMachine=VirtualMachine-vm-27255/NetworkAdapter=4000/
ConnectionState : Connected, GuestControl, StartConnected
ExtensionData : VMware.Vim.VirtualVmxnet3
Id : VirtualMachine-vm-27255/4000
Name : Network adapter 1
....
可以看到相应虚拟机的网卡连接状态已由“NotConnected, GuestControl, NoStartConnected”设置为“Connected, GuestControl, StartConnected”,即当前连接,开机连接。
登录vCenter进行验证:
确实OK了。
附:以“Set-NetworkAdapter”指令为例,PowerCLI通过Get-Help获取指令帮助及获取指令样例的方法
PS D:\vmware\批量创建虚拟机脚本> Get-Help Set-NetworkAdapter
名称
Set-NetworkAdapter摘要
This cmdlet modifies the configuration of the virtual network adapter.
语法
Set-NetworkAdapter [-NetworkAdapter][-Connected n>] [-MacAddress ] [-NetworkName ] [-RunAsync] [-Server Server[]>] [-StartConnected ] [-Type ]
[-WakeOnLan] [-Confirm] [-WhatIf] [ ] Set-NetworkAdapter [-NetworkAdapter]
[-Connected n>] -DistributedSwitch [-MacAddress ] -PortId <
String> [-RunAsync] [-Server] [-StartConnected ] [-Ty
pe] [-WakeOnLan ] [-Confirm] [-WhatIf]
[] Set-NetworkAdapter [-NetworkAdapter]
-Portgroup PortGroupBase> [-RunAsync] [-Server ] [-Confirm] [-WhatIf] [ mmonParameters>]
说明
This cmdlet modifies the configuration of the virtual network adapter. You
can change the MAC address and the network name, and to configure the Conne
cted, StartConnected, and WakeOnLan properties of the adapter.
相关链接
Online Version: http://developer.vmware.com/docs/powercli/latest/vmware.vim
automation.core/commands/set-networkadapter
Get-NetworkAdapter
New-NetworkAdapter
Remove-NetworkAdapter备注
若要查看示例,请键入: "get-help Set-NetworkAdapter -examples".
有关详细信息,请键入: "get-help Set-NetworkAdapter -detailed".
若要获取技术信息,请键入: "get-help Set-NetworkAdapter -full".
有关在线帮助,请键入: "get-help Set-NetworkAdapter -online"
PS D:\vmware\批量创建虚拟机脚本> get-help Set-NetworkAdapter -examples
名称
Set-NetworkAdapter摘要
This cmdlet modifies the configuration of the virtual network adapter.
-------------------------- Example 1 --------------------------Get-VM VM | Get-NetworkAdapter | Set-NetworkAdapter -MacAddress '00:50:56:a
1:00:00' -WakeOnLan:$trueConfigures the Mac address and the WakeOnLan setting of a virtual network
adapter.
-------------------------- Example 2 --------------------------Get-VM VM | Get-NetworkAdapter | Set-NetworkAdapter -Type EnhancedVmxnet
Sets the type of the virtual network adapter.
-------------------------- Example 3 --------------------------Get-VM VM | Get-NetworkAdapter | Set-NetworkAdapter -Connected:$true
Sets the connection state of the virtual network adapter.
-------------------------- Example 4 --------------------------$myNetworkAdapters = Get-VM | Get-NetworkAdapter -Name "Network adapter 1"
$myVDPortGroup = Get-VDPortgroup -Name MyVDPortGroup
Set-NetworkAdapter -NetworkAdapter $myNetworkAdapters -Portgroup $myVDPortG
roupRetrieves all network adapters named "Network adapter 1" from all virtual m
achines and connects them to the specified distributed port group.
-------------------------- Example 5 --------------------------$myNetworkAdapter = Get-VM -Name MyVM | Get-NetworkAdapter -Name "Network a
dapter 1"
$myVDSwitch = Get-VDSwitch -Name MyVDSwitch
Set-NetworkAdapter -NetworkAdapter $myNetworkAdapter -DistributedSwitch $My
VDSwitch -PortId 100Retrieves the network adapter named "Network adapter 1" added to the specif
ied virtual machine and connects it to the specified port on the specified
distributed switch.