PowerCLI

 View Only
Expand all | Collapse all

change ip address of all vms

  • 1.  change ip address of all vms

    Posted Jul 09, 2011 12:17 PM

    Hi Guys,

    I am totally novice to scripting.

    i have seen the script invoke-vmscript to change the IP address of the VMs. on the below website.

    http://www.virtu-al.net/2010/02/05/powercli-changing-a-vm-ip-address-with-invoke-vmscript/

    how can i adjust this script to read all ip addresses from a csv file or text file and apply them to multiple virtual machines.

    Thanks in advance.



  • 2.  RE: change ip address of all vms

    Posted Jul 09, 2011 12:30 PM

    You can call Alan's Set-WinVMIP function in a Foreach-Object (alias %) loop.

    Something like this

    $HostCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter ESX host credentials for $ESXHost", "root", "")
    $GuestCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for $VM", "", "")

    Import-Csv "C:\vm-ip.csv" -UseCulture | %{

      $VM = Get-VM $_.VMname
      $ESXHost = $VM | Get-VMHost $IP = "192.168.0.81"
      Set-WinVMIP $VM $HostCred $GuestCred $_.IP $_.SNM $_.GW
    }

    This assumes that your CSV looks like this

    "VMname","IP","SNM","GW"

    "vm1","192.168.1.1","255.255.255.0","192.168.1.254"

    "vm2","192.168.1.2","255.255.255.0","192.168.1.254"

    "vm3","192.168.1.3","255.255.255.0","192.168.1.254"

    And it assumes that credentials for each host and guest are the same.



  • 3.  RE: change ip address of all vms

    Posted Jul 09, 2011 01:10 PM

    Thanks for the reply,  I think this will work i will test this on this weekend.



  • 4.  RE: change ip address of all vms

    Posted Jul 09, 2011 02:57 PM

    Let me know how it works out.

    If not, include the error messages :smileywink:



  • 5.  RE: change ip address of all vms

    Posted Apr 03, 2013 07:13 PM

    Hi Expert,

    I am in a  situation to change existing IPs from one subnet (portGroup) to another subnet(portgroup) for about 100 Windows VMs, which needs change of VM portGroup as it changes the IP.

    I have already the reserved the new IPs for these VMs and I want to pass the VMName, IP, SM and GW and I also need to change the port while it changes the IPs. Can you please?

    -Shyam



  • 6.  RE: change ip address of all vms

    Posted Apr 03, 2013 08:07 PM

    This post is quite old and new features have appeared in PowerCLI in the mean time.

    With the Set-VMGuestNetworkInterface and the Set-NetworkAdapter cmdlets you should be able to do all that.



  • 7.  RE: change ip address of all vms

    Posted Apr 03, 2013 09:41 PM

    Thank You LucD. I am progressing a lot.

    Please help me with below issue pointing to? I am trying to return the value using the Get-VMGuestNetworkInterface and then use it in Set command.

    Get-VMGuestNetworkInterface : 4/3/2013 5:20:58 PM    Get-VMGuestNetworkInterface        The guest authentication being used does not have sufficient permissions to

    perform the operation.   
    At C:\Users\xxxxx\AppData\Local\Temp\8a929efc-647b-4ce9-bf5e-0b0d096ac567.ps1:1 char:28
    + Get-VMGuestNetworkInterface <<<<  -VM atladminv01 -GuestUser xxxxx -GuestPassword xxxxx
        + CategoryInfo          : NotSpecified: (:) [Get-VMGuestNetworkInterface], GuestPermissionDenied
        + FullyQualifiedErrorId : Client20_VmGuestServiceImpl_RunScriptInGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVmGuestNetworkInterface

    -Shyam



  • 8.  RE: change ip address of all vms

    Posted Apr 04, 2013 05:04 AM

    The Set-VMGuestNetworkInterface cmdlet in fact launches a script inside the guest OS.

    This script needs to run under an account that has sufficient permissions inside the guest OS.

    The message indicates that the account you specified on the GuestUser (or GuestCredential) doesn't have sufficient permissions to run the script inside the guest OS.



  • 9.  RE: change ip address of all vms

    Posted Apr 04, 2013 04:19 PM

    with -getcredentials we passed this but now at this stage.

    Set-VMGuestNetworkInterface : Cannot bind parameter 'VmGuestNetworkInterface'. Cannot convert the "Public" value of type "System.String" to type "VMware.VimAutomat


  • 10.  RE: change ip address of all vms

    Posted Apr 04, 2013 04:56 PM

    You have to do a Get-VMGuestNetworkInterface and pass the resulting object to the Set-VMGuestNetworkInterface cmdlet.

    There is no OBN for the VMGuestNetworkInterface parameter.



  • 11.  RE: change ip address of all vms

    Posted Apr 04, 2013 10:21 PM

    This is my script, I am trying.

    Connect-VIServer aaaaaa.xxx.com

    $HostCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter ESX host credentials for $ESXHost", "root", "")
    $GuestCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for $VM", "aymxds\admin_sasdefd", "")
    foreach ($_.VMname in (Import-Csv "C:\scripts\ips.csv" -UseCulture) ) {
    $VM = Get-VM $_.VMname
    $nic = Get-VMGuestNetworkInterface -VM $VM -HostCredential $HostCred -GuestCredential $guestcred
    Set-VMGuestNetworkInterface -VMGuestNetworkInterface $nic -HostCredential $HostCred  -GuestCredential $guestcred -Ip $_.ip -Netmask 255.255.252.0 -Gateway 10.177.123.254 -Confirm:$false
    $myNetworkAdapters = Get-VM $VM | Get-NetworkAdapter -Name "Network adapter 1"
    Set-NetworkAdapter -NetworkAdapter $myNetworkAdapters -Networkname "DEV/QA_800" -Confirm:$false
    }
    Disconnect-VIServer

    Below are the errors: I really need to get this working by tomorrow. We have around 100 VMs to make the change within 2 hours

    Please help.


    Set-VMGuestNetworkInterface : 4/4/2013 5:26:44 PM    Set-VMGuestNetworkInterface        "Error occured while configuring the network:'The RPC server is unavailable

    .

    Access is denied.

    The requested operation requires elevation (Run as administrator).

    The requested operation requires elevation (Run as administrator).

    '.   

    At C:\Users\sankinap\AppData\Local\Temp\bdd85c73-74af-4e6b-b030-153af1874470.ps1:9 char:28

    + Set-VMGuestNetworkInterface <<<<  -VMGuestNetworkInterface $nic -HostCredential $HostCred  -GuestCredential $guestcred -Ip $_.ip -Netmask 255.255.252.0 -Gateway

    10.177.67.254 -Confirm:$false

        + CategoryInfo          : InvalidArgument: (:) [Set-VMGuestNetworkInterface], ViError

        + FullyQualifiedErrorId : Client20_VmGuestService_SetVmGuestNetworkInterface_Error,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetVmGuestNetworkInterface

    Set-VMGuestNetworkInterface : 4/4/2013 5:27:29 PM    Set-VMGuestNetworkInterface        "Error occured while configuring the network:'The RPC server is unavailable



  • 12.  RE: change ip address of all vms

    Posted Apr 05, 2013 05:23 AM

    When you assign a fixed IP address, you should also pass the -IPPolicy Static parameter.

    The error seems to indicate that the account you pass in $guestcred doesn't have the required permissions inside the guest OS.

    And is the guest OS one of the supported OSs ? See the prereqs for using the Set-VMGuestNetworkInterface cmdlet.

    As an alternative you could look at Alan's post called PowerCLI: Changing a VM IP Address with Invoke-VMScript



  • 13.  RE: change ip address of all vms

    Posted Jul 30, 2013 07:29 PM

    Hi LucD - great post to help out on getting Virtual Al's script to work from a CSV file containing multiple machines, I have this part working fine thanks to you suggestion on this post.

    However - I need to ask if you could back up to a more simple issue with this - the 'guestcrendential' piece of the invoke script.

    I do not want to be prompted to enter in the credentials for each Guest OS because they all have the same local admin PW.  However, I can't seem to authenticate without the prompting code.. and  I see something about "secure string" in the guestpassword parameter which is  probably my issue...

    How do I get the invoke-VMScript to work from a text file using the local admin and pw for each guestOS?

    I have tried the following and nether worked:

    Invoke-VMScript -VM $VM -HostCredential $HC -GuestCredential administrator -Guestpass xxxxxxxx -ScriptType bat -ScriptText $netsh

    Invoke-VMScript -VM $VM -HostCredential $HC -GuestCredential .\administrator -Guestpass xxxxxxxx -ScriptType bat -ScriptText $netsh

    Also, I see the HostCrendential parameter in use here and on other posts, but I have found that when connecting to vCenter and provided the proper credentials when prompted to connect this is never really needed, can you confirm why I see this so much? 



  • 14.  RE: change ip address of all vms

    Posted Jul 30, 2013 07:45 PM

    I'm afraid you are mixing two things here.

    • Use the GuestUser and GuestPassword parameters
    • Use the GuestCRedential parameter

    The GuestCRedential parameter can be easily obtained like this

    $cred = Get-Credential

    Invoke-VMScript .... -GuestCredential $cred ....

    When you run the Invoke-VMScript cmdlet against vSphere earlier than  4.0, you have to use the HostUser/HostPassword or HostCredential parameter.

    Since vSphere 4.0 that is not required anymore.

    The sample code you mention is older, or is written to run against most vSphere versions.



  • 15.  RE: change ip address of all vms

    Posted Aug 01, 2013 07:12 PM

    Thanks LucD - that did it using GuestUser and GuestPassword.  Now have a working script that uses the invoke-vmscript CMDlet that draws the servers to configure from a CSV file that contains names and IPs, the gateway and mask are always the same in my case so I have removed them from Virtu-AL's script example and have replaced them with variables so that they don't need to be entered into the CSV.

    Now I have seen where others have used variables for these parameters, for example,

    $GU = "administrator"

    $GP = "'password$$$'" (note I have a set of single quotes because the pw has special charater)

    -GuestUser $GU -GuestPassword $GP

    I have replace the typed user and pw with these variables, and the instant I do this the script no longer works.

    I am trying to use the $GU and $GP in Virtu-AL's function Set-WinVMIP as well as lower in the script where the function is executed.. something is wrong..

    Function Set-WinVMIP ($VM, $IP, $SNM, $GW){

    $netsh = "c:\windows\system32\netsh.exe interface ip set address ""Local Area Connection 3"" static $IP $SNM $GW 1"

    Write-Host "Setting IP address for $VM..."

    Invoke-VMScript -VM $VM -GuestUser $GU -GuestPassword $GP -ScriptType bat -ScriptText $netsh

    Write-Host "Setting IP address completed."

    }

    # and farther down I use it here:

    Set-WinVMIP $VM -GuestUser $GU -GuestPassword $GP $_.IP $SNM $GW



  • 16.  RE: change ip address of all vms

    Posted Aug 01, 2013 07:27 PM

    You will have to define these parameters on the function, as parameters

    Function Set-WinVMIP ($VM, $IP, $SNM, $GW, $GfU, $GfP){

    ...

    Invoke-VMScript -VM $VM -GuestUser $GfU -GuestPassword $GfP -ScriptType bat -ScriptText $netsh

    }

    and then you call the function for example like this (positional parameters)

    Set-WinVMIP $VM $_.IP $SNM $GW $GU $GP

    You can also use named parameters

    Set-WinVMIP $VM $_.IP $SNM $GW -GfU $GU -GfP $GP



  • 17.  RE: change ip address of all vms

    Posted Nov 15, 2013 07:28 PM

    I need to input IP information into a NIC.  I use the following command.

    get-vmguestnetworkinterface -vm $VMNAME -guestcredential $cred | where{$_.name -eq "Local Area Connection"}  | set-vmguestnetworkinterface -ip "IP_Address" -netmask "255.255.255.0" -gateway "Gateway_Address" -dns "DNS_Address" -guestcredential $cred

    I get an error stating "Access is denied.  The requested operation requires elevation (Run as administrator)."  However, the credentials I'm providing int he $cred variable is a domain admin account.  I don't want to have to turn off UAC.  Is there a way around UAC?

    Essentially, what I'm trying to do is replace the existing e1000 vnic and a vmxnet3 nic on 180 vms.  I need to do this with as little manual work as possible.

    Thanks - PS loved you sessions at VMWorld in SF this year.



  • 18.  RE: change ip address of all vms

    Posted Nov 15, 2013 08:29 PM

    Thanks.

    These GuestNetworkInterface cmdlets are in fact BAT scripts that are located in the C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts folder.

    In the BAT file you'll see that it uses a netsh command to make the changes.

    The netsh command in more recent Windows OS requires to be run from an elevated prompt.

    Is the guest OS running in that VM reachable over the network ?

    Then you might use psexec from SysInternals to execute the netsh command inside the guest OS.



  • 19.  RE: change ip address of all vms

    Posted Nov 15, 2013 09:18 PM

    I just finished a script that handles the re-ip of the VM.  Ran into many obstacles, some of which LucD had provided assistance.. another friend of mine as well that programs for a living, unlike admins and integrators...

    First, how sure are you all adapters are the "local area connection" and not "local area connection 1" or some other #?  The netsh command will fail unless you know the exact adapter for which you wish to configure the IP on.  Got passed it by querying the ip stack's primary connection for the proper local area connection name in use, and creating a variable with the results that can be called later.  

    Invoke-VMScript -VM $VM -GuestUser administrator -GuestPassword xxxxxxxx -ScriptType bat -ScriptText ipconfig | select-string -pattern "Local Area Connection[ ]?[\d]*" | % { $_.Matches } | % {$_.Value} | new-variable LAC

    $LAC1 = $LAC -replace ".$"

    $LAC2 = '"' + $LAC1 + '"'

    Then I used the variable here:

    $netsh = "c:\windows\system32\netsh.exe interface ip set address $LAC2 static $IP $SNM $GW 1"

    put into full action:

    Function Set-WinVMIP ($VM, $IP, $SNM, $GW, $dns1, $dns2, $LAC2){

    Invoke-VMScript -VM $VM -GuestUser administrator -GuestPassword xxxxxxx -ScriptType bat -ScriptText ipconfig | select-string -pattern "Local Area Connection[ ]?[\d]*" | % { $_.Matches } | % {$_.Value} | new-variable LAC
    $LAC1 = $LAC -replace ".$"
    $LAC2 = '"' + $LAC1 + '"'

    $netsh = "c:\windows\system32\netsh.exe interface ip set address $LAC2 static $IP $SNM $GW 1"
    Write-Host "Setting IP address for $VM..."
    Invoke-VMScript -VM $VM -GuestUser administrator -GuestPassword xxxxxxxx -ScriptType bat -ScriptText $netsh
    Write-Host "Setting IP address completed."

    $netsh1 = "c:\windows\system32\netsh.exe interface ip set dns name=$LAC2 static $dns1"
    Write-Host "Setting Primary DNS for $VM..."
    Invoke-VMScript -VM $VM -GuestUser administrator -GuestPassword xxxxxxxx -ScriptType bat -ScriptText $netsh1
    Write-Host "Setting Primary DNS completed."

    $netsh2 = "c:\windows\system32\netsh.exe interface ip add dns name=$LAC2 $dns2"
    Write-Host "Setting Secondary DNS for $VM..."
    Invoke-VMScript -VM $VM -GuestUser administrator -GuestPassword xxxxxxxx -ScriptType bat -ScriptText $netsh2
    Write-Host "Setting secondary DNS completed."

    Connect-VIServer $vcenter

    foreach ($_.name in (Import-Csv "$ENV:USERPROFILE\Desktop\vscripts\vm-ip.csv" -UseCulture) ) {

    $VM = Get-VM $_.name

    Set-WinVMIP $VM -GuestUser administrator -GuestPassword xxxxxxx $_.IP $SNM $GW $dns1 $dns2

    }


    Second, make sure DHCP servers are NOT handing out DHCP addresses on the network, because if your new vmxnet3 adapter picks up an address - and not the auto generated address resulting from NO DHCP addresses available - netsh will fail with the error "unable to configure  DHCP services"     I had to scratch my head on this a thousand times, but it is the case.  Netsh will overlay the auto assigned IP that is auto configured when the VM powers up after the new adapter is in.

    I did not have any problems with user account control with w2008R2 builds, just vista, I believe the policies for UAC may have been shut off not sure..

    Then to verify - the most important part of this because you will find a handful of failures I guarantee PERIOD - this runs against the cluster you define as a variable and performs an IP config against all the VMs and pipes the output to a file on your desktop

    $vcenter = "10.x.x.x"
    $Cluster = "test"

    ##########################################################   
    ##### connect to vCenter, you will be prompted for credentials
    ##########################################################  

    connect-viserver $vcenter

    Get-VM -location $Cluster | Invoke-VMScript -ScriptType bat -ScriptText "netsh interface IP show config" -GuestUser administrator -GuestPassword xxxxx | export-Csv  $ENV:USERPROFILE\Desktop\vscripts\verify_VM_IPconfig.csv -NoTypeInformation
    Disconnect-VIServer

    #####  End of Script, That's all Folks!

    I'm sharing this because I couldn't have done it without the help of others so it is  only right..

    There are some great ping verification scripts out there as well that you can easily modify and verify connectivity as well which I recommend



  • 20.  RE: change ip address of all vms

    Posted Nov 15, 2013 09:21 PM

    Forgot to mention, the ip script requires variables that I neglected to copy in which belong at the start

    ####  Variables:

    $vcenter = "x.x.x.x.x."
    $SNM = "255.255.255.0"
    $GW = "10.x.x.x.x"
    $dns1 = "10.x.x.x.x"
    $dns2 = "10.x.x.x.x"




  • 21.  RE: change ip address of all vms

    Posted Nov 15, 2013 10:58 PM

    kgottleib

    Great info.  Thanks.

    My VCenter is actually not on the same network as the VMs and can't communicate with them across the network.  Apparently, I'm gonna have to disable UAC on the VMs via a domain GPO prior to changing the NICs.  After disabling UAC and rebootign the VM the below command worked fine.  After a bit of testing a treaking I came up with this.

    get-vmguestnetworkinterface -vm $vmname -guestcredential $cred | where{$_.ip -ne $null}  | set-vmguestnetworkinterface -ip "IP_Address" -netmask "255.255.255.0" -gateway "GW_Address" -dns "Primary_DNS","Secondary_DNS" -guestcredential $cred

    The where{$_.ip -ne $null} command basically looks for a NIC with an IP assigned or an IP field that isn't empty.  This would included either a currently configured static IP or an autoconfigured "dhcp" IP.

    Now I just need to put it all together.

    Shutdown VM = get-vm $vmname | shutdown-vmguest -confirm:$false

    Reconfigure NIC = get-vm $vmname | get-networkadapter | set-networkadapter -type "vmxnet3"

    PowerOn VM = get-vm $vmname | start-vm

    Reconfigure IP info = get-vmguestnetworkinterface -vm $vmname -guestcredential $cred | where{$_.ip -ne $null}  | set-vmguestnetworkinterface -ip "IP_Address" -netmask "255.255.255.0" -gateway "GW_Address" -dns "Primary_DNS","Secondary_DNS" -guestcredential $cred



  • 22.  RE: change ip address of all vms

    Posted Nov 16, 2013 02:57 PM

    It doesn't matter that vcenter  is not on the same network.  The invoke vmscript CMDlet that I use talk through VMware tools of the VM even when it's off the wire.  That is it's specific function, To inject scripts inside the guest OS Through the VMware API. 

    After you swap out the adapter for the VMXnet3 VM willpower up without any IP address and the VM will not be on the network.