Search code examples
powershellpowershell-studio

Connect a function to progess-bar in powershell studio


Is there a way that I can connect a function to a progress bar object?

$buttonAdd_Click = {
    create
    $loadingbar.Refresh
}

This is my button object. The variable `$loadingbar is my progess-bar object.

This is my create function:

function create()
{

    $wshell = New-Object -ComObject Wscript.Shell
    $UserList = Import-Csv -Path $txt_csv.Text -Delimiter ";" | Select-Object -ExpandProperty UPN   


<# ------- CREATE USERS ------- #>
    foreach ($User in $UserList)
    {

        $OU = $User.path
        $UPN = $User.UPN
        $Password = $User.password
        $Detailedname = $User.firstname + " " + $User.Lastname
        $UserFirstname = $User.Firstname
        $FirstLetterFirstname = $UserFirstname.substring(0, 1)
        $SAM = $User.UPN
        $Company = $User.company
        $Description = $User.description
        $AccountExpirationDate = $User.accountexpirationdate

        $params = @{
            'Name' = $Detailedname;
            'SamAccountName' = $SAM;
            'UserPrincipalName' = $UPN + '@ib.nl';
            'DisplayName' = $Detailedname;
            'GivenName' = $UserFirstname;
            'Surname' = $User.Lastname;
            'AccountPassword' = (ConvertTo-SecureString $Password -AsPlainText -Force);
            'Enabled' = $True;
            'PasswordNeverExpires' = $True;
            'Path' = $OU;
            'Company' = $Company;
            'Description' = $Description;
            'AccountExpirationDate' = $AccountExpirationDate
        }

        New-ADUser @params
    }
    }`

The question: when I click on the button I want to see the progess of the create function into the loading bar. How can I do that?

Another question: what is the different between progessbar and progress baroverlay in Powershell studio?


Solution

  • I just had work around. I used just hard-coded values to see my loading bar working. For example in the at the begin of the method I use $loadingbar.value = 50; and at the end I used $loadingbar.value = 100;. A little bit dirty code, but the owner was satisfied.