[How To]: 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


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

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

#Get Guest OS Credential
$Credential=Get-Credential
#Get VM Names (As Name Template) - Example Test-2*
$VM=Read-Host "Please Enter The VMs Template Name"
#Get IP Address Range - Example 10.1.1 or 192.168 - This IP Range Will Be Used For Replacing
$NIPAddress=Read-Host "Please Enter New IP Range"
$OIPAddress=Read-Host "Please Enter Current IP Range"
#Replace New IP Range With Old IP Range
Get-VM "$VM" | Invoke-VMScript -ScriptText 'sed -ri "s|$NIPAddress|$OIPAddress|" /etc/sysconfig/network-scripts/ifcfg-eth0 && cat /etc/sysconfig/network-scripts/ifcfg-eth0' -GuestCredential $credential
Get-VM "$VM" | Invoke-VMScript -ScriptText 'sed -ri "s|$NIPAddress|$OIPAddress|" /etc/fstab && cat /etc/fstab' -GuestCredential $credential

Read More on Teimouri.Net:

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

Davoud Teimouri is as a professional blogger, vExpert 2015/2016/2017/2018/2019, VCA, MCITP. This blog is started with simple posts and now, it has large following readers.

9 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

    ScriptOutput
    ———————————————————————————————————————–| 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

    ScriptOutput
    ———————————————————————————————————————–| 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|192.168.0.25|192.168.20.5|

    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

  1. 27/05/2019

    […] [How To]: Change Linux IP Address via PowerCLI […]

Leave a Reply

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

Subscribe to our newsletter and join other subscribers

Holler Box