here is the code:
input - # of vm's to create, controls for loop
first 3 octets of ip to assign, then incremented
vm names start at vmtest0001, and go for how many you chose to create vmtest0001, 0002, etc.
also, set up a dhcp server to get around the static ip issue ? I guess I am missing something
it is esx5.0.1, and powercli 5.5
it sets the corrent name in the vsphere gui, but does not change the guest os name (like in etc/sysconfig/ifcfg-eth0, hosts file, wherever
for ($i=1; $i -le $NumVms; $i++) {
#Write-Host "IP = " $IpAddress "gateway" $DgateWay "vmname" $VmName
#$CustSpec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IpAddress -SubnetMask $NetMask -DefaultGateway $DgateWay
$CustSpec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseDhcp
$VmHost = Get-Cluster Sandbox | Get-VMHost | Get-Random | Where{$_ -ne $null}
$VmName = $VmNamePrefix + ('0' * (4 - $intInc.ToSTring().Length)) + ($intInc++).ToString()
$IpAddress = $IpPrefix + ($IpInc++).ToString()
Write-Host "IP = " $IpAddress "gateway" $DgateWay "vmname" $VmName
New-VM -Name $VmName -Template $StrTemplate -VMHost $VmHost -OSCustomizationSpec $CustSpec -Datastore $DataStore -Location hesm
#Set-VM -GuestId $VmName
#Start-VM -VM (Get-VM -Name $VmName) -Confirm #-RunAsync
#Start-VM $VmName
write-host "Build started for" $VmName $IpAddress $NetMask $DgateWay
thanks.
Does the actual ID of the NIC on the VM correspond with the ID you use in the OSCustomization specification ?
You can check by looking at the content of
$CustSpec | Get-OSCustomizationNicMapping
and for the VM you can check in the VMX file.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The first thing is thanks for checking this out.
I have not used an id in OSCustomization, I guess by id you mean the MAC addr?
I am not sure when the MAC gets assigned, or how to retrieve it once the new-vm cmd starts to run.
The way it is now, I can create all the vm's I want, but can't seem to set an ip, or a name in /etc/sysconfig/network or in the *eth0 file in network-scripts?
I am kind of new to this powercli stuff. We are getting a tintri storage device that my boss wants to stress out. My idea is to create 200-500 vm's
to try to generate thousands of IOPS so we can see the tintri will do what they say it will do bedore we buy it, that said.....
I want to be able to create 100-500 cent 6.3 vms, each with unique hostnames and ip address. We have a sandbox are all set up for this, so
they will just be 10.10.xx.xx ip's with mask of 255.255.0.0 so any can talk to any other, nothing fancy for now. In my script to create them I
tried putting the guesid(hostname) within the for loop which would go from vmtest0000 to vmtest9999. I could not seem to set the ip. Keep in mind
that our version of esx does not let me manually create a customizationSpec manually though vsphere, not sure why. Anyway this I figured
I could do on the fly in the script, seems to work for the most part. To get around the ip address thing, I built a windows dhcp server with the sandbox(10.10.1.1)
I will try to pull an ip from that. I guess that will happen when the vm is powered on ??
Still don't understand why I cant set the guest os name(hostname), do I need to maybe run the invoke-VMscript cmd after the fact to get this to work as in this link:
http://ravatheodor.blogspot.com/2013/02/guest-customization-vcloud-director-and.html
I would rather do it all from powercli if possible. Here is the whole code the way I had it minus the commented lines:
Connect-VIServer whqvc01t
$StrTemplate = "vmtest-template"
$NumCpus=4
$VmNamePrefix="vmtest"
$DataStore="vnx_whq_std_001"
$IntInc=1
$IpInc=1
$IpAddress=""
$NetMask="255.255.0.0"
$DgateWay="10.10.1.2"
$RealIp="0.0.0.0"
$VmName=""
#Set-ExecutionPolicy RemoteSigned
$NumVms = [int]$strResponse = Read-Host "How many pest vms would you like to create? "
$IpPrefix = Read-Host "Please enter the first 3 octets of the ip address range these vm's will use eg. (192.168.1.) --> "
$IpAddress = $IpPrefix + $IpInc.ToString()
$CustSpec = New-OSCustomizationSpec -Type NonPersistent -OSType Linux -Domain "test"
#$CustSpec = New-OSCustomizationSpec -Type NonPersistent -Domain Domain -DomainUsername Root -DomainPassword pass -FullName krodger -OrgName test
$CustSpec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IpAddress -SubnetMask $NetMask -DefaultGateway $DgateWay
$VmHost = Get-Cluster Sandbox | Get-VMHost | Get-Random | Where{$_ -ne $null}
$VmName = $VmNamePrefix + ('0' * (4 - $intInc.ToSTring().Length)) + ($intInc++).ToString()
for ($i=1; $i -le $NumVms; $i++) {
$CustSpec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IpAddress -SubnetMask $NetMask -DefaultGateway $DgateWay
$VmHost = Get-Cluster Sandbox | Get-VMHost | Get-Random | Where{$_ -ne $null}
$VmName = $VmNamePrefix + ('0' * (4 - $intInc.ToSTring().Length)) + ($intInc++).ToString()
$IpAddress = $IpPrefix + ($IpInc++).ToString()
Write-Host "IP = " $IpAddress "gateway" $DgateWay "vmname" $VmName
New-VM -Name $VmName -Template $StrTemplate -VMHost $VmHost -OSCustomizationSpec $CustSpec -Datastore $DataStore -Location hesm
#Set-VM -GuestId $VmName
#Start-VM -VM (Get-VM -Name $VmName) -Confirm #-RunAsync
write-host "Build started for" $VmName $IpAddress $NetMask $DgateWay
}
Disconnect-VIServer -Server whqvc01t -Confirm:$false
Write-Host "disconnected"
Thanks Again, keith
No, I don't mean the MAC, but the ID of the NIC.
When you clone a CentOS VM, you might end up with 2 NICs in the new VM.
See How to use OS customization for CentOS 6 in vCenter 5
In the OSCustomizationSpec NIC mapping there is an ID for each NIC for which you specify settings.
You can see that
Get-OSCustomizationSpec -name MyCust |
Get-OSCustomizationNicMapping |
Select Position, IPAddress
In the VMX file of the new VM you might have 2 NIC entries.
The number of the NIC + 1 in the VMX has to correspond with the Position number in the OSCustomizationSpec
This NIC in the VMX will correspond with Position 1 in the OSCustomizationSpec.
If the clone ends up with 2 NICs, you will have to change the Position in the OSCustomizationSpec (or just add a 2nd entry)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This fuction(customziation) is not supported by clientos: CentOS *.*.
Even when you use manual operation in the vm-client to change centos template customization, you still can not change guest-ip and guest-hostname.
Maybe you can change your template's OS-TYPE to linux to cheat vcenter to solve this problem(I have heard some case in test enviroment). But it is not a safe way in production enviroment, or maybe we can change the type of the vm(not template) back to centos after batch provision.
BTW, I can learn a lot from your coding! I love your coding style. Thanks a lot!
Hi
I am also facing problems applying hostname to windows VMs I am creating; I don't have full rights so I am not able to create a Customization Specification on the vCenter server; hence using the local copy; This local copy created via New-OSCustomizationSpec does not update with the new VM name for every iteration in for loop; The code is working with for loop.
Currently
- the local copy does not destroy by setting the variable to $null
- Remove-OSCustomizationSpec is not able to find it
- It is not taking the New-OSCustomizationSpec -Name $_.vmname is not working; it creates an almost blank object
And finally the editor to post this comment is not letting me paste anything. Seem I have turned into a malfunctioning software black hole.
I did eventually get everything to work correctly, and invoke a script that gets run on the guest, then I init 6 the guest and all is great.
New-VM -Name $VmName -Template $StrTemplate -Datastore $DataStore -ResourcePool hesm -Location pest
Start-VM $VmName | Wait-Tools
start-sleep -Seconds 2
do {
Start-Sleep -milliseconds 200
$stat=(Get-VM $VmName | Get-View).Guest.ToolsStatus
Write-Host $VmName $stat
} until($stat -eq "toolsOk")
$command = "/root/config.sh $VmName $IpAddress"
Invoke-VMScript -VM $VmName -ScriptText $command -ScriptType Bash -GuestUser "root" -GuestPassword "password"
Restart-VMGuest -VM $VmName
Thanks a for your response.
So the bottom line is OSCustomizationSpec did not help you and you used work around via invoking some shell/batch script already saved in VM template.
And that's the suggested way out?