1 2 Previous Next 23 Replies Latest reply on Feb 11, 2011 1:32 PM by potherim

    change all vm's to enhanced vmxnet + ip address

    RobMokkink Expert

       

      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.

       

       

        • 1. Re: change all vm's to enhanced vmxnet + ip address
          LucD Guru
          Community WarriorsvExpertUser Moderators

          Did you try the code in the  Managing VMware with PowerShell FAQ ?

           

          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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          1 person found this helpful
          • 2. Re: change all vm's to enhanced vmxnet + ip address
            RobMokkink 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

             

             

             

             

             

            • 3. Re: change all vm's to enhanced vmxnet + ip address
              wboaz Novice

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

               

               

               

               

               

               

               

              Wayne

              • 4. Re: change all vm's to enhanced vmxnet + ip address
                RobMokkink 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 

                 

                 

                • 5. Re: change all vm's to enhanced vmxnet + ip address
                  RobMokkink 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.

                   

                   

                  • 6. Re: change all vm's to enhanced vmxnet + ip address
                    RobMokkink 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.

                     

                     

                    • 7. Re: change all vm's to enhanced vmxnet + ip address
                      wboaz Novice

                       

                      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

                       

                       

                      • 8. Re: change all vm's to enhanced vmxnet + ip address
                        RobMokkink 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.

                        • 9. Re: change all vm's to enhanced vmxnet + ip address
                          RobMokkink 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?

                           

                           

                          • 10. Re: change all vm's to enhanced vmxnet + ip address
                            RobMokkink 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?

                             

                             

                             

                             

                             

                            • 11. Re: change all vm's to enhanced vmxnet + ip address
                              RobMokkink 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.

                              • 12. Re: change all vm's to enhanced vmxnet + ip address
                                RobMokkink 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

                                • 13. Re: change all vm's to enhanced vmxnet + ip address
                                  Brian Knutsson Enthusiast
                                  vExpert

                                  Hi RobMokkink,

                                   

                                  What happened to the script?

                                   

                                  Regards Mnemonic

                                  • 14. Re: change all vm's to enhanced vmxnet + ip address
                                    potherim Lurker
                                    VMware EmployeesvExpert

                                    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

                                    1 2 Previous Next