First progress bar for export ad users

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.
Posts: 4
Last visit: Thu Aug 11, 2022 12:42 am

First progress bar for export ad users

Post by raf65tuir »

Please indulgence for me, I want to create a first progress bar in the Get-ADUser selection. I have read most of the help on the sapiens pages, but my script doesn't show the progress correctly or doesn't create a file.
Thank you.
(1.18 KiB) Downloaded 18 times
Posts: 15285
Last visit: Sat Sep 24, 2022 12:01 pm
Answers: 26
Has voted: 4 times
Been upvoted: 27 times

Re: First progress bar for export ad users

Post by jvierra »

What you are trying to do cannot be done as Get-AdUser is a blocking call. Once event code is blocked then you cannot execute any other code until the bocking ends. Enumerating the result will happen so fast you will not see the pbar increment.

It is also better to increment the value as the method you are using is delayed by messaging and doesn't work well in an event. You can try adding "DoEvents" in the loop which can help.

The following will get you closer. This is also one case where you should void negative logic as it creates confusion for you and others.
  1. $buttonSearch_Click = {
  3.     $progressbar1.Maximum = $AllUsers.Count;
  4.     $progressbar1.Step = 1;
  5.     $progressbar1.Value = 0;
  8.     If ($checkbox1.Checked) {
  9.         #I want to export all users from AD with some values to file and showing progress on progress bar.
  10.         [array]$AllUsers += Get-Aduser -Filter * -Properties samaccountname |
  11.             ForEach-Object{
  12.                 $progressbar1.PerformStep();
  13.                 [System.Windows.Forms.Application]::DoEvents()
  14.             }
  15.     }else{
  16.         #$textbox1.text is a place where will be write samaccountname user.
  17.         Get-ADUser -Identity $textbox1.Text -Properties * |
  18.             select GivenName, Surname, Name, DisplayName, mailNickname, mail, EmailAddress, Enabled, memberOf |
  19.             Out-GridView
  20.     }
  21. }