Hi Lucd,
Thanks for your support and below is the final script which I am going to use for multiple deployments.
Is there any way that due to some problem if the command doesn't executed then It will generate a log in some path like C:\error.log
Ex: VM1 is not cloned, VM2 CPU is not configured etc.
Please suggest.
$MaxClones = 3
$CSV = "C:\users\$env:username\Linux.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 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
}
}
#Configuring CPU If values exists in CSV
$nvm | %{ if($_.CPU -ne [string]$null) {Set-VM -VM $_.Name -NumCpu $_.Cpu -Confirm:$false }}
#Configuring Memory If values exists in CSV
$nvm | %{ if($_.MGB -ne [string]$null) {Set-VM -VM $_.Name -MemoryGB $_.MGB -Confirm:$false }}
#Adding HardDisk If values exists in CSV
$nvm | %{ if($_.HDD -ne [string]$null) {New-HardDisk -VM $_.Name -CapacityGB $_.HDD -Persistence persistent -Confirm:$false }}
#Configuring VLAN If values exists in CSV
$nvm | %{ if($_.VLAN -ne [string]$null) {Get-VM $_.Name | Set-NetworkAdapter -NetworkName "$_.VLAN" -Confirm:$false}}