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()