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.
Isn't the "0..2 | ForEach-Object {}" a for loop? IMHO it is a for loop in the PowerShell way.
Welcome to the VMware VMTN Communities!
You can use the Set-VMGuestNetworkInterface cmdlet to set the IP configuration of a virtual machine.
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é
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.
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.
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 !!!!
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.
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 ?
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.
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.
There was a typo in the script. The -GuestCredential was specified twice. I modified the script and it should work now.
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
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.
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.
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.
Have a look at the latest PowerCLI Blog post called Back to Basics: Connecting to vCenter or a vSphere Host
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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.
you are the best guys.
I have another question, do you know how I can proceed to change the hostname of the vms?
Thanks.
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)