VMware Cloud Community
malichite
Contributor
Contributor
Jump to solution

Progress bar in VM Deployment Script

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
malichite
Contributor
Contributor
Jump to solution

Works like a champ!  Thanks!

0 Kudos