VMware Cloud Community
Proxmire
Contributor
Contributor
Jump to solution

Changing VM IP Address from a list

Hello everyone,

I was trying to use PowerCLI to change the VM IP addresses from a list in a text file.  I recently learned about the Invoke-VMScript cmdlet and tried using it instead of possibly performing this task with the Get-WMI win32_networkadapterconfiguration cmdlet and Enablestatic method.  I attempted the following script:

$b=Get-VM | %{

  foreach($nic in $_.Guest.Nics){
    Write-output $nic.IPAddress}
}

$a = get-content c:\scripts\IPaddress.txt

for($i=0; $i -lt $a.count; $i++){

$sm="X.X.X.X"
$dg="X.X.X.X"

Invoke-VMScript -VM $b[$i] -ScriptText "c:\windows\system32\netsh.exe interface ip set address

name=""Local Area Connection"" static $a[$i] $sm $dg 1" -ScriptType bat

}

$b is the list of VMs on the host and their IP addresses would be replaced with $a, which is the list of new IP addresses generated before hand.  I used $i as both the loop to stop the script when it reaches the end of list $a, but also as the array number corresponding to one VM per each new IP address.  So ideally in my head it would work like this:  $i starts out at 0 which is the first line of both variables $b and $a, change one VM with the first IP address on the list, increase $i by 1, loop back and repeat with the next VM and address until the end.

The error message I received when running the script is:

Invoke-VMScript Could not find Virtual Machine with name 'X.X.X.X'

Any feedback would be greatly appreciated and please feel free to  comment if you might know of any other ways that may change the VM IP addresses from a list using any other functions.

Best regards

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The $b variable contains the IP addresses of all NICs in your guests.

Unless your guests are also registered with the IP address, PowerCLI will not be able to find the VM.

You have to use the Name property.

The 2nd problem is that the file IPaddress.txt needs to have a column that states for which VM and which NIC the new IP address is intended.

It would be easier if you use a CSV file with a layout like this

VM,NICname,IPaddr

vm1,Local Area Connection 1,192.168.1.1

vm2,Local Area Connection 1,192.168.1.2

Note that there is also the Set-VMGuestNetworkInterface cmdlet, which is in fact a script, provided by the PowerCLI Dev Team, that is passed to the guest's OS through the Invoke-VMScript cmdlet. It uses the same netsh command you are using.

Your script could become something like this, provided you use a CSV as input like I explained above.

$esxCred = Get-Credential -Credential "ESX host user"

$sm="X.X.X.X"

$dg="X.X.X.X"

foreach($line in Import-Csv c:\scripts\IPaddress.csv){

   $vmCred = Get-CRedential -Credential ("VM OS user for " + $line.VM)

   Get-VM -Name $line.VM | Get-VMGuestNetworkInterface -HostCredentials $esxCred -GuestCredentials $vmCred | `

      where {$_.Name -eq $line.NICname} | `

      Set-VMGuestNetworkInterface -Ip $line.IPaddr -Netmask $sm -Gateway $dg -HostCredentials $esxCred -GuestCredentials $vmCred

}

Note that this assumes that all the VM are on hosts that can use the same host credential.


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

View solution in original post

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

The $b variable contains the IP addresses of all NICs in your guests.

Unless your guests are also registered with the IP address, PowerCLI will not be able to find the VM.

You have to use the Name property.

The 2nd problem is that the file IPaddress.txt needs to have a column that states for which VM and which NIC the new IP address is intended.

It would be easier if you use a CSV file with a layout like this

VM,NICname,IPaddr

vm1,Local Area Connection 1,192.168.1.1

vm2,Local Area Connection 1,192.168.1.2

Note that there is also the Set-VMGuestNetworkInterface cmdlet, which is in fact a script, provided by the PowerCLI Dev Team, that is passed to the guest's OS through the Invoke-VMScript cmdlet. It uses the same netsh command you are using.

Your script could become something like this, provided you use a CSV as input like I explained above.

$esxCred = Get-Credential -Credential "ESX host user"

$sm="X.X.X.X"

$dg="X.X.X.X"

foreach($line in Import-Csv c:\scripts\IPaddress.csv){

   $vmCred = Get-CRedential -Credential ("VM OS user for " + $line.VM)

   Get-VM -Name $line.VM | Get-VMGuestNetworkInterface -HostCredentials $esxCred -GuestCredentials $vmCred | `

      where {$_.Name -eq $line.NICname} | `

      Set-VMGuestNetworkInterface -Ip $line.IPaddr -Netmask $sm -Gateway $dg -HostCredentials $esxCred -GuestCredentials $vmCred

}

Note that this assumes that all the VM are on hosts that can use the same host credential.


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

Reply
0 Kudos
Proxmire
Contributor
Contributor
Jump to solution

Hello Luc,

Thanks so much for your help once again.  I would have never figured this out on my own.  This is the first time I've run a script using a CSV file and it's just as simple as it is easy.

The Set-VMGuestNetworkInterface cmdlet is also amazing.  I feel like it's much easier to use than performing a get-wmiobject win32_networkadapterconfiguration with an enablestatic method.  I'm very glad I was able to learn this.

I made one slight change just for convenience, which was that I added a read-host password in connection with the get-credentials so that I wouldn't need to re-enter the password after each VM was changed.  I would have to enter it many times for hosts containing a lot of VMs.  Besides that it was a perfect script for me.

Always a learning experience, thanks Luc.

Best regards

Reply
0 Kudos
rolltidega
Contributor
Contributor
Jump to solution

Question for Luc and  Proxmire:

Let's say I have over 100 VMs and I have the CSV file in question.  I only plan to power on 8 VMs at a time to change the IPs on.  Do either of you know if there are variables I can set and run using the CSV file so that it pulls the first 8 lines of the CSV, powers them on, makes the IP changes and reboots them.  I would then wait 10 minutes and then the next 8 would process, repeating until all the VMs have been processed and changed.  Let me know your thoughts...

Shawn

Shawn Cannon
Reply
0 Kudos