I am trying to make my deployment script more efficient. I currently use the wait-task commandlet to show progress of the cloneing task. I would rather be able to run through the loop, get all the machines cloning at once and they put up a progress bar of all tasks using the write-progress commandlet maybe using the output from get-task. Any help would be appreciated.
$vms = Import-CSV D:\PSScripts\vmware\deploy.csv
foreach ($vm in $vms){
$Template = Get-Template $vm.template
$VMHost = Get-VMHost $vm.host
$Datastore = Get-Datastore $vm.datastore
$OSCustomization = Get-OSCustomizationSpec $vm.customization
$Task = New-VM -Name $vm.name -OSCustomizationSpec $OSCustomization -Template $Template -VMHost $VMHost -Datastore $Datastore -RunAsync -DiskStorageFormat Thin
Wait-Task -Task $Task
}
foreach ($vm in $vms)
{
Set-NetworkAdapter -NetworkName $vm.networkName -StartConnected:$true
Start-VM $vm.name
}
output of get-task
[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> get-
task
Name State % Complete Start Time Finish Time
---- ----- ---------- ---------- -----------
CloneVM_Task Success 100 10:01:42 AM 10:10:25 AM
CloneVM_Task Running 19 10:10:29 AM
You could collect all tasks in an array and then run the Wait-Task cmdlet against that array.
Something like this
$vms = Import-CSV D:\PSScripts\vmware\deploy.csv
$Tasks = @()
foreach ($vm in $vms){
$Template = Get-Template $vm.template
$VMHost = Get-VMHost $vm.host
$Datastore = Get-Datastore $vm.datastore
$OSCustomization = Get-OSCustomizationSpec $vm.customization
$Tasks += (New-VM -Name $vm.name -OSCustomizationSpec $OSCustomization -Template $Template -VMHost $VMHost -Datastore $Datastore -RunAsync -DiskStorageFormat Thin)
}
Wait-Task -Task $Tasks
foreach ($vm in $vms)
{
Set-NetworkAdapter -NetworkName $vm.networkName -StartConnected:$true Start-VM $vm.name
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could collect all tasks in an array and then run the Wait-Task cmdlet against that array.
Something like this
$vms = Import-CSV D:\PSScripts\vmware\deploy.csv
$Tasks = @()
foreach ($vm in $vms){
$Template = Get-Template $vm.template
$VMHost = Get-VMHost $vm.host
$Datastore = Get-Datastore $vm.datastore
$OSCustomization = Get-OSCustomizationSpec $vm.customization
$Tasks += (New-VM -Name $vm.name -OSCustomizationSpec $OSCustomization -Template $Template -VMHost $VMHost -Datastore $Datastore -RunAsync -DiskStorageFormat Thin)
}
Wait-Task -Task $Tasks
foreach ($vm in $vms)
{
Set-NetworkAdapter -NetworkName $vm.networkName -StartConnected:$true Start-VM $vm.name
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Works like a champ! Thanks!