Search code examples
powershellstatusbar

Redirect Powershell Status output for form


I created a form in powershell and I would like to redirect the status that shows in the ISE to a popup. Currently when I run the script outside of ISE I do not see the status bar, but seeing it would be very helpful.

Here is the function

Function Upload-File {

    param (
        $LocalData
    )

Add-PSSnapin ShareFile

#Run the following interactively to create a login token that can be used by Get-SfClient in unattended scripts
$sfClient = Get-SfClient -Name ((Join-Path $env:USERPROFILE "Documents\Sharefile") + "\MySubdomain.sfps")


#upload directory is relative to the root of the account
#get the current user's home folder to use as the starting point
$ShareFileHomeFolder = (Send-SfRequest $sfClient -Entity Items).Url

# Create a PowerShell provider for ShareFile pointing to Personal Folsers\Send

New-PSDrive -Name sfDrive -PSProvider ShareFile -Client $sfClient -Root "\Personal Folders\Send" -RootUri $ShareFilePath

#upload all the files (recursively) in the local folder to the specified folder in ShareFile
Copy-SfItem -Path $LocalData -Destination "sfDrive:"

#Remove the PSProvider when we are done
Remove-PSDrive sfdrive
}

When it runs in ISE

Function running in ISE

Is there a way to redirect that to a pop-up window?

This is the command to call the function

 Start-Process (Upload-File -LocalData "$LocalData") -Wait

I saw the write-progress command but didn't see how I could apply it here. Should I be doing something different?


Solution

  • As per my comment, you must add form/gui resources/namespaces to the top of your code when running from the consoles, for form stuff to load since they are not auto-loaded as they are in the ISE.

    Note: You don't need all of the namespaces. I only put these here to make you aware of the most common ones used in PS GUI scenarios.

    Add-Type -AssemblyName  System.Drawing,
                            PresentationCore,
                            PresentationFramework,
                            System.Windows.Forms,
                            microsoft.VisualBasic
    [System.Windows.Forms.Application]::EnableVisualStyles()
    
    
    $Form               = New-Object System.Windows.Forms.Form
    $Form.width         = 400
    $Form.height        = 600
    $Form.Text          = 'Add Resource'
    $Form.StartPosition = 'CenterScreen'
    
    $pbrTest          = New-Object System.Windows.Forms.ProgressBar
    $pbrTest.Maximum  = 100
    $pbrTest.Minimum  = 0
    $pbrTest.Location = new-object System.Drawing.Size(10,10)
    $pbrTest.size = new-object System.Drawing.Size(100,50)
    
    $i = 0
    
    $Form.Controls.Add($pbrTest)
    
    $btnConfirm          = new-object System.Windows.Forms.Button
    $btnConfirm.Location = new-object System.Drawing.Size(120,10)
    $btnConfirm.Size     = new-object System.Drawing.Size(100,30)
    $btnConfirm.Text     = 'Start Progress'
    $Form.Controls.Add($btnConfirm)
    
    
    $btnConfirm.Add_Click({
        
        While ($i -le 100) 
        {
            $pbrTest.Value = $i
            Start-Sleep -m 1
            'VALUE EQ'
            $i
            $i += 1
        }
    })
    
    
    $Form.Add_Shown({$Form.Activate()})
    $Form.ShowDialog()