Automation

 View Only
Expand all | Collapse all

I Need help, How to configure IP addresses of VMs with PowerCLI

Spartacus75

Spartacus75Mar 21, 2013 08:43 AM

  • 1.  I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Feb 28, 2013 10:04 AM

    Hello everyone,

    I open this topic because i really need your help.

    I've written a script that can automatically create 10 virtual machines on my ESX Server.

    I run the script with PowerCLI.

    I want now by the aid of script, give parameters ip on the 10 machines created.

    I do not know how, can you enlighten me on the subject ?

    Is there a script that can do this ?

    Thank you very much.



  • 2.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Feb 28, 2013 10:10 AM

    Welcome to the VMware VMTN Communities!

    You can use the Set-VMGuestNetworkInterface cmdlet to set the IP configuration of a virtual machine.



  • 3.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Feb 28, 2013 12:36 PM

    Hello,

    Thank you for the welcome and for your answers fast.

    I tried to use the Set-VMGuestNetworkInterface to configure my machines, but it does not work.

    Here is my command:

    Get-VMHost MY_ESX_HOST | get-vm Machinedetest1 | 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

    I do not have a proper syntax.

    Thank you for your help.



  • 4.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Feb 28, 2013 12:52 PM

    There are a number of things that can go wrong as described in the Set-VMGuestNetworkInterface cmdlet help description:

    This cmdlet configures the network settings of a virtual machine using VMware Tools. The cmdlet allows IP and routing configuration. You can modify Wins settings only for Windows virtual machines. The cmdlet sends a remote script which executes inside the virtual machine in the context of the specified user account. This cmdlet supports only Windows XP 32 SP3, Windows Server 2003 32bit SP2, Windows Server 2003 64bit SP2, Windows 7 64 bit, Windows Server 2008 R2 64bit and Redhat Enterprise 5 operating systems.

    To run this cmdlet against vCenter Server/ESX/ESXi versions earlier than 5.0, you need to meet the following requirements:
    *You must run the cmdlet on the 32-bit version of Windows PowerShell.
    *You must have access to the ESX that hosts the virtual machine over TCP port 902.
    *For vCenter Server/ESX/ESXi versions earlier than 4.1, you need VirtualMachine.Interact.ConsoleInteract privilege. For vCenter Server/ESX/ESXi 4.1 and later, you need VirtualMachine.Interact.GuestControl privilege.

    To run this cmdlet against vCenter Server/ESXi 5.0 and later, you need VirtualMachine.GuestOperations.Execute and VirtualMachine.GuestOperations.Modify
    privileges.


    Does the command give an error message and can you show that to us?

    If you are not using an account that has rights in your vSphere environement then you have to specify credentials in your command. For example:

    $GuestCredential = Get-Credential
    Get-VMHost MY_ESX_HOST |
    Get-VM Machinedetest1 |
    Get-VMGuestNetworkInterface -GuestCredential $GuestCredential |
    Where-Object {$_.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 -GuestCredential $GuestCredential
    
    

    Message was edited by: RvdNieuwendijk Repaired a typo in the script.



  • 5.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Feb 28, 2013 01:47 PM

    Thank you for you help.

    Here is the error message when I run the following command Get-VMHost MY_ESX_HOST | get-vm Machinedetest1 | 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

    Get-VMGuestNetworkInterface : 28/02/2013 14:33:25    Get-VMGuestNetworkInterfac
    e        Timeout error while waiting for VMware Tools to start in the guest.

    At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\1.ps1:1 char
    :99
    + Get-VMHost esx.paris.free.fr | get-vm Machinedetest1 | Get-VM
    GuestNetworkInterface <<<<  | ? {$_.name -eq "Local Area Connection"} | Set-VMG
    uestNetworkInterface -IPPolicy static -Gateway 192.168.1.1 -Netmask 255.255.255
    .0 -Ip 192.168.1.5 -Verbose
        + CategoryInfo          : OperationTimeout: (:) [Get-VMGuestNetworkInterfa
       ce], VimException
        + FullyQualifiedErrorId : Client20_VmGuestServiceImpl_WaitProcessInGuest_O
       perationTimeout,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVmGuestNet
      workInterface

    For information, the OS of the virtual machines is Windows XP.

    Thanks !!!!



  • 6.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Feb 28, 2013 01:54 PM

    The error message says:

    Timeout error while waiting for VMware Tools to start in the guest

    Can you check that the VMware Tools are installed and the VMTools service is started in the virtual machine? The cmdlet uses the VMware Tools and will not work without them.



  • 7.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Feb 28, 2013 03:01 PM

    When do i start my machine, tools is activated.

    When I revival my script, I got another error message :

    Get-VMGuestNetworkInterface : 28/02/2013 15:36:14    Get-VMGuestNetworkInterfa
    e        Failed to authenticate with the operating system cli
    nt with the supplied credentials.

    At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\1.ps1:1 cha
    :99
    + Get-VMHost server.Paris.free.fr | get-vm Machinedetest1 | Get-V
    GuestNetworkInterface <<<<  | ? {$_.name -eq "Local Area Connection"} | Set-VM
    uestNetworkInterface -IPPolicy static -Gateway 192.168.1.1 -Netmask 255.255.25
    .0 -Ip 192.168.1.5 -Verbose
        + CategoryInfo          : NotSpecified: (:) [Get-VMGuestNetworkInterface],
        ViError
        + FullyQualifiedErrorId : Client20_VmGuestServiceImpl_GetGuestAuthenticati
       on_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVmGuestNetworkI
      nterface

    Error syntax ?

    My script :

    Get-VMHost server.Paris.free.fr | get-vm Machinedetest1 | 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

    The Os of my machine is Windows Xp, its a problem ?



  • 8.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Feb 28, 2013 03:08 PM

    If it is Windows XP 32 SP3 then it is no problem.

    Take a look at the script from my second post in this thread. It shows you how to pass credentials to the Get-VMGuestnetworkInterface cmdlet. That will solve your authentication problem.



  • 9.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Feb 28, 2013 03:29 PM

    Thank you RvdNieuwendijk,

    I used your script but I still have an error message:

    Set-VMGuestNetworkInterface : Missing an argument for parameter 'GuestCredentia
    l'. Specify a parameter of type 'System.Management.Automation.PSCredential' and
    try again.
    At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\2.ps1:6 char
    :131
    + Set-VMGuestNetworkInterface -IPPolicy static -Gateway 192.168.1.1 -Netmask 25
    5.255.255.0 -Ip 192.168.1.5 -Verbose -GuestCredential <<<<  -GuestCredential $G
    uestCredential
        + CategoryInfo          : InvalidArgument: (:) [Set-VMGuestNetworkInterfac
       e], ParameterBindingException
        + FullyQualifiedErrorId : MissingArgument,VMware.VimAutomation.ViCore.Cmdl
       ets.Commands.SetVmGuestNetworkInterface

    however my credentials are good.



  • 10.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Feb 28, 2013 03:35 PM

    There was a typo in the script. The -GuestCredential was specified twice. I modified the script and it should work now.



  • 11.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Feb 28, 2013 03:51 PM

    Another error message:

    Get-VMGuestNetworkInterface : 28/02/2013 16:40:18    Get-VMGuestNetworkInterfac
    e        Unable to parse script output.
    At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\2.ps1:4 char
    :28
    + Get-VMGuestNetworkInterface <<<<  -GuestCredential $GuestCredential |
        + CategoryInfo          : InvalidArgument: (:) [Get-VMGuestNetworkInterfac
       e], ViError
        + FullyQualifiedErrorId : Client20_VmHostServiceImpl_GetVmGuestNetworkInte
       rface_UnableToParse,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVmGues
      tNetworkInterface

    Unable to parse script output.

    I do not see or could come the problem.

    Thanks



  • 12.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Feb 28, 2013 04:03 PM

    The Get-VMGuestNetworkInterface and Set-VMGuestNetworkInterface cmdlets run scripts in the guest OS through the VMware Tools. These scripts can be found in the "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts" folder on the PC where you installed PowerCLI. If you are running a localized version of the OS in the guest then these scripts might fail.

    Another method to modify the networkinterface in a guest OS is through the Invoke-VMScript cmdlet. Take a look at PowerCLI: Changing a VM IP Address with Invoke-VMScript for a way to use this cmdlet.



  • 13.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Feb 28, 2013 05:15 PM

    I tried the following script:

    Function Set-WinVMIP ($VM, $HC, $GC, $IP, $SNM, $GW){
     $netsh = "c:\windows\system32\netsh.exe interface ip set address ""Local Area Connection"" static $IP $SNM $GW 1"
     Write-Host "Setting IP address for $VM..."
     Invoke-VMScript -VM $VM -HostCredential $HC -GuestCredential $GC -ScriptType bat -ScriptText $netsh
     Write-Host "Setting IP address completed."
    }
    
    Connect-VIServer 10.*.*.*


    $VM = Get-VM ( Read-Host "Enter VM name" ) $ESXHost = $VM | Get-VMHost $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", "", "") $IP = "192.168.0.81" $SNM = "255.255.255.0" $GW = "192.168.0.1" Set-WinVMIP $VM $HostCred $GuestCred $IP $SNM $GW


    But nothing happens, the first interface "vm-name" does not appear.


    Thank you very much for your help.



  • 14.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Feb 28, 2013 10:10 AM

    Welcome to the Community,

    although it might be possible to do this using PowerCLI, I think this is more something you should do on the guest OS level, i.e. running a script which interacts directly with the specific guest OS rather than with the ESXi host!?

    André



  • 15.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Mar 11, 2013 09:47 AM

    hello everyone,

    Thank you all for the different tracks that you gave me.

    I was able to assign an IP address using the following script:

    Function Set-WinVMIP ($VM, $HC, $GC, $IP, $SNM, $GW){
    $netsh = "c:\windows\system32\netsh.exe interface ip set address ""Connexion au réseau local 2"" static $IP $SNM $GW 1"
    Write-Host "Configuration de l'adresse IP de la VM $VM..."
    Invoke-VMScript -VM $VM -HostCredential $HC -GuestCredential $GC -ScriptType bat -ScriptText $netsh
    Write-Host "Setting IP address completed."
    }

    Connect-VIServer -Server 10.*.*.* -Protocol https -User admin -Password p@ssword

    $VM = Get-VM Machinedetest1
    $ESXHost = $VM | Get-VMHost

    $GuestCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for $VM", "", "")

    $IP = "192.168.0.13"
    $SNM = "255.255.255.0"
    $GW = "192.168.0.60"

    Set-WinVMIP $VM $HostCred $GuestCred $IP $SNM $GW


    How can I put a fixed password in the script?

    Thanks so much.



  • 16.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Mar 11, 2013 09:51 AM

    Have a look at the latest PowerCLI Blog post called Back to Basics: Connecting to vCenter or a vSphere Host



  • 17.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Mar 11, 2013 10:29 AM

    sorry, my question is not correct,

    I would like to know how to put a username and fixed password without entering the data manually through the interface graphic.

    the line of the script that uses the GUI is as follows (red color):

    Function Set-WinVMIP ($VM, $HC, $GC, $IP, $SNM, $GW){
    $netsh = "c:\windows\system32\netsh.exe interface ip set address ""Connexion au réseau local 2"" static $IP $SNM $GW 1"
    Write-Host "Configuration de l'adresse IP de la VM $VM..."
    Invoke-VMScript -VM $VM -HostCredential $HC -GuestCredential $GC -ScriptType bat -ScriptText $netsh
    Write-Host "Setting IP address completed."
    }

    Connect-VIServer -Server 10.*.*.* -Protocol https -User admin -Password p@ssword

    $VM = Get-VM Machinedetest1
    $ESXHost = $VM | Get-VMHost

    $GuestCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for $VM", "", "")

    $IP = "192.168.0.13"
    $SNM = "255.255.255.0"
    $GW = "192.168.0.60"

    Set-WinVMIP $VM $HostCred $GuestCred $IP $SNM $GW

    Thanks.



  • 18.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Mar 11, 2013 10:41 AM

    The blog post PowerShell - How to create a PSCredential object shows you how you can create a credential that you can put in your script.



  • 19.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Mar 11, 2013 01:07 PM

    you are the best guys.

    I have another question, do you know how I can proceed to change the hostname of the vms?

    Thanks.



  • 20.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Mar 11, 2013 01:15 PM

    If you mean that you want to move vm's to another host, then you can use the Move-VM cmdlet for that. For example:

    Get-VM -Name YourVM | Move-VM -Destination (Get-VMHost -Name ESX1.yourdomain.com)
    
    



  • 21.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Mar 11, 2013 01:37 PM

    Thank you, but, I was more referring to the virtual machine name and not the name of the vm on Vphere.:smileyhappy::smileywink:



  • 22.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Mar 11, 2013 01:44 PM

    You can use the Set-VM cmdlet to change the name of a virtual machine. For example:

    Get-VM -Name OldName | Set-VM -Name NewName
    
    



  • 23.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Mar 11, 2013 02:32 PM

    I'm sorry, may not have made myself clear

    I 'm referring to the hostname of the virtual machine, the name is visible by the "hostname" command in the command prompt.

    is there a script "Invoke-scriptvm"that allows to give a name to the machine creates.

    Thank you.



  • 24.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Mar 11, 2013 02:47 PM

    A hostname in vSphere is the name of the ESXi server where the virtual machine is running on. This caused the confusion. :smileywink:

    Because you refer to the hostname command I think that you want to rename a Windows computer. You can rename a Windows computer in PowerShell v3 using the Rename-Computer cmdlet. If you are using PowerShell v2 then you can use WMI to rename a computer. See the following blogpost for more information: Rename Computer by using WMI .



  • 25.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Mar 11, 2013 03:00 PM

    It looks as if your VM is running a Linux OS (judging from the hostname command you mentioned).

    Which Linux distribution is that ?

    The Invoke-VMScript will not work with all Linux flavours I'm afraid.



  • 26.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Mar 12, 2013 12:34 PM

    Hello,

    I use powercli 5.0.

    The operating system the virtual machine  is Windows.

    How do I proceed to indicate the vm to MODIFY on the script?

    in the script, how can I indicate the name of the VM modify has ?

    The script:


    Function Rename-ComputerName ([string]$NewComputerName){ 
    &NewComputerName= TOTO 
    $ComputerInfo = Get-WmiObject -Class Win32_ComputerSystem 
    $ComputerInfo.rename($NewComputerName) 
     
    }

    Thank you very much friends.



  • 27.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Mar 13, 2013 01:09 PM

    The following function Rename-Computer has parameters for the computer to rename, the new name and the credentials. See the comment based help inside the function for an example:

    Function Rename-Computer {
    <#
      .SYNOPSIS
        Renames a computer.
    
      .DESCRIPTION
        Renames a computer
    
      .PARAMETER  Computer
        Specify the old name of the computer.
    
      .PARAMETER  NewName
        Specify the new name of the computer
    
      .PARAMETER  Credential
         Specifies a PSCredential object containing the credentials you want to use
         for authenticating with the computer's guest OS.
        
      .EXAMPLE
        PS C:\> Rename-Computer -Computer Server1 -NewName Server2 -Credential (Get-Credential)
    
      .INPUTS
        System.String,System.Management.Automation.PSCredential
    
    #>
    
    param(
      [string]$Computer = $env:COMPUTERNAME,
      [string]$NewName,
      [System.Management.Automation.PSCredential]$Credential
      )
      
      try {
        # Check if the computer can be connected
        if (Test-Connection -ComputerName $Computer -Count 1 -Quiet)
        {
          $Parameters = @{
            Class = "Win32_ComputerSystem"
          }
          if ($Computer -ne $env:COMPUTERNAME)
          {
            $Parameters["ComputerName"] = $Computer
            $Parameters["Credential"] = $Credential
          }
          $ComputerInfo = Get-WmiObject @Parameters
          $ComputerInfo.rename($NewName)
        }
        else
        {
          Write-Warning "Unable to connect to Computer '$Computer'."
        }
      }
      catch {
        Write-Warning "Unable to retrieve information from WMI class Win32_ComputerSystem.`n$($_.Exception.Message)"
      }
    }
    
    



  • 28.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI

    Posted Mar 12, 2013 03:19 PM

    I am required to use scripts to configure the machine name and of preference by PowerCLI.


    Thank you for your invaluable help.



  • 29.  RE: I Need help, How to configure IP addresses of VMs with PowerCLI