[How To]: Change Linux IP Address via PowerCLI
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.
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
#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