I am redoing my old form to look more modern and I'm updating the scripts. I was looking at jobs and got them working fine but I was wondering if there was a way to output to multiple text boxes while the code is running, instead of populating all textboxes at the end. I'm also trying to update the progress bar after each textbox is filled. I tried adding my "completed" script block to the "updated" and now it's not working too great. Any help would be appreciated.
- $buttonStartJob_Click = {
- $buttonStartJob.Enabled = $false
- #$toolstripprogressbar1.Visible = $true
- $progressbaroverlay1.Visible = $true
- $progressbaroverlay1.Text = "Please wait, this will take a few moments..."
- #Initialize the Progress Bar
- $progressbaroverlay1.Maximum = 7;
- $progressbaroverlay1.Step = 1;
- $progressbaroverlay1.Value = 1;
- #Create a New Job using the Job Tracker
- $paramAddJobTracker = @{
- Name = 'JobName'
- JobScript = {
- #--------------------------------------------------
- #TODO: Set a script block
- #Important: Do not access form controls from this script block.
- Param ($computerName) #Pass any arguments using the ArgumentList parameter
- for ($i = 0; $i -lt 50; $i++) { Start-Sleep -Milliseconds 1 }
- #region Gets username of current user
- $userid = Get-CimInstance -ComputerName $computerName -Class win32_ComputerSystem | Select-Object UserName -ExpandProperty UserName
- #Email Gets email of current user without using AD module
- $SAMName = "$userid"
- $Results2 = @()
- $Root = [ADSI]''
- $Searcher = New-Object System.DirectoryServices.DirectorySearcher($Root)
- $Searcher.filter = "(&(objectClass=user)(sAMAccountName= $SAMName))"
- $User = $Searcher.findall()
- If ($User)
- {
- # Create PS Object
- $Object = New-Object PSObject -Property @{
- Email = (($User.Properties).mail | Out-String)
- }
- $Results2 += $Object
- }
- # Display results
- $Email = $Results2 | Select-Object Email -ExpandProperty Email
- #endregion
- #region Get Serial Number
- $serialNumber = Get-CimInstance -ComputerName $computerName -Class win32_Bios | Select-Object SerialNumber -ExpandProperty SerialNumber
- #endregion
- #region Get Model
- $model = Get-CimInstance -ComputerName $computerName -Class win32_ComputerSystem | Select-Object Model -ExpandProperty Model
- #endregion
- #region Get Build Number
- $build = Invoke-Command -ComputerName $computerName {
- (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId
- }
- #endregion
- #region Get-OS Version
- $operatingSystem = (Get-CimInstance -ComputerName $computerName -class Win32_OperatingSystem).Caption
- #endregion
- #region Get-Office Version
- $officeVersion = invoke-command -computername $computerName -scriptblock {
- if (test-path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\O365*')
- {
- Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\O365*' | Select-Object DisplayVersion -ExpandProperty DisplayVersion
- Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Microsoft\Office\16.0\Outlook' -Name Bitness | Select-Object Bitness -ExpandProperty Bitness
- }
- }
- #endregion
- #region Get Last Reboot
- $restart = Get-CimInstance -ComputerName $computerName -ClassName win32_operatingsystem | select -ExpandProperty lastbootuptime
- $date = Get-Date
- $ts = New-TimeSpan -Start $restart -End $date
- $its = $ts.Days
- #endregion
- $obj = @{ }
- $obj.user = $userid
- $obj.email = $email
- $obj.serial = $serialNumber
- $obj.model = $model
- $obj.build = $build
- $obj.os = $operatingSystem
- $obj.version = $officeVersion
- $obj.restart = $its
- New-Object psobject -property $obj
- #--------------------------------------------------
- }
- ArgumentList = $textbox1.Text
- CompletedScript = {
- Param ([System.Management.Automation.Job]$Job)
- #THIS IS WHAT I HAD BEFORE MOVING IT TO UPDATESCRIPT#IT OUTPUT FINE BUT WAS ONLY AT THE END OF THE SCRIPT#
- #$results = Receive-Job -Job $Job
- #$textbox3.Text = $results.user
- #$textbox2.Text = $results.email
- #$textbox4.Text = $results.serial
- #$textbox5.Text = $results.build
- #if ($textbox5.TextLength -eq 0) { $textbox5.Text = "N/A" }
- #$textbox6.Text = $results.model
- #$textbox7.Text = $results.os
- #$textbox18.Text = $results.restart
- #$textbox19.Text = $results.version
- #if ($textbox19.textLength -eq 0) { $textbox19.Text = "Old Version of Office Detected. Please Update" }
- $progressbaroverlay1.Value = 7
- $progressbaroverlay1.Text = "Complete"
- #Enable the Button
- $buttonStartJob.ImageIndex = -1
- $buttonStartJob.Enabled = $true
- }
- UpdateScript = {
- Param ([System.Management.Automation.Job]$Job)
- $results = Receive-Job -Job $Job -Keep
- ## - Grab results from background job:
- $textbox3.Text = $results.user
- $textbox2.Text = $results.email
- $progressbaroverlay1.PerformStep();
- $textbox4.Text = $results.serial
- $progressbaroverlay1.PerformStep();
- $textbox5.Text = $results.build
- if ($textbox5.TextLength -eq 0) { $textbox5.Text = "N/A" }
- $progressbaroverlay1.PerformStep();
- $textbox6.Text = $results.model
- $progressbaroverlay1.PerformStep();
- $textbox7.Text = $results.os
- $progressbaroverlay1.PerformStep();
- $textbox18.Text = $results.restart
- $progressbaroverlay1.PerformStep();
- $textbox19.Text = $results.version
- if ($textbox19.textLength -eq 0) { $textbox19.Text = "Old Version of Office Detected. Please Update" }
- ## - Increment progress bar:
- #Animate the Button
- if ($null -ne $buttonStartJob.ImageList)
- {
- if ($buttonStartJob.ImageIndex -lt $buttonStartJob.ImageList.Images.Count - 1)
- {
- $buttonStartJob.ImageIndex += 1
- }
- else
- {
- $buttonStartJob.ImageIndex = 0
- }
- }
- }
- }
- Add-JobTracker @paramAddJobTracker
- }
- }