Page 1 of 1

First progress bar for export ad users

Posted: Fri Aug 05, 2022 10:03 am
by raf65tuir
Hello,
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.

Re: First progress bar for export ad users

Posted: Fri Aug 05, 2022 2:35 pm
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 = {
  2.    
  3.     $progressbar1.Maximum = $AllUsers.Count;
  4.     $progressbar1.Step = 1;
  5.     $progressbar1.Value = 0;
  6.    
  7.    
  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. }
  22.