VMware Cloud Community
Ryo-Ohki
Contributor
Contributor

Changing IP address of VM

Hi there,

Struggling with Powershell to implement a DR automation process here (SRM is not an option). We have two sites with A/SRDF replication between two independant VMware infrastructures.

I'm at the point where I need to build a routine to take every replicated VM and change their IP address to match the IP range of the new site. I've tried the script on the FAQ http://communities.vmware.com/docs/DOC-4210 but with little success. Using it "as-is" doesn't work : it misses some further definition for the "Identity" object, which I added, and its purpose is apparently to clone the VM, which is not my goal.

I've done a little more research and somehow adapted the script so that it uses the CustomizeVM_Task instead. Unfortunately this launches Sysprep inside the VM and apparently requires a full Sysprep customization to proceed. This is a bit too heavy for a simple IP reconfiguration! (for the sake of the debate the script is attached, with my source CSV file I use to identify the VM to reconfigure into the DR datacenter).

Is it possible to JUST change the IP of the VM without running a Sysprep operation? If not, I thought about running a command inside the VM, a la VIX, is it possible through PowerShell? (using VIX itself is not really an option so far since the infrastructure is still running ESX 3.0.2).

Thanks.

0 Kudos
4 Replies
ggochkov
VMware Employee
VMware Employee

In case the OS cust. spec does not support changing IP, you can do this through Invoke-VmScript (of course powershell should be installed on the VM):

<code>

$cmd = "netsh interface IP set address local static 192.32.99.76 255.255.0.0 10.242.0.254 1"

#$cmd = "netsh interface IP set address local dhcp" - in case you want DHCP:

Invoke-VMScript -VM TESTWIN64R2 -ScriptText $cmd -HostUser <vmHostUser - the user of the host of the VM> -HostPassword <vmHostPassword> -GuestUser <VM user> -GuestPassword <VM password>

</code>

LucD
Leadership
Leadership

If your guests are all Windows machines and if you have PowerShell installed on the guests you could use the Invoke-VMScript cmdlet.

See for a (draft ?) list of requirements to run Invoke-VMScript.

The following script shows how you could do this


$esxUser = <ESX-user>
$esxPswd = <ESX-password>
$vmUser = <guest-user>
$vmPswd = <guest-password>

$ipshow = "netsh interface ip show config"
$ipset1 = "netsh interface ip set address name="
$ipset2 = " static "

$regIP = [regex]"[\s]+IP Address:[\s]+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
$regSM = [regex]"[\s]+SubnetMask:[\s]+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
$regGW = [regex]"[\s]+Default Gateway:[\s]+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
$regGWM = [regex]"[\s]+GatewayMetric:[\s]+([\d]+"
$regLAN = [regex]"Configuration for interface `"([\w\s]+)`""

$vm = Get-VM <VM-name>

$cmd = $ipshow
$text = Invoke-VMScript -VM $vm -ScriptText $cmd `
   -GuestUser $vmUser -GuestPassword $vmPswd `
   -HostUser $esxUser -HostPassword $esxPswd
   
$ip = $regIP.Match($text).Groups[1].Value
$mask = $regSM.Match($text).Groups[1].Value
$gateway = $regGW.Match($text).Groups[1].Value
$lanName = $regLAN.Match($text).Groups[1].Value
$gwMetric = $regGWM.Match($text).Groups[1].Value

# Reuse part of the old settings or define new values
$newLAN = $lanName
#$newIP = $ip
$newIP = <new-IP-address>
$newMask = $mask
$newGateway = $gateway
$newGWMetric = $gwMetric

$cmd = $ipset1 + "`"" + $lanName + "`"" + $ipset2 + $newIp + " " +  $newMask + " " + $newGateway + " " + $newGWMetric
Invoke-VMScript -VM $vm -ScriptText $cmd `
   -GuestUser $vmUser -GuestPassword $vmPswd `
   -HostUser $esxUser -HostPassword $esxPswd

Message was edited by: LucD

Just noticed there was already a reply.

Should have refreshed my browser before answering.

Sorry about that


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Ryo-Ohki
Contributor
Contributor

thanks for the pointer... Will do some testing and consider it if I have no other choice. Marked as helpful answer.

Sorry for Luc, a close second Smiley Wink

0 Kudos
alasdair_carnie
Enthusiast
Enthusiast

Hi All,

Is there a way to do this in from Powershell for Ubuntu Linux VMs? Or is there a bit of remote script I could run to achieve the same result?

0 Kudos