Created
March 31, 2015 21:35
-
-
Save KyleMartin901/dece3f19a1973e9a7533 to your computer and use it in GitHub Desktop.
Convert VMware VM NIC adapter from e1000 to VMXNET3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-PSSnapin VMware.VimAutomation.Core | |
Add-PSSnapin VMware.VimAutomation.VDS | |
$viserver = 'vi_server' | |
$credentials = Get-Credential | |
Connect-VIServer -Server $viserver -Credential $credentials | |
Write-Output | |
#Adpter type to retrive | |
$vm_adapter_type = "e1000" | |
$server = "vm_name" | |
$Data = @() | |
$VM = Get-VM -Name $server | Where {$_.PowerState -eq "PoweredOn"} | |
#Take snapshot | |
New-Snapshot -VM $VM -Name "Before NIC Update to VMXNET3" | |
Try { | |
$NICs = Get-NetworkAdapter -VM $VM | Where-Object {$_.type -eq $vm_adapter_type} | |
$NICs | |
ForEach ($NIC in $NICs) { | |
Write-Output $NIC.Id | |
$INTERFACE = Get-VMGuestNetworkInterface -VM $VM -GuestCredential $credentials -ErrorAction Stop | Where-Object {$_.NicId -eq $NIC.Id} | |
$INTERFACE | |
$hash = @{ | |
ip = $INTERFACE.Ip | |
netmask = $INTERFACE.SubnetMask | |
gateway = $INTERFACE.DefaultGateway | |
dns = $INTERFACE.Dns | |
dnspolicy = $INTERFACE.DnsPolicy | |
ippolicy = $INTERFACE.IPPolicy | |
nicid = $INTERFACE.NicId | |
vmId = $INTERFACE.VMId | |
} | |
set-networkadapter -NetworkAdapter $NIC -type vmxnet3 -confirm:$false | |
$Object = New-Object PSObject -Property $hash | |
$Data += $Object | |
$Data | |
} | |
#Disable IPv6, edit the registry | |
Write-Output "Disabling IPv6" | |
$script = ' | |
function Elevate-Process { | |
param ([string]$exe = $(Throw "Pleave provide the name and path of an executable"),[string]$arguments) | |
$startinfo = new-object System.Diagnostics.ProcessStartInfo | |
$startinfo.FileName = $exe | |
$startinfo.Arguments = $arguments | |
$startinfo.verb = "RunAs" | |
$process = [System.Diagnostics.Process]::Start($startinfo) | |
} | |
Elevate-Process -Exe powershell.exe -Arguments "-noninteractive -command reg add hklm\system\currentcontrolset\services\tcpip6\parameters /v DisabledComponents /t REG_DWORD /d 255 /f" | |
' | |
Invoke-VMScript -VM $VM -ScriptText $script -ScriptType PowerShell | |
Shutdown-VMGuest -VM $VM -Confirm:$false | |
do { | |
sleep 1.0 | |
} while (Get-VM $VM | Where {$_.PowerState -eq "PoweredOn"}) | |
Write-Output "Shutdown Complete" | |
Start-VM -VM $VM -Confirm:$false | |
do { | |
sleep 1.0 | |
} while (Get-VM $VM | Get-VMGuest | Where {$_.State -ne "Running"}) | |
sleep 20.0 | |
Write-Output "Startup Complete" | |
$NICs = Get-NetworkAdapter -VM $VM | |
ForEach ($NIC in $NICs) { | |
ForEach ($object in $Data) { | |
IF ($object.nicid -eq $NIC.ID) { | |
$failed = 1 | |
do { | |
Try { | |
Write-Output "Attempting to get $NIC details" | |
$vminterface = Get-Vm -Id $object.vmId -ErrorAction Stop | Get-VMGuestNetworkInterface -GuestCredential $credentials -ErrorAction Stop | Where-Object {$_.NicId -eq $NIC.Id} -ErrorAction Stop | |
$failed = 0 | |
} | |
Catch [system.exception] { | |
Write-Output "Caught an exception: " $Error[0].Exception.Message -ForegroundColor Red | |
$failed = 1 | |
} | |
} until ( $failed -eq 0) | |
if ($object.gateway -ne "") { | |
Write-Output "Setting $NIC details" | |
Set-VMGuestNetworkInterface -VmGuestNetworkInterface $vminterface -Dns $object.dns -DnsPolicy $object.dnspolicy -Gateway $object.gateway -Ip $object.ip -IPPolicy $object.ippolicy -Netmask $object.netmask | |
Write-Output "$NIC has been configured" | |
} Else { | |
Write-Output "Setting $NIC details" | |
Set-VMGuestNetworkInterface -VmGuestNetworkInterface $vminterface -Ip $object.ip -IPPolicy $object.ippolicy -Netmask $object.netmask | |
Write-Output "$NIC has been configured" | |
} | |
} | |
} | |
} | |
} Catch { | |
Write-Output "Unable to query the $VM network interface" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment