none
Form Button running Script that does not complete Progress Bar RRS feed

  • Question

  • Hello,

    Currently I have created a form using the System.Reflection.Assembly which includes a textbox and button.   The button calls a script with a progress bar using the write-progress method, but when the progress bar completes it does not go away.

    Any assistance is appreciated.   As the code is quite long and in several files I have posted some of the code below.

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $getVMware       = Join-Path -path $ScriptPath -ChildPath "bin\get-vmhosts.ps1"
    Add-PSSnapin VMware.VIMAutomation.Core -ErrorAction SilentlyContinue
    
    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = "VMware Script - Created by Walter Beach"
    $objForm.Size = New-Object System.Drawing.Size(650,390) 
    $objForm.StartPosition = "CenterScreen"
    
    $AuditButton = New-Object System.Windows.Forms.Button
    $AuditButton.Location = New-Object System.Drawing.Size(310,80)
    $AuditButton.Size = New-Object System.Drawing.Size(75,23)
    $AuditButton.Text = "Audit"
    $AuditButton.Add_Click({ get-vmhosts | set-vmdnsservers -DNS $DNS })
    $objForm.Controls.Add($AuditButton)
    
    Function set-vmdnsservers
    {
    
    param
    (
      [Parameter(ValueFromPipelineByPropertyName=$true)]$Name,
      [Parameter(Mandatory=$true,HelpMessage="Example: 192.168.1.1")]$DNS
    )
    
    $i = 0
    $itemCount=$input.Count
    
    write-host ""
    Write-Host "Configuring DNS Server  " -NoNewline
    
        $Input | ForEach {
    
           IF ($PSVersionTable.PSVersion.Major -ge 3) { Write-Progress -activity "Configuring DNS Servers" -status $_ -PercentComplete (($i++ / $itemCount)  * 100) } Else { ticker }
    
            $_ | Get-VMHostNetwork | Set-VMHostNetwork -DnsAddress $DNS | Out-Null
    
        } # End For Each Loop
    
    } # End of set-vmdnsservers


    Walter

    Sunday, January 19, 2014 5:38 PM

Answers

  • You need to put a final call to Write-Progress with the -Completed switch, probably right after your ForEach loop, in this case:

        $Input | ForEach {
    
           IF ($PSVersionTable.PSVersion.Major -ge 3) { Write-Progress -activity "Configuring DNS Servers" -status $_ -PercentComplete (($i++ / $itemCount)  * 100) } Else { ticker }
    
            $_ | Get-VMHostNetwork | Set-VMHostNetwork -DnsAddress $DNS | Out-Null
    
        } # End For Each Loop
    
        Write-Progress -Activity "Configuring DNS Servers" -Completed
    

    • Marked as answer by Walter Beach Sunday, January 19, 2014 7:59 PM
    Sunday, January 19, 2014 5:48 PM

All replies

  • You need to put a final call to Write-Progress with the -Completed switch, probably right after your ForEach loop, in this case:

        $Input | ForEach {
    
           IF ($PSVersionTable.PSVersion.Major -ge 3) { Write-Progress -activity "Configuring DNS Servers" -status $_ -PercentComplete (($i++ / $itemCount)  * 100) } Else { ticker }
    
            $_ | Get-VMHostNetwork | Set-VMHostNetwork -DnsAddress $DNS | Out-Null
    
        } # End For Each Loop
    
        Write-Progress -Activity "Configuring DNS Servers" -Completed
    

    • Marked as answer by Walter Beach Sunday, January 19, 2014 7:59 PM
    Sunday, January 19, 2014 5:48 PM
  • You are correct that was all that was needed.

    Thank you for your help and quick response.


    Walter

    Sunday, January 19, 2014 8:00 PM