VMware Cloud Community
Itzikr
Enthusiast
Enthusiast

a script to automate PVSCSI and VMXnet3

Hi,

im doing ng an enviornment upgrade form VI3 to vSphere and want to upgrade the vm's in an automated way as much as i can

vmware update manager offer automatic upgrade to the vmware tools and the VM virtual hardware...

but what if i want to switch the vmxent2/flexible nic with the new VMxnet3?

also, what about injecting the PVSCSI drivers to the OS, shut down the VM and replace the controller with the new PVSCSI adpater..

any ideas will be gladly received

Itzik

Itzik Reich
Reply
0 Kudos
1 Reply
Herschelle
Enthusiast
Enthusiast

Here is what I wrote to do a similar thing, when changing all existing VMs that had the default e1000 nics. In my environment though all my VMs are connected to vNetwork Distributed Switches, but the process should be similar.

# Description: Adds an vmxnet3 nic to a vm and connects it to the same virtual 
#      port group (on a vNetwork Distributed Switch) as the first nic on the vm.
# Author: Herschelle42
# Usage 1: Add-Additional-VMXNet3NIC.ps1 <VM as [object]>
# Usage 2: Add-Additional-VMXNet3NIC.ps1 (Get-VM -Name "MyVMName")
# Uasge 3: $vm = Get-VM -Name "MyVMName"
#          Add-Additional-VMXNet3NIC.ps1 $vm

Process {
	#Firstly check that the parameter that has been passed is a VM object
	if ( $_ -isnot [http://VMware.VimAutomation.Client20.VirtualMachineImpl|http://VMware.VimAutomation.Client20.VirtualMachineImpl] ) {
		Write-Output "Invalid input recieved: A Virtual Machine object is expected."
		continue
	} # end if

	$vmview = Get-View -viewtype VirtualMachine -filter @{"Name" = $_.Name}
	Write-Output $_.Name

	#Get the vNetwork Port group the VM is currently attached to
	$dvPortGroup = $vmview.Network[0].Value
	#dvportgroup-191
	
	#Get the Switch UUID of the current Network interface.
	foreach ($item in $vmview.Config.Hardware.Device) { 
		if($item.Backing.Port.PortgroupKey -eq $dvPortGroup) {
			$SwitchUUID=$item.Backing.Port.SwitchUuid
			#Write-output "SwitchUUID: "$SwitchUUID
			#f3 40 20 50 15 c0 d1 8d-02 4d f8 b4 48 9b df 06
		} # end if
	} # end foreach
		
	# Now add a new NIC of vmxnet3 to the same network.
	$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
	$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)
	$spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec
	$spec.deviceChange[0].operation = "add"
	$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualVmxnet3
	$spec.deviceChange[0].device.key = -100
	$spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualEthernetCardDistributedVirtualPortBackingInfo
	$spec.deviceChange[0].device.backing.port = New-Object VMware.Vim.DistributedVirtualSwitchPortConnection
	$spec.deviceChange[0].device.backing.port.switchUuid = $SwitchUUID
	$spec.deviceChange[0].device.backing.port.portgroupKey = $dvPortGroup
	$spec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo
	$spec.deviceChange[0].device.connectable.startConnected = $true
	$spec.deviceChange[0].device.connectable.allowGuestControl = $true
	$spec.deviceChange[0].device.connectable.connected = $true
	$spec.deviceChange[0].device.controllerKey = 100
	$spec.deviceChange[0].device.addressType = "generated"
	$spec.deviceChange[0].device.wakeOnLanEnabled = $true
	
	$_this = Get-View -Id $vmview.MoRef
	$_this.ReconfigVM_Task($spec)
    
  Write-Output "Script Finished"

} # end process

Something to note that this will not move any IP address configuration from the e1000 to the vmxnet3 nic, you will need to do this inside the Guest O/S.

Another thing is that before you remove the old nic from the VM, ensure you have disabled and or uninstalled the e1000 nic from within the guest os. This is for Windows guests anyway, otherwise you will end up with devices in device manager. And if you do not removed any fixed IP address from the e1000 you will not be able to assign that same IP address to the vmxnet3 nic as it will already "be in use" according to Windows. Just a trap I fell into myself.

So for my Windows VMs I basically did the following steps.

1. Ran my script to add a vmxnet3 nic

2. Open a Vi Client console window and within Windows change the old nic to dhcp

3. Set new nic to the fixed ip address

4. disable old nic

5. Open device manager, uninstall the old e1000 nic

6. Remove the old nic from the VM.

Of course if your VMs do not have any fixed IP and only use DHCP you can skip some of the steps. Smiley Happy

Hope that helps.

Herschelle42

Reply
0 Kudos