response from get-job only on form exit

Welcome Forums General PowerShell Q&A response from get-job only on form exit

This topic contains 5 replies, has 3 voices, and was last updated by

9 months, 3 weeks ago.

  • Author
  • #99460

    Points: 0
    Rank: Member

    Hi Guys,

    I'm new to the forum and running into probably a noob thing, but I tried a lot of different things and I'm stuck now.
    What I want is get a response from the job when it's finished in the background and then start running something else. The reason it is in a job, is because otherwise the form will look like it is hanging until it is finished. The reason I need the Expand is because the AD-group has too many members and I run into a limit. That being said, that piece of code does exactly what I need it to do.
    At the moment I am showing a message-box, because of a test, but this only comes up when I close the form.

        Add-Type -AssemblyName System.Windows.Forms     
        Add-Type -AssemblyName System.Drawing
        Get-Job |Remove-Job
        $scriptDir = "C:\scripts\Grid Users Disabled"
        # Build Form
        $Form = New-Object System.Windows.Forms.Form
        $Form.Text = "Clean up Grid Tokens"
        $Form.Size = New-Object System.Drawing.Size(335,250)
        $Form.StartPosition = "CenterScreen"
        $Form.Topmost = $False
        # Add Button
        $Button1 = New-Object System.Windows.Forms.Button
        $Button1.Location = New-Object System.Drawing.Size(35,75)
        $Button1.Size = New-Object System.Drawing.Size(75,23)
        $Button1.Text = "Start"
        #Add label
        $label1 = New-Object System.Windows.Forms.Label
        $label1.Location = New-Object System.Drawing.Point(35,25) 
        $label1.Size = New-Object System.Drawing.Size(250,23)
        $label1.Text = "Please click start to clean up"
        #Add a picturebox
        ########### PictureBox Label
        $PictureBoxLabel = New-Object System.Windows.Forms.Label
        $PictureBoxLabel.Location = New-Object System.Drawing.Size(135,85) 
        $PictureBoxLabel.Size = New-Object System.Drawing.Size(32,32) 
        $PictureBoxLabel.Image=[System.Drawing.Image]::FromFile($scriptDir + '\prgSpinner.gif')
        $PictureBoxLabel.Visible = $False
        #Add Button event 
    {   $label1.Text = "Cleaning up. Please wait...."
        $PictureBoxLabel.Visible = $true
        $Button1.Visible = $false
    # Get the Ad user, filter out the disabled users and export the usernames to a CSV file
    $j = Start-Job -Name Job1 -ScriptBlock {
        Get-ADGroup "G-UG-CryptoCard-Grid-Users" -Properties Member|Select-Object -ExpandProperty Member |Get-ADUser|Select-Object name, enabled| Where-Object {$_.enabled -ne "False"} |Select-Object name| Export-csv -path "C:\temp\GridDisabledUsers.csv" -NoTypeInformation -Delimiter ";"
    $action = { [System.Windows.MessageBox]::Show('Done');Stop-Job -Name Check;Get-Job | Remove-Job }
    Register-ObjectEvent -InputObject $j -EventName StateChanged -SourceIdentifier Check -Action $action
        #Show the Form 
  • #99471

    Points: 0
    Rank: Member

    Look up "Wait-Job" ( Now since it is a GUI I think it may still cause the GUI to be un-responsive; I have not tested but I can and get back to you later.

    • #99475

      Points: 0
      Rank: Member

      Ok tested with a simple UI and yeah it still stops interaction with the GUI but does not show up as "Not Responding".


      		#TODO: Place custom script here
      		$SB = { get-qaduser "johnny boy" }
      		start-job -Name QADuser01 -ScriptBlock $SB
      		wait-job -Name QADuser01
      		$OutputJob = Receive-Job -Name QADuser01
  • #99817

    Points: 0
    Rank: Member

    Thanks for that, I will give it a try!

  • #99819

    Points: 64
    Rank: Member

    Have you tried running your code in a separate runspace ? This link below may give you some ideas or search for boe Prox on runspaces

    Part V – Building Responsive PowerShell Apps with Progress bars

  • #99834

    Points: 0
    Rank: Member

    @nick : If I implement that in my code, the form hangs, it doesn't say it isn't responding, but that's it.
    @simon, I will have a look at it

The topic ‘response from get-job only on form exit’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort