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
Typo (“Credntial”) in code.
Thanks!
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.
Check guest credential, you need to root permission to change the file.
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
Hi,
Thank you for comment.
Please read this post: https://www.teimouri.net/invoke-vmscript-challenges-with-linux-guest/
The post is about Invoke-VMScript and Linux bash commands or script.
You can do it same as the example in the post.
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
You’re welcome.
It’s too easy, instead of run commands via Invoke-VMScript, create a bash script within the guest OS by run Linux commands.
Then run the bash script.
In this post: https://www.teimouri.net/invoke-vmscript-challenges-with-linux-guest/
I’ve added all commands to a script file and I’ve made it executable.
I’ve not had a chance to try this, though I will…
I was wondering about the sed syntax…. generally sed is
s/this/that/
substitute this with that.
Your example has the newIP and oldIP in the wrong place.
s|$NIPAddress|$OIPAddress|
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).
Hi,
That’s an example, you can customize the script as you wish.
Thanks for comment.