Author Posts

April 23, 2018 at 10:41 am

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"
    $Form.Controls.Add($Button1)

    #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"
    $form.Controls.Add($label1) 

    #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
    $Form.Controls.Add($PictureBoxLabel)
    
 
    
    #Add Button event 
    $Button1.Add_Click(
    
{   $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 
    $form.ShowDialog()

April 23, 2018 at 12:49 pm

Look up "Wait-Job" (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/wait-job?view=powershell-6). 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.

April 23, 2018 at 1:17 pm

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

Sample:

$button1_Click={
		#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
		$textbox1.Text=$OutputJob.DisplayName
	}

April 26, 2018 at 11:38 am

Thanks for that, I will give it a try!

April 26, 2018 at 11:51 am

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

April 26, 2018 at 1:29 pm

@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