Hi All,
$MaxClones = 3
$CSV = "C:\users\$env:username\desktop\file1.csv"
$nvm = Import-CSV $CSV -Useculture
$idTab = @()
$nvm |
%{
$idTab += New-VM -Name $_.Name`
-vm $(Get-Cluster $_.cluster | Get-ResourcePool $_.SVMLocation | get-vm $_.SourceVM)`
-VMHost $(Get-Cluster $_.cluster | Get-VMHost -state "connected" | Get-Random)`
-ResourcePool $(Get-Cluster $_.cluster | Get-ResourcePool $_.rp)`
-datastore $(Get-Cluster $_.cluster | Get-DataStore $_.ds)`
-Notes $_.Notes -RunAsync | Select -ExpandProperty Id
$CurrentClones = Get-Task -Status Running |
where {$idTab -contains $_.Id} |
Measure-Object | Select -ExpandProperty Count
Write-Output "$($_.Name) is deploying"
Write-Output "In loop: Current: $($CurrentClones) Max: $($MaxClones)"
while($CurrentClones -ge $MaxClones)
{
sleep 1200
Write-Output "$($_.Name) is deploying"
Write-Output "In loop: Current: $($CurrentClones) Max: $($MaxClones)"
if($currentclones -eq $maxclones){
$CurrentClones = Get-Task -Status Running |
where {$idTab -contains $_.Id} |
Measure-Object | Select -ExpandProperty Count
}
} }
I got the above code from Lucd, As this post is regarding continuation of this script.
Now I have a different requirement.
I am provisioning new VM by executing the command "c:\provision\new-vm.ps1 -hostname vm1 -template customername"
As this script does lot of customization at OS level as per Customer requirement specified in the template parameter.
we have dependency that 1st 2 VMs OS customization has to be successful, Post only we have to create additional 10-20 VMs for same customer by passing the VM Name & Template.
Is there any possibility of automating the additional 10-20 servers by altering the above code.
As passing additional VM Name & template is taking more time as we have to look into current clones I the same vCenter and then initiate it.
I have tried like below, but I am not getting output as the above code.
$MaxClones = 3
$CSV = "C:\users\$env:username\desktop\file1.csv"
$nvm = Import-CSV $CSV -Useculture
$idTab = @()
$nvm |
%{
$idTab += c:\provision\Provision.ps1 -Hostname $($_.name) -AutomationTemplate $($_.template) -| Select -ExpandProperty Id
$CurrentClones = Get-Task -Status Running |
where {$idTab -contains $_.Id} |
Measure-Object | Select -ExpandProperty Count
Write-Output "$($_.Name) is deploying"
Write-Output "In loop: Current: $($CurrentClones) Max: $($MaxClones)"
while($CurrentClones -ge $MaxClones)
{
sleep 30
Write-Output "$($_.Name) is deploying"
Write-Output "In loop: Current: $($CurrentClones) Max: $($MaxClones)"
if($currentclones -eq $maxclones){
$CurrentClones = Get-Task -Status Running |
where {$idTab -contains $_.Id} |
Measure-Object | Select -ExpandProperty Count
}
} }
In CSV file I have 2 columns 1 with VM name & another with Template name.
Please do let me know if any additional details are required.
Note: I can't share Provision.ps1 as it is having lot of entries related to organization
Is provision.ps1 returning an exit code, indicating success or failure?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd,
Sorry for the delayed response.
I am getting an exit code stating "Select: Property 'ID' Cannot be found".
It Is provisioning only 1 VM.
and not starting new VM provisioning and exiting stating you are not connected any servers.
It's difficult analysing the issue without seeing all the code I'm afraid.
It looks as if the object returned by the provisioning script doesn't contain a property named Id.
But if you are also getting error saying "Not connected" it could be that none of the PowerCLI cmdlets work correctly, due to the missing Connect-VIServer cmdlet.
Are you sure you are connected, or doing a Connect-VIServer in one of the scripts?
It would perhaps also help if you included the error messages as you see them on he console.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference