VMware Cloud Community
Spartacus75
Contributor
Contributor
Jump to solution

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

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.

Reply
0 Kudos
35 Replies
Spartacus75
Contributor
Contributor
Jump to solution

Thank you, but, I was more referring to the virtual machine name and not the name of the vm on Vphere.Smiley HappySmiley Wink

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Spartacus75
Contributor
Contributor
Jump to solution

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.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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

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 .

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


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

Reply
0 Kudos
Spartacus75
Contributor
Contributor
Jump to solution

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.

Reply
0 Kudos
Spartacus75
Contributor
Contributor
Jump to solution

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


Thank you for your invaluable help.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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)"
  }
}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Spartacus75
Contributor
Contributor
Jump to solution

Hello,

Thank you for your availability.

I finally used another method to change the name.

I'll post the method on the forum use for in case can help someone.

I have another question.
I have a script to automatically create virtual machines.
I would like that each machine has for name the field "name" followed by field "number". Example: "Machine1, Machine2, Machine 3."
I absolutely have to use the loop "For" in my script.
I would therefore have a dynamic variable.

Here is my script:

$ Name = Machine

for ($ i = 0; $ i-ne 2; $ i + +)
{
New-vm-vmhost server1.paris.rio-Name ($ Name $ i)-Template-Datastore TestScript VMFS_LOCAL_server1
}

I have a problem with my script, the name created possesses that  the field "number" Example: 1, 2, 3.

Someone would have an idea ?

thanks !!!

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

In PowerShell you can use the 0..2 to get all the numbers 0, 1 and 2. Using this you can do:

$Name = "Machine"
0..2 |
ForEach-Object {
  New-vm -vmhost server1.paris.rio -Name "$Name$_" -Template Template1 -Datastore VMFS_LOCAL_server1
}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Spartacus75
Contributor
Contributor
Jump to solution

helloSmiley Happy,

thank you very much "RvdNieuwendijk" but as part of my project, I am required to use a "for" loop to create my VMs.

can we proceed in the same way but with a "for" loop ?:smileygrin:

Thank you ! Smiley Wink

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Isn't the "0..2 | ForEach-Object {}" a for loop? IMHO it is a for loop in the PowerShell way. Smiley Wink

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Spartacus75
Contributor
Contributor
Jump to solution

Oops, you're right. Sorry, I'm still a beginner:smileyconfused:.
Thank you for the correction.

Thank you very much for your timeSmiley Wink.

Reply
0 Kudos
Spartacus75
Contributor
Contributor
Jump to solution

Really great the VMWare community .

Reply
0 Kudos
Spartacus75
Contributor
Contributor
Jump to solution

Hello everybody,

I have a big problem.

I have a script that allows me to create machines, start the machine, give a name and IP parameters.

Overall, the script works.

I now wish to configure the IP address using a foreach loop and increment by 1 the last decimal of my ip address so that all machines can have an IP configuration.

example:

10170229.% 1 <- 24
10.170.229.25 to the machine 2.
10.170.229.26 for machine 3 etc ...

I designed my script so that it automatically increments the last decimal of my Ip (10,170,219.%1).

My problem is that my script can do everything except incrementing the IP decimal. Yet I put in my loop the argument "$ Ip + +" to increment.

Here is my scrip:

param($Count,$Name,$Type,$Ip)

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

2..$Count | Foreach {
  New-vm -vmhost 007.london.england.eu -Name "$Name$_" -Template $Type -Datastore Data_base007  }
 
read-host "Press Enter"

2..$Count | Foreach {
  Start-VM -VM "$Name$_"}
 
read-host "Press Enter"


2..$Count | Foreach {
$VM = Get-VM "$Name$_"
write-host "$Name$_"
$ESXHost = $VM | Get-VMHost
$secpasswd = ConvertTo-SecureString "formation" -AsPlainText -Force
$GuestCred = New-Object System.Management.Automation.PSCredential ("formation", $secpasswd)
Function Set-WinVMIP ($VM, $HC, $GC){
$Ip++
$ps1= "c:\ip.bat $Ip $VM"
Invoke-VMScript -VM $VM -HostCredential $HC -GuestCredential $GC -ScriptType bat -ScriptText $ps1


}
Set-WinVMIP $VM $HostCred $GuestCred
}

would you have a solution to this problem?

Thank you.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I answered that last question in Re: How to pass parameters in a variable in a loop FOREACH ?


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

Reply
0 Kudos