ProgressbarOverlay problems

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
Richibm
Posts: 1
Joined: Thu Jan 25, 2018 8:38 am

ProgressbarOverlay problems

Post by Richibm » Thu Feb 22, 2018 8:35 am

Product, version and build: Powershell Studio 2018 5.5.148.0
32 or 64 bit version of product: 64 bitt
Operating system: Windows Server 2016
32 or 64 bit OS: 64 bit

Good Afternoon.
Hopefully someone more knowledgable than me can help.
I am trying to write a gui which connects to our vCenter server(s) and executes a number of powerCLI commands when corresponding button is pressed.
All my codes is working fine BUT i cannot get this Progressbaroverlay to work. Some of my PowerCLI commands take several seconds to complete, I want a progress bar (or animated icon) to visualise that the command is executing.
I cannot get my head around how it works.
For Example :
This is my code :

Code: Select all

$buttonEveryVMDiskCapacity_Click={
	#TODO: Place custom script here
	$EveryVMdisk = ForEach ($VM in Get-VM) { ($VM.Extensiondata.Guest.Disk | Select @{ N = "Name"; E = { $VM.Name } }, DiskPath, @{ N = "Capacity(MB)"; E = { [math]::Round($_.Capacity/ 1MB) } }, @{ N = "Free Space(MB)"; E = { [math]::Round($_.FreeSpace / 1MB) } }, @{ N = "Free Space %"; E = { [math]::Round(((100 * ($_.FreeSpace))/ ($_.Capacity)), 0) } }) }
	Update-DataGridView -DataGridView $datagridview1 -Item $EveryVMdisk -AutoSizeColumns AllCells
}
I know i need this :

Code: Select all

$progressbaroverlay1_Click={
	#TODO: Place custom script here
	
}
How do I get my code, and wrap it in the progressoverlay? I have read the help files on Sapien but I am still confused :(

Thank you in advance.

Richard

User avatar
davidc
Posts: 5225
Joined: Thu Aug 18, 2011 4:56 am

Re: ProgressbarOverlay problems

Post by davidc » Thu Feb 22, 2018 10:00 am

[TOPIC MOVED TO POWERSHELL GUIS BY MODERATOR]

This article may help:

https://info.sapien.com/index.php/guis/ ... sive-loops
David
SAPIEN Technologies, Inc.

User avatar
TonySpeight
Posts: 4
Joined: Wed Dec 13, 2017 9:13 am

Re: ProgressbarOverlay problems

Post by TonySpeight » Tue Feb 27, 2018 9:15 am

People may suggest creating a job and use the Update Job function within Powershell Studio.

I have a robocopy script which is has kind of similar commands which may help you.

I'm no expert in Powershell but I have been learning over the past few months. I have added a small sample of a script I have used.
  1. $progress = Receive-Job -Job $robocopyjob -Keep -ErrorAction SilentlyContinue
  2.  
  3.         if ($progress)
  4.  
  5.         {
  6.  
  7.             $copiedfiles = ($progress | Select-String -SimpleMatch 'new file', 'newer')
  8.  
  9.             if ($copiedfiles.count -le 0) { $TotalFilesCopied = $copiedfiles.Count }
  10.  
  11.             else { $TotalFilesCopied = $copiedfiles.Count - 1 }
  12.  
  13.             $FilesRemaining = ($totalfiles.count - $TotalFilesCopied)
  14.  
  15.             $Bytesarray = @()
  16.  
  17.             foreach ($Newfile in $copiedfiles)
  18.  
  19.             {
  20.  
  21.                 if ($Newfile.tostring().substring(13, 13).trim().length -eq 9) { $Bytesarray += $Newfile.tostring().substring(13, 15).trim() }
  22.  
  23.                 else { $Bytesarray += $Newfile.tostring().substring(13, 13).trim() }
  24.  
  25.             }
  26.  
  27.             $bytescopied = ([int64]$Bytesarray[-1] * ($Filepercentcomplete/100))
  28.  
  29.             $totalfilebytes = [int64]$Bytesarray[-1]
  30.  
  31.             $TotalBytesCopied = ((($Bytesarray | Measure-Object -Sum).sum) - $totalfilebytes) + $bytescopied
  32.  
  33.             $TotalBytesRemaining = ($totalbytes - $totalBytesCopied)
  34.  
  35.             if ($copiedfiles)
  36.  
  37.             {
  38.  
  39.                 if ($copiedfiles[-1].tostring().substring(13, 13).trim().length -eq 9) { $currentfile = $copiedfiles[-1].tostring().substring(28).trim() }
  40.  
  41.                 else { $currentfile = $copiedfiles[-1].tostring().substring(25).trim() }
  42.  
  43.             }
  44.  
  45.             $totalfilescount = $totalfiles.count
  46.  
  47.             if ($progress[-1] -match '%') { $Filepercentcomplete = $progress[-1].substring(0, 3).trim() }
  48.  
  49.             else { $Filepercentcomplete = 0 }
  50.  
  51.             $totalPercentcomplete = (($TotalBytesCopied/$totalbytes) * 100)
  52.  
  53.             if ($totalbytes -gt 2gb) { $BytesCopiedprogress = "{0:N2}" -f ($totalBytesCopied/1gb); $totalbytesprogress = "{0:N2}" -f ($totalbytes/1gb); $bytes = 'Gbytes' }
  54.  
  55.             else { $BytesCopiedprogress = "{0:N2}" -f ($totalBytesCopied/1mb); $totalbytesprogress = "{0:N2}" -f ($totalbytes/1mb); $bytes = 'Mbytes' }
  56.  
  57.             if ($totalfilebytes -gt 1gb) { $totalfilebytes = "{0:N2}" -f ($totalfilebytes/1gb); $bytescopied = "{0:N2}" -f ($bytescopied/1gb); $filebytes = 'Gbytes' }
  58.  
  59.             else { $totalfilebytes = "{0:N2}" -f ($totalfilebytes/1mb); $bytescopied = "{0:N2}" -f ($bytescopied/1mb); $filebytes = 'Mbytes' }
  60.  
  61.            
  62.  
  63.             If ($totalPercentcomplete -gt 0)
  64.  
  65.             {
  66.  
  67.                 $TotalProgressbar.Value = $totalPercentcomplete
  68.  
  69.             }

User avatar
jvierra
Posts: 12258
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: ProgressbarOverlay problems

Post by jvierra » Tue Feb 27, 2018 9:52 am

The Window Form cannot be updated while a command is running. Use a job or the Job Tracker custom control to allow a form to remain responsive while long running commands are executing.

A simple partial fix is to add a "DoEvents" command after each PowerCli command.

[System.Windows.Forms.Application]::DoEvents()

Locked