VMware Cloud Community
RobMokkink
Expert
Expert

change all vm's to enhanced vmxnet + ip address

Hi,

I wan't to change the networkcard on all our vm's to enhancedvmxnet, for this i created a script. But one problem resided, and that is configuring the ip adres inside the vm.

Who has got a snippet for that?

Thanks.

Reply
0 Kudos
23 Replies
LucD
Leadership
Leadership

Did you try the code in the ?

Extract

Q: How can I change a VM's IP address?
A: This script can be used to change the IP setting.

connect-viserver -server <server> -user <user> -password <pwd>
 
$vmclonespec = New-Object VMware.Vim.VirtualMachineCloneSpec
$vmclonespec.Customization = New-Object  VMware.Vim.CustomizationSpec
$vmclonespec.Customization.NicSettingMap = @(New-Object VMware.Vim.CustomizationAdapterMapping)
$vmclonespec.Customization.NicSettingMap[0].Adapter = New-Object VMware.Vim.CustomizationIPSettings
$vmclonespec.Customization.NicSettingMap[0].Adapter.ip = New-Object VMware.Vim.CustomizationFixedIp
$vmclonespec.Customization.NicSettingMap[0].Adapter.Ip.IpAddress = "<Target IP Address>"
$vmclonespec.Customization.Identity = New-Object vmware.Vim.CustomizationIdentitySettings
$vmclonespec.Customization.GlobalIPSettings = New-Object VMware.Vim.CustomizationGlobalIPSettings
 
$vmclonespec.config = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmclonespec.location = New-Object VMware.Vim.VirtualMachineRelocateSpec
 
$vmclonespec.powerOn = $false
$vmclonespec.template = $false
$name = "Test"
 
$target = Get-Folder -Name TARGETFOLDER | % {Get-View $_.ID}  
$vmmor = Get-VM VMNAME | Get-View
$vmmor.CloneVM_Task($target.MoRef ,$name, $vmclonespec )


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

RobMokkink
Expert
Expert

Luc,

I didn't check the FAQ. But that doesn't work, because it need more sysprep options.

I get an error: A specified parameter was not correct: Spec.identiy

Reply
0 Kudos
wboaz
Enthusiast
Enthusiast

Can you share your script for replacing a NIC? I need to do that on a couple hundred machines. 😕

Wayne

Reply
0 Kudos
RobMokkink
Expert
Expert

Sorry for the delay, i didn't get a email that the thread was updated.

This is what is have so far:

#ADD VMWARE PSSNAPIN

Add-PSSnapin -Name "VMware.VimAutomation.Core"

#FORCE TO LOAD VMWARE POWERSHELL PLUGIN

[http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("vmware.vim")

#ON ERROR CONTINUE

$ErrorActionPreference = "Continue"

#VARIABLES

$VCS = "localhost"

#----


  1. FUNCTION SHUTDOWN_VM

#----


Function Shutdown_VM

{

Param($VS)

$SHUTDOWNOS = get-vm $VS | Shutdown-VMGuest

#SET THE STATE TO $FALSE

$DOWN = $False

#START A DO LOOP TO CHECK IF THE VM IS REALLY DOWN

Do

{

$CHECKVM = get-vm $VS

$CHECKSTATE = $CHECKVM.Powerstate

if ($CHECKSTATE -eq "PoweredOn")

{

$DOWN = $False

}

else

{

$DOWN = $True

}

}Until($DOWN -eq $True)

}

#----


  1. FUNCTION REPLACE_NIC

#----


Function Replace_Nic

{

Param($VS)

$ADAPTER = Get-NetworkAdapter -VM $VS

$REMOVENIC = Remove-NetworkAdapter -NetworkAdapter $adapter -Confirm:$False

$ADDNIC = New-NetworkAdapter -VM $VS -Type "EnhancedVmxnet" -NetworkName "VM-Network" -StartConnected -Confirm:$False

}

#----


  1. MAIN SCRIPT LOGIC

#----


#CONNECT TO THE vCenter SERVER

connect-viserver $VCS | out-null

#FILL THE ARRAY

$ARRVM = get-vm

#LOOP THROUGH THE ARRAY

foreach ($VMS in $ARRVM)

{

#CHECK IF THE NETWORK CARD IS EnhancedVmxnet

$GETNICS = $VMS | get-networkadapter

$NICTYPE = $GETNICS.Type

if ($NICTYPE -ne "Vmxnet")

{

write-output "$VMS has network card $NICTYPE"

write-output "$VMS needs to be migrated"

#SHUTDOWN THE VM

Shutdown_VM $VMS

write-output "THE VM: $VMS IS SHUTDOWN"

#REPLACE THE NIC

Replace_Nic $VMS

}

else

{

write-output "NOTHING NEEDS TO BE DONE FOR SERVER: $VMS"

}

}

#LOGOUT

disconnect-viserver -Confirm:$False

Reply
0 Kudos
RobMokkink
Expert
Expert

Now the following challenge.

I want to use invoke-vmscript to configure the network inside the vm.

But how can i use the Scripttext with a variable?

Like for instance i create command like:

$strCommand = "netsh interface ip set address name=" + 34 + $NICCARD + 34 + " static " + $oldip + " 255.255.225.0 192.168.0.254 1"

Because then i can finish off my script.

Reply
0 Kudos
RobMokkink
Expert
Expert

I see powershell and netsh don't get along well.

I created a workaround. I also created a snippet that removes the old Ghost network card.

Next week i have the script ready and will post it inside this thread.

Reply
0 Kudos
wboaz
Enthusiast
Enthusiast

I think it's probably easier to get and set the IP address via the WMI provider. I'll work on that once I get off of on-call.

Wayne

Reply
0 Kudos
RobMokkink
Expert
Expert

I have seen some issues when replacing the nic. DHCP wasn't working, i got a lot of RPC errors. Even after cleaning up the old nic and rebooting.

My plan is to mount an iso to the vm and using the invoke-vmscript with devcon to cleanup the old nic (i already have that working) and execute a vbscript on the iso with some parameters, again using invoke-vmscript to set the proper ip address.

But it will be next week when i finish the script.

Reply
0 Kudos
RobMokkink
Expert
Expert

I still have once problem before getting the script to work.

I use a netset command inside the script, and for some reason it does not work with invoke-vmscript is executed.

When i run it from the console there is no problem.

Who has an answer?

Reply
0 Kudos
RobMokkink
Expert
Expert

I am trying to the netset03 issue fixed which i use in the .vbs to set the net ip configuration.

If i use the following:

$strCommand = "C:\windows\systems32\netset03.exe C:\unattend.txt"

Invoke-VMScript -vm $VS -ScriptText $strCommand -GuestUser $ADMINUSER -GuestPassword $ADMINPW -hostuser root -hostpassword $ROOTPW

I get:

The term 'C:\windows\systems32\netset03.exe' is not recognized as a cmdlet, fun

ction, operable program, or script file. Verify the term and try again.

At line:1 char:37

+ &

How can i fix this?

Reply
0 Kudos
RobMokkink
Expert
Expert

Oke i found the problem. It's has to do with 64 bit system redirection. In other words. The vCenter server is 32bits and connecting to a 64 bits VM's , means it will be looking for netset03 in C:\windows\syswow64

I fixed it by putitng the netset03 on the iso.

Reply
0 Kudos
RobMokkink
Expert
Expert

Here is the first version.

Check the variables setions for cddrive and administrator account.

The script works as follows.

  1. You have to select a datacenter and then a cluster.

  2. You will be asked to specify root, admin password for the account you specified, subnetmask, gateway, dns.

  3. Current ip get's collected

  4. iso get's mounted

  5. vm goes down

  6. current nic will be replaced with enhanced vmxnet

  7. vm get's powered on

  8. the old nic will be removed with devcon

  9. the vbs's on the iso will be executed with parameters

  10. iso will be unmounted

  11. vm will be rebooted

Let me know what you think.

Message was edited by:

forgot to remove the test array from the script. this is fixed now

Message was edited by: RobMokkink

Removed the scripts

Reply
0 Kudos
Mnemonic
Enthusiast
Enthusiast

Hi RobMokkink,

What happened to the script?

Regards Mnemonic

Reply
0 Kudos
potherim
VMware Employee
VMware Employee

I could really use this script for a customer that requires thousands of VMs moved from E1000 to VMXNET. Appreciate any help with repost of the script.

Mohan

Reply
0 Kudos
LucD
Leadership
Leadership

The scripts were removed because in the mean time there are cmdlets to do this.

See the Set-NetworkAdapter doc.

Get-VM | Get-NetworkAdapter | where {$_.Type -eq "e1000"} | Set-NetworkAdapter -Type Vmxnet -WhatIf

Remove the -WhatIf parameter if you're happy with actions this line is doing.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
avlieshout
VMware Employee
VMware Employee

Just wondering:

Is there no need to reapply ip addresses anymore?

Is this inplace change fully hidden to the OS by the VMware Tools?

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
Reply
0 Kudos
LucD
Leadership
Leadership

I'm pretty sure you will have to configure the IP settings again inside the OS running on the guest.

For the OS this will be a new NIC.

Unless you get all the config settings (IP addr, DNS servers, gateway...) from the DHCP server(s).


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
avlieshout
VMware Employee
VMware Employee

That's what I was thinking too.

HOWEVER,

I just ran some tests and I was able to replace my flexible adapter with a Vmxnet3 adapter on a running machine without any issues.

When monitoring (ping) the machine I only get 1 ping time-out.

The environment is running ESXi 4.1 and the VM is running the latest VMware Tools and HW level 7.

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
Reply
0 Kudos
LucD
Leadership
Leadership

What OS do you run in the guest ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos