VMware Cloud Community
zare_N0222
Enthusiast
Enthusiast
Jump to solution

Change Ip address on VM

Hi,

I am looking how can change IP address on 25 VM . on VMs are is windows os and all had install vmware tool.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Then you should be able to do something like this.
You might have to add credentials for the guest on the Invoke-VMScript cmdlet.

$code = @'

`$if = Get-NetAdapter -Physical | where{`$_.LinkLayerAddress -eq '$mac'}

New-NetIPAddress -InterfaceIndex `$if.ifIndex -IPAddress $ip -PrefixLength $prefix

'@


$vmName = 'MyVM'

$ip = '192.168.1.111'

$prefix = '24'


$vm = Get-VM -Name $vmName

$nic = Get-NetworkAdapter -VM $vm -Name 'Network adapter 1'

$mac = $nic.MacAddress.Replace(':', '-')


Invoke-VMScript -VM $vm -ScriptType Powershell -ScriptText $ExecutionContext.InvokeCommand.ExpandString($code)

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

Which Windows version do these VMs have?
Is PowerShell enabled in the guest OS?


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

0 Kudos
zare_N0222
Enthusiast
Enthusiast
Jump to solution

HI , LucD

Its windows 2016 and there are in workgroup no domain Smiley Sad.

Powershell is enabled.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then you should be able to do something like this.
You might have to add credentials for the guest on the Invoke-VMScript cmdlet.

$code = @'

`$if = Get-NetAdapter -Physical | where{`$_.LinkLayerAddress -eq '$mac'}

New-NetIPAddress -InterfaceIndex `$if.ifIndex -IPAddress $ip -PrefixLength $prefix

'@


$vmName = 'MyVM'

$ip = '192.168.1.111'

$prefix = '24'


$vm = Get-VM -Name $vmName

$nic = Get-NetworkAdapter -VM $vm -Name 'Network adapter 1'

$mac = $nic.MacAddress.Replace(':', '-')


Invoke-VMScript -VM $vm -ScriptType Powershell -ScriptText $ExecutionContext.InvokeCommand.ExpandString($code)

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

0 Kudos
zare_N0222
Enthusiast
Enthusiast
Jump to solution

Is this script for changing for MAC  or IP?

To me, its look its for MAC

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, it uses the MAC address to find the correct NIC inside the guest OS.


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

0 Kudos
zare_N0222
Enthusiast
Enthusiast
Jump to solution

Thanks Lucd , I try script and it add a new ip address like a second one. Old IP address is still  ramain.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is correct, the New-NetIPAddress creates a new IP address and makes the existing IP address a secondary address.

If you want to remove the old IP address, you will have to use Remove-NetIPAddress first, before doing the New-NetIPAddress.


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

0 Kudos