PowerCLI

 View Only
Expand all | Collapse all

PowerCLI can't change IP address of VM

  • 1.  PowerCLI can't change IP address of VM

    Posted Jan 20, 2010 03:44 PM

    I'm relatively new to Powershell and PowerCLI (as in 1 week ago relatively new) and I'm trying to stumble through the process of provisioning about 12 VM's on a recurring basis for a class. I can deploy VM's from a template just fine, however I want to assign specific IP addresses to VM's once they're done sysprep'ing. I am using Powershell 1.0 and PowerCLI 4.0.1 in a windows 2003 VM (also vcenter on that box). The VM's that I'm deploying are also Windows 2003 and sysprep just fine. Here is the section in my script that I'm using to set the IP address:

    $net1 = "LAN"

    set-vmguestnetworkinterface -VmGuestNetworkInterface $net1 -vm P2V_student06 -hostuser root -hostpass ****** -guestuser administrator -guestpass ****** -ippolicy static -gateway 192.168.20.1 -netmask 255.255.255.0 -ip 192.168.20.126

    Here is the output of get-vmguestnetworkinterface for that VM:

    VMId : VirtualMachine-vm-578

    SubnetMask : 255.255.255.0

    NicId : VirtualMachine-vm-578/4000

    Name : LAN

    IPPolicy : Dhcp

    Ip : 192.168.20.188

    Dns : {192.168.20.2}

    DefaultGateway : 192.168.20.1

    Description : Intel(R) PRO/1000 MT Network Connection

    Mac : 00-50-56-B7-36-45

    RouteInterfaceId :

    DnsPolicy : Dhcp

    WinsPolicy : Dhcp

    Wins :

    When I run the script- it gives the following output:

    Name Port User

    -


    -


    -


    192.168.20.35 443 Administrator

    Set-VMGuestNetworkInterface : Cannot bind parameter 'VmGuestNetworkInterface'. Cannot convert value "LAN" to type "VMware.VimAutomation.Types.VMGuestNetworkInterface". Error: "Invalid cast from 'System.String' to 'VMware.VimAutomation.Types.VMGuestNetworkInterface'."

    At C:\powercli_scripts\test_setup_vsphere_4.0_class.ps1:6 char:53

    + set-vmguestnetworkinterface -VmGuestNetworkInterface <<<< LAN -vm P2V_student06 -hostuser root -hostpass ****** -guestuser administrator -guestpass ****** -ippolicy static -gateway 192.168.20.1 -netmask 255.255.255.0 -ip 192.168.20.126

    Originally the VM had the standard "Local Area Connection" for the network adapter name. I thought it might be having problems with spaces so I renamed it to "LAN" and it still errors out. Can anyone think of what I'm doing wrong? This almost seems like a bug of some kind.

    Thanks in advance for any help you can offer. I'm pulling my hair out being so close to getting this to work it's driving me NUTS!

    Best Regards,

    Eric Steed



  • 2.  RE: PowerCLI can't change IP address of VM

    Posted Jan 20, 2010 03:54 PM

    $net1 needs to be an object "VMware.VimAutomation.Types.VMGuestNetworkInterface"

    So you need to reference the object by using get-vmguestnetworkinterface.

    Something like this:

    $net1 = get-vm vmname | get-vmguestnetworkinterface

    K. Chris Nakagaki (Zsoldier)



  • 3.  RE: PowerCLI can't change IP address of VM

    Posted Jan 20, 2010 03:54 PM

    I believe you need WinRM installed on the VM you want to configure as well as powershell. Here are the directions to install WinRM

    http://msdn.microsoft.com/en-us/library/aa384372(VS.85).aspx



  • 4.  RE: PowerCLI can't change IP address of VM

    Posted Jan 20, 2010 04:52 PM

    That's correct. To execute a posh script inside a VM using the PowerCLI, you must have PowerShell installed inside the guest.

    Jase McCarty

    http://www.jasemccarty.com

    Co-Author: VMware ESX Essentials in the Virtual Data Center (ISBN:1420070274) Auerbach

    Co-Author: VMware vSphere 4 Administration Instant Reference (ISBN:0470520728) Sybex

    Please consider awarding points if this post was helpful or correct



  • 5.  RE: PowerCLI can't change IP address of VM

    Posted Jan 20, 2010 05:55 PM

    Ok. So I installed powershell inside the guest VM and it still doesn't work. To be clear- I'm executing the script from the vCenter server and reaching into the guest VM to make the changes. Do I also need to install PowerCLI inside the guest VM?

    One other question- I think zsoldier may have been onto something. If I want to try this I use the following syntax:

    $net1 = get-vm t_w2k3_ent_32bit_sp2_VC | get-vmguestnetworkinterface -hostuser root -hostpass ****** -guestuser administrator -guestpass ****** | ? {$_.Name -eq "Local Area Connection"}

    set-vmguestnetworkinterface -VmGuestNetworkInterface $net1 -vm t_w2k3_ent_32bit_sp2_VC -hostuser root -hostpass ****** -guestuser administrator -guestpass ****** -ippolicy static -gateway 192.168.20.1 -netmask 255.255.255.0 -ip 192.168.20.126 -Verbose

    but I still get an error. This time it's:

    C:\powercli_scripts> C:\powercli_scripts\test_setup_vsphere_4.0_class.ps1

    Name Port User

          • ---- ----

    192.168.20.35 443 Administrator

    Set-VMGuestNetworkInterface : Cannot bind parameter because parameter 'VmGuestNetworkInterface' is specified more than once. To pro

    vide multiple values to parameters that can accept multiple values, use the array syntax. For example, "-parameter value1,value2,va

    lue3".

    At C:\powercli_scripts\test_setup_vsphere_4.0_class.ps1:5 char:63

    + set-vmguestnetworkinterface -VmGuestNetworkInterface $net1 -vm <<<< t_w2k3_ent_32bit_sp2_VC -hostuser root -hostpass ****** -gu

    estuser administrator -guestpass ****** -ippolicy static -gateway 192.168.20.1 -netmask 255.255.255.0 -ip 192.168.20.126 -Verbo

    se

    I think I'm on the verge of getting this correct but I'm missing something related to setting the vmguestnetworkinterface parameter correctly. I wish the documentation was a bit better and included some real world examples that made sense :smileysad:



  • 6.  RE: PowerCLI can't change IP address of VM

    Broadcom Employee
    Posted Jan 20, 2010 06:58 PM

    Actually you don't need PowerCLI to be installed within the guest to perform this type of an operation afaik, though the PS experts can correct me if I'm wrong

    1) If you're cloning/deploying a VM, you can customize the guest using the APIs and guest customization which is feature of vCenter + sysprep for Windows and linux prep for Linux/UNIX systems

    2) You can utilize VMware VIX so long as the guest OS has VMware Tools running (http://professionalvmware.com/2008/12/vixated-with-vmwares-vix/)

    3) I believe PowerCLI has some integration with VIX already to execute a script within the guest, so you can just setup a batch script or whatever that'll execute within the guest via the PowerCLI cmdlet that'll configure the networking for you

    =========================================================================

    William Lam

    VMware vExpert 2009

    VMware ESX/ESXi scripts and resources at:

    Twitter: @lamw

    VMware Code Central - Scripts/Sample code for Developers and Administrators

    VMware Developer Comuunity

    If you find this information useful, please award points for "correct" or "helpful".



  • 7.  RE: PowerCLI can't change IP address of VM

    Posted Jan 20, 2010 07:30 PM

    When you run just $net1 in powershell, do you get more than one object returned? If so, you might want to try changing this portion:

    set-vmguestnetworkinterface -vmguestinterface $net1[0]

    $net[0] should return only the first object in the array. $net[1], the next value, etc.

    K. Chris Nakagaki (Zsoldier)



  • 8.  RE: PowerCLI can't change IP address of VM
    Best Answer

    Posted Jan 20, 2010 11:50 PM

    Instead of populating the variable followed by setting the nic by specifying the variable can you try just getting the interface and piping to the set command? I repro'd your problem but got it to work by doing this:

    Get-VMHost MY_ESX_HOST | get-vm MY_VM | Get-VMGuestNetworkInterface | ? {$_.name -eq "Local Area Connection"} | Set-VMGuestNetworkInterface -IPPolicy static -Gateway 192.168.1.1 -Netmask 255.255.255.0 -Ip 192.168.1.5 -Verbose

    To answer the "is powershell needed in the guest" the answer is no. The powercli command you are using creates a batch file, uses vix, or the equivelant of copy-vmguestfile or invoke-vmscript to call the batch file from the guest...it's a CMD file on the guest.

    Now, there used to be a problem where the vmguestnetworkinterface commands were assuming the interface name was "Local Area Connection*" so naming it anything other than that would break the script. Not sure if that's been fixed yet, but I can check a little later.

    Hope this helps,

    alex.



  • 9.  RE: PowerCLI can't change IP address of VM

    Posted Jan 21, 2010 03:20 AM

    Which versions of windows does this work on. And the CMD on the guest

    is there by default? Thanks for clearing this up. I will have to give

    it a try tomorrow.

    On Jan 20, 2010, at 6:49 PM, afontana <communities-emailer@vmware.com



  • 10.  RE: PowerCLI can't change IP address of VM

    Posted Jan 21, 2010 04:19 AM

    Well...it should work on all supported Windows guest OS's. You can have a look at the bat script that is used by looking in %programfiles%\vmware\infrastructure\vsphere powercli\scripts. Basically the get script uses ipconfig to obtain the info and echo back to std out, the set script uses netsh to make the change.

    -alex



  • 11.  RE: PowerCLI can't change IP address of VM

    Posted Jan 23, 2010 02:32 PM

    thank you so much for that solution! i knew it could be done i just couldn't figure out the proper syntax. still not too sure why it has to be so convoluted but it works :smileyhappy: