Change Linux IP Address via PowerCLI

There is some reason that you want to change IP address on virtual machines as an example, moving virtual machines to a new VLAN or port-group or moving them to another site or replicate virtual machine to an offsite recovery site.

Changing IP on machines one by one is very difficult on more than one machine and it be tougher.

PowerCLI has cmdlet that it called “Invoke-VMScript”, the cmdlet lets you to run scripts on guest OS even when guest OS has no IP. But VMware Tools must be installed and also updated VMware Tools is recommended.

Also you must have guest OS credential to run your script.

Sample Script for Change IP Address

Here is a sample script to change address on multiple VM and also change NFS mount point addresses on “fstab” file.

Before run, you must customize the below script and put proper network configuration file within the script:

Further Reading

Add existing virtual desktops to “Automated Pool” in VMware View manually

[PowerCLI]: Change Local Account Password on ESXi

[Script]: Find ESXi HBA WWN via PowerCLI

Veeam Backup & Replication – Re-IP Rule on Linux VM

Davoud Teimouri

Professional blogger, vExpert 2015/2016/2017/2018/2019/2020/2021/2022/2023, vExpert NSX, vExpert PRO, vExpert Security, vExpert EUC, VCA, MCITP. This blog is started with simple posts and now, it has large following readers.

11 Responses

  1. darwa says:

    Typo (“Credntial”) in code.

  2. kulvinder says:

    Hello, I am trying to change the IP for RHEL VM and getting this error. Can you please advise.

    Invoke-VMScript : 2019-08-11 10:20:30 PM Invoke-VMScript Error occured while executing script on guest OS in VM ‘nicedep01’. Could not locate “Powershell” script interpreter in any of the expected locations. Probably you do not have enough permissions to
    execute command within guest.

  3. Kulvinder says:

    Hi Davoud

    I added this “-scripttype bash” and now I am able to proceed but now getting error.

    PS C:\WINDOWS\system32> Get-VM “$VM_dep01” | Invoke-VMScript -ScriptText ‘sed -ri “s|$vm_dep01_new_ip|$VM_dep01_org_ip|” /etc/sysconfig/network-scripts/ifcfg-ens160 && cat /etc/sysconfig/network-scripts/ifcfg-ens160′ -GuestCredential $credential -scripttype bash

    ———————————————————————————————————————–| bash: -c: line 0: syntax error near unexpected token `;&’
    | bash: -c: line 0: `sed -ri “s|$vm_dep01_new_ip|$VM_dep01_org_ip|” /etc/sysconfig/network-scripts/ifcfg-ens160 && cat /etc/sysconfig/network-scripts/ifcfg-ens160′

    I removed “;&” and then it give me this error

    PS C:\WINDOWS\system32> Get-VM “$VM_dep01” | Invoke-VMScript -ScriptText ‘sed -ri “s|$vm_dep01_new_ip|$VM_dep01_org_ip|” /etc/sysconfig/network-scripts/ifcfg-ens160 & cat /etc/sysconfig/network-scripts/ifcfg-ens160’ -GuestCredential $credential -scripttype bash

    ———————————————————————————————————————–| sed: -e expression #1, char 0: no previous regular expression
    | bash: amp: command not found

  4. kulvinder says:

    Hi Davoud, Thanks for your quick reply..

    After some quick tricks I am able to use that, but only when I pass the actual value and not as variable
    like sed -ri “s|||

    I just seen the other link which you mentioned, But I am not sure how can I use that.
    Can you give some more details when you have time.

    I am trying to achieve this
    Change the hostname of 6 linux VM
    Change the IP, SM, DG and DNS for those 6 VM’s
    Add entry in /etc/fstab for mounting the NFS

    At present I have to do this multiple time as I have to create and destroy this 6 VM cluster very often
    Thanks a ton for all your Help

  5. Jon Tankersley says:

    I’ve not had a chance to try this, though I will…
    I was wondering about the sed syntax…. generally sed is
    substitute this with that.
    Your example has the newIP and oldIP in the wrong place.
    which SHOULD fail.

    Also, this assumes 1) eth0 and not another NIC, and doesn’t update /etc/hosts which may have the IP also.
    Lastly, this will generally require a few more steps… a reboot at the least since you are updating the fstab for possible hard coded IP addresses there (again, /etc/hosts would catch that).

Leave a Reply

Your email address will not be published. Required fields are marked *