Powershell Forms Progress Bar Not Working Correctly

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
monarch684
Posts: 3
Last visit: Tue Mar 06, 2018 1:36 pm

Powershell Forms Progress Bar Not Working Correctly

Post by monarch684 »

All,

I have created a tool for my company so that our Remote Workstations Updates can be controlled (they do not want to pay for something pre-built to do this.). Everything in the tool works as it should with the exception of a progress bar. When the updates are being downloaded, there should be a marquee progress bar showing. It works great when testing in Studio 2016. Once it is deployed, however, the progress bar just doesn't show up. Any help is appreciated. This is also posted on TechNet

Running Script

Code: Select all

#region Notes
<#	
	.NOTES
	===========================================================================
	 Created with: 	SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.129
	 Created on:   	11/21/2017 8:59 AM
	 Created by:   	
	 Organization: 	
	 Filename:     	
	===========================================================================
	.DESCRIPTION
		A description of the file.
#>
#endregion

#region Functions
# Get Dynamic Date
function Get-TimeStamp {
	return (Get-Date -Format o)
}

# Prompt for Reboot
function Reboot {
	"$(Get-TimeStamp) | Informa | Workstation needs reboot" | Out-File -FilePath $logpath -Append -Encoding $encoding
	"$(Get-TimeStamp) | Informa | Prompting End User for reboot" | Out-File -FilePath $logpath -Append -Encoding $encoding
	
	[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
	$reboot = [System.Windows.Forms.MessageBox]::Show("This workstation needs to reboot in order to complete the updates.  Do you wish to reboot now?", "Monthly Updates", 4)
	if ($reboot -eq "Yes") {
		"$(Get-TimeStamp) | Informa | End User is rebooting" | Out-File -FilePath $logpath -Append -Encoding $encoding
		$ftp = [System.Net.FtpWebRequest]::Create("ftp://domain/Public/Monthly Updates/Logs/$logfile")
		$ftp = [System.Net.FtpWebRequest]$ftp
		$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
		
		$content = [System.IO.File]::ReadAllBytes($logpath)
		$ftp.ContentLength = $content.Length
		$rs = $ftp.GetRequestStream()
		$rs.Write($content, 0, $content.Length)
		$rs.Close()
		$rs.Dispose()
		
		Restart-Computer
	}
	else {
		"$(Get-TimeStamp) | Informa | End User is rebooting later" | Out-File -FilePath $logpath -Append -Encoding $encoding
		[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
		[System.Windows.Forms.MessageBox]::Show("Please reboot by the end of your workday for updates to finish installing.", "Monthly Updates", 0)
		$ftp = [System.Net.FtpWebRequest]::Create("ftp://domain/Public/Monthly Updates/Logs/$logfile")
		$ftp = [System.Net.FtpWebRequest]$ftp
		$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
		
		$content = [System.IO.File]::ReadAllBytes($logpath)
		$ftp.ContentLength = $content.Length
		$rs = $ftp.GetRequestStream()
		$rs.Write($content, 0, $content.Length)
		$rs.Close()
		$rs.Dispose()
		
		$form1.close()
	}
}
#endregion

#region Global Variables
$pc = $ENV:COMPUTERNAME
$tempdir = "C:\Temp\Updates\"
$timestamp = Get-Date -Format ("yyyyMMddhhmmss")
$logfile = $pc + "_" + $timestamp +".txt"
$logpath = $tempdir + $logfile
$encoding = "ASCII"
$os = (Get-WmiObject Win32_OperatingSystem).Caption
$osversion = (Get-WmiObject Win32_OperatingSystem).Version
$version = switch ($osversion) {
	10.0.10586 { "1511" }
	10.0.14393 { "1607" }
	10.0.15063 { "1703" }
	10.0.16299 { "1709" }
	10.0.17040 { "1803" }
	default { "0" }
}
$ftpserver = "ftp://domain"
$ftpfolder = "Public/Monthly Updates"
$ftpuri = $ftpserver + "/" + $ftpfolder
$regroot = "HKLM:\SOFTWARE\regkey\Monthly Updates"
$reguninstall = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\regkey"
$regupdateroot = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate"
$wusatxt = "C:\Temp\Updates\wusalog.txt"
#endregion

#region Start Log File
New-Item -ItemType file -Path C:\Temp\Updates -Name $logfile -Force | Out-Null
"$(Get-TimeStamp) | Informa | Start Logging ..." | Out-File -FilePath $logpath -Append -Encoding $encoding
"$(Get-TimeStamp) | Informa | Log file set to $logpath" | Out-File -FilePath $logpath -Append -Encoding $encoding
"$(Get-TimeStamp) | Informa | Workstation Name: $pc" | Out-File -FilePath $logpath -Append -Encoding $encoding
"$(Get-TimeStamp) | Informa | Operating System: $os" | Out-File -FilePath $logpath -Append -Encoding $encoding
"$(Get-TimeStamp) | Informa | Version ID: $osversion" | Out-File -FilePath $logpath -Append -Encoding $encoding
"$(Get-TimeStamp) | Informa | Version Name: $version" | Out-File -FilePath $logpath -Append -Encoding $encoding
#endregion

#region Get Update Settings
"$(Get-TimeStamp) | Informa | Verifying Windows Update setting in registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
try {
	$ilocations = (Get-ItemProperty -Path $regupdateroot -Name "DoNotConnectToWindowsUpdateInternetLocations").DoNotConnectToWindowsUpdateInternetLocations
	"$(Get-TimeStamp) | Success | Do Not Connect to Windows Update Internet Locations: $ilocations" | Out-File -FilePath $logpath -Append -Encoding $encoding
}
catch {
	"$(Get-TimeStamp) | Failure | Unable to retrieve Do Not Connect to Windows Update Internet Locations value" | Out-File -FilePath $logpath -Append -Encoding $encoding
}

try {
	$UXWUAccess = (Get-ItemProperty -Path $regupdateroot -Name "SetDisableUXWUAccess").SetDisableUXWUAccess
	"$(Get-TimeStamp) | Success | Set Disable UXWU Access: $UXWUAccess" | Out-File -FilePath $logpath -Append -Encoding $encoding
}
catch {
	"$(Get-TimeStamp) | Failure | Unable to retrieve Set Disable UXWU Access value" | Out-File -FilePath $logpath -Append -Encoding $encoding
}

try {
	$NoAutoUpdate = (Get-ItemProperty -Path "$regupdateroot\AU" -Name "NoAutoUpdate").NoAutoUpdate
	"$(Get-TimeStamp) | Success | No Auto Update: $NoAutoUpdate" | Out-File -FilePath $logpath -Append -Encoding $encoding
}
catch {
	"$(Get-TimeStamp) | Failure | Unable to retrieve No Auto Updates value" | Out-File -FilePath $logpath -Append -Encoding $encoding
}

try {
	$AUOptions = (Get-ItemProperty -Path "$regupdateroot\AU" -Name "AUOptions").AUOptions
	"$(Get-TimeStamp) | Success | AU Options: $AUOptions" | Out-File -FilePath $logpath -Append -Encoding $encoding
}
catch {
	"$(Get-TimeStamp) | Failure | Unable to retrieve AU Options value" | Out-File -FilePath $logpath -Append -Encoding $encoding
}

try {
	$NoAUAsDefaultShutdownOption = (Get-ItemProperty -Path "$regupdateroot\AU" -Name "NoAUAsDefaultShutdownOption").NoAUAsDefaultShutdownOption
	"$(Get-TimeStamp) | Success | No AU As Default Shotdown Option: $NoAUAsDefaultShutdownOption" | Out-File -FilePath $logpath -Append -Encoding $encoding
}
catch {
	"$(Get-TimeStamp) | Failure | Unable to retrieve No AU As Default Shotdown Option value" | Out-File -FilePath $logpath -Append -Encoding $encoding
}

try {
	$NoAUShutdownOption = (Get-ItemProperty -Path "$regupdateroot\AU" -Name "NoAUShutdownOption").NoAUShutdownOption
	"$(Get-TimeStamp) | Success | No AU Shutdown Option: $NoAUShutdownOption" | Out-File -FilePath $logpath -Append -Encoding $encoding
}
catch {
	"$(Get-TimeStamp) | Failure | Unable to retrieve No AU Shutdown Option value" | Out-File -FilePath $logpath -Append -Encoding $encoding
}
#endregion

#region Reset Update Settings if Needed

if (Test-Path $regupdateroot) {
	if (Test-Path "$regupdateroot`\AU") { }
	else {
		"$(Get-TimeStamp) | Informa | Windows Update settings are not set properly" | Out-File -FilePath $logpath -Append -Encoding $encoding
		"$(Get-TimeStamp) | Informa | Resetting Windows Update Settings" | Out-File -FilePath $logpath -Append -Encoding $encoding
		try {
			New-Item -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate" -Name "AU" -Force | Out-Null
			"$(Get-TimeStamp) | Success | Created AU Key in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
		}
		catch {
			"$(Get-TimeStamp) | Success | Could not create AU Key in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
		}
		try {
			New-ItemProperty -Path "$regupdateroot\AU" -Name NoAutoUpdate -Value "1" -PropertyType DWORD -Force | Out-Null
			"$(Get-TimeStamp) | Success | Created NoAutoUpdate Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
		}
		catch {
			"$(Get-TimeStamp) | Success | Could not create NoAutoUpdate Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
		}
		try {
			New-ItemProperty -Path "$regupdateroot\AU" -Name AUOptions -Value "2" -PropertyType DWORD -Force | Out-Null
			"$(Get-TimeStamp) | Success | Created AUOptions Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
		}
		catch {
			"$(Get-TimeStamp) | Success | Could not create AUOptions Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
		}
		try {
			New-ItemProperty -Path "$regupdateroot\AU" -Name NoAUAsDefaultShutdownOption -Value "1" -PropertyType DWORD -Force | Out-Null
			"$(Get-TimeStamp) | Success | Created NoAUAsDefaultShutdownOption Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
		}
		catch {
			"$(Get-TimeStamp) | Success | Could not create NoAUAsDefaultShutdownOption Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
		}
		try {
			New-ItemProperty -Path "$regupdateroot\AU" -Name NoAUShutdownOption -Value "1" -PropertyType DWORD -Force | Out-Null
			"$(Get-TimeStamp) | Success | Created NoAUShutdownOption Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
		}
		catch {
			"$(Get-TimeStamp) | Success | Could not create NoAUShutdownOption Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
		}
	}
}
else {
	"$(Get-TimeStamp) | Informa | Windows Update settings are not set properly" | Out-File -FilePath $logpath -Append -Encoding $encoding
	"$(Get-TimeStamp) | Informa | Resetting Windows Update Settings" | Out-File -FilePath $logpath -Append -Encoding $encoding
	try {
		New-Item -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows" -Name "WindowsUpdate" -Force | Out-Null
		"$(Get-TimeStamp) | Success | Created WindowsUpdate Key in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	catch {
		"$(Get-TimeStamp) | Success | Could not create WindowsUpdate Key in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	try {
		New-Item -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate" -Name "AU" -Force | Out-Null
		"$(Get-TimeStamp) | Success | Created AU Key in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	catch {
		"$(Get-TimeStamp) | Success | Could not create AU Key in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	try {
		New-ItemProperty -Path "$regupdateroot" -Name DoNotConnectToWindowsUpdateInternetLocations -Value "1" -PropertyType DWORD -Force | Out-Null
		"$(Get-TimeStamp) | Success | Created DoNotConnectToWindowsUpdateInternetLocations Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	catch {
		"$(Get-TimeStamp) | Success | Could not create DoNotConnectToWindowsUpdateInternetLocations Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	try {
		New-ItemProperty -Path "$regupdateroot" -Name SetDisableUXWUAccess -Value "1" -PropertyType DWORD -Force | Out-Null
		"$(Get-TimeStamp) | Success | Created SetDisableUXWUAccess Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	catch {
		"$(Get-TimeStamp) | Success | Could not create SetDisableUXWUAccess Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	try {
		New-ItemProperty -Path "$regupdateroot\AU" -Name NoAutoUpdate -Value "1" -PropertyType DWORD -Force | Out-Null
		"$(Get-TimeStamp) | Success | Created NoAutoUpdate Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	catch {
		"$(Get-TimeStamp) | Success | Could not create NoAutoUpdate Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	try {
		New-ItemProperty -Path "$regupdateroot\AU" -Name AUOptions -Value "2" -PropertyType DWORD -Force | Out-Null
		"$(Get-TimeStamp) | Success | Created AUOptions Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	catch {
		"$(Get-TimeStamp) | Success | Could not create AUOptions Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	try {
		New-ItemProperty -Path "$regupdateroot\AU" -Name NoAUAsDefaultShutdownOption -Value "1" -PropertyType DWORD -Force | Out-Null
		"$(Get-TimeStamp) | Success | Created NoAUAsDefaultShutdownOption Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	catch {
		"$(Get-TimeStamp) | Success | Could not create NoAUAsDefaultShutdownOption Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	try {
		New-ItemProperty -Path "$regupdateroot\AU" -Name NoAUShutdownOption -Value "1" -PropertyType DWORD -Force | Out-Null
		"$(Get-TimeStamp) | Success | Created NoAUShutdownOption Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	catch {
		"$(Get-TimeStamp) | Success | Could not create NoAUShutdownOption Item in Registry" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
}

#endregion

#region Initial FTP Connection and Download
$ftpfile = $ftpuri + "/vars_2.txt"
"$(Get-TimeStamp) | Informa | Downloading Variables from $ftpuri" | Out-File -FilePath $logpath -Append -Encoding $encoding
try {
	Invoke-WebRequest -Uri $ftpfile -OutFile "$tempdir`\vars.txt"
	"$(Get-TimeStamp) | Success | Downloaded Variables from $ftpfile" | Out-File -FilePath $logpath -Append -Encoding $encoding
}
catch {
	"$(Get-TimeStamp) | Failure | FATAL ERROR: Could not download variables from $ftpfile" | Out-File -FilePath $logpath -Append -Encoding $encoding
	throw "Could not download variables from $ftpfile"
}

"$(Get-TimeStamp) | Informa | Creating vars variable" | Out-File -FilePath $logpath -Append -Encoding $encoding
try {
	$vars = Get-Content "C:\Temp\Updates\vars.txt"
	$patch = $vars
	"$(Get-TimeStamp) | Success | Created vars variable" | Out-File -FilePath $logpath -Append -Encoding $encoding
}
catch {
	"$(Get-TimeStamp) | Failure | FATAL ERROR: Could not create vars variable" | Out-File -FilePath $logpath -Append -Encoding $encoding
	throw "Could not create vars variable"
}
"$(Get-TimeStamp) | Informa | Patch level: $patch" | Out-File -FilePath $logpath -Append -Encoding $encoding
#endregion

#region Verify Current Patch Level
"$(Get-TimeStamp) | Informa | Verifying current patch level" | Out-File -FilePath $logpath -Append -Encoding $encoding
try {
	$cpatch = (Get-ItemProperty -Path $regroot -Name "CurrentPatchLevel").CurrentPatchLevel
	"$(Get-TimeStamp) | Success | Current Patch level: $cpatch" | Out-File -FilePath $logpath -Append -Encoding $encoding
}
catch {
	"$(Get-TimeStamp) | Failure | Unable to retrieve current patch level" | Out-File -FilePath $logpath -Append -Encoding $encoding
} 
#endregion

#region Start Install
$install = {
	$installButton.Text = "Downloading Updates"
	$installButton.Enabled = $false
	$installButton.Refresh()
	
	#region Get Installs to Download
	$ftpfile = $ftpuri + "/" + $patch + "/" + $version + "/contents.txt"
	
	"$(Get-TimeStamp) | Informa | Downloading Patch Contents from $ftpfile" | Out-File -FilePath $logpath -Append -Encoding $encoding
	try {
		Invoke-WebRequest -Uri $ftpfile -OutFile "C:\Temp\Updates\contents.txt"
		"$(Get-TimeStamp) | Success | Downloaded Patch Contents from $ftpfile" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	catch {
		"$(Get-TimeStamp) | Failure | FATAL ERROR: Could not download Patch Contents from $ftpfile" | Out-File -FilePath $logpath -Append -Encoding $encoding
		throw "Could not download contents from $ftpfile"
	}
	"$(Get-TimeStamp) | Informa | Creating files variable" | Out-File -FilePath $logpath -Append -Encoding $encoding
	try {
		$files = Get-Content "C:\Temp\Updates\contents.txt"
		"$(Get-TimeStamp) | Success | Created files variable" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	catch {
		"$(Get-TimeStamp) | Failure | FATAL ERROR: Could not create files variable" | Out-File -FilePath $logpath -Append -Encoding $encoding
		throw "Could not create files variable"
	}
	#endregion
	
	#region Download Installs	
	$ScriptBlock = {
		# Code to execute per runspace
		Param ($logpath,$ftpuri,$patch,$version,$file,$tempdir)
		
		# Download File
		function DownloadFile($url, $targetFile) {
			try {
				$uri = New-Object "System.Uri" "$url"
				$request = [System.Net.HttpWebRequest]::Create($uri)
				$response = $request.GetResponse()
				$responseStream = $response.GetResponseStream()
				$targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create
				$buffer = new-object byte[] 10KB
				$count = $responseStream.Read($buffer, 0, $buffer.length)
				$downloadedBytes = $count
				while ($count -gt 0) {
					$targetStream.Write($buffer, 0, $count)
					$count = $responseStream.Read($buffer, 0, $buffer.length)
					$downloadedBytes = $downloadedBytes + $count
				}
				$targetStream.Flush()
				$targetStream.Close()
				$targetStream.Dispose()
				$responseStream.Dispose()
				
				"$(Get-TimeStamp) | Success | Downloaded $ftpfile" | Out-File -FilePath $logpath -Append -Encoding ASCII
			}
			catch {
				"$(Get-TimeStamp) | Failure | Could not download $ftpfile" | Out-File -FilePath $logpath -Append -Encoding ASCII
			}
		}
		
		$ftpfile = $ftpuri + "/" + $patch + "/" + $version + "/" + $file
		$localfile = $tempdir + "\" + $file
		"$(Get-TimeStamp) | Informa | Downloading $ftpfile" | Out-File -FilePath $logpath -Append -Encoding $encoding
		DownloadFile $ftpfile $localfile
	}
		
	foreach ($file in $files) {
		$arg = $logpath, $ftpuri, $patch, $version, $file, $tempdir
		Start-Job $ScriptBlock -ArgumentList $arg
	}
	
	$pblabel.Text = "Downloading Updates"
	$pb.Style = "Marquee"
	$pblabel.Refresh()
	$pb.Refresh()
	
	do {
		$pb.Refresh()
		$jobs = Get-Job
		$jobscount = $jobs.Count
		
		$done = 0
		foreach ($job in $jobs) {
			$state = $job.State
			if ($state -eq "Completed") {
				$done = $done + 1
			}
		}
		Start-Sleep -Milliseconds 25
	}
	while ($done -lt $jobscount)
	Get-Job | Remove-Job
	
	"$(Get-TimeStamp) | Informa | Creating installfile variable" | Out-File -FilePath $logpath -Append -Encoding $encoding
	try {
		$installfiles = Get-ChildItem -Path $tempdir -File -Include *.exe, *.msi, *.msu -Recurse -Force | Select Name, @{ Name = "Path"; Expression = { $_.FullName } }
		"$(Get-TimeStamp) | Success | Created installfile variable" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	catch {
		"$(Get-TimeStamp) | Failure | Could not create installfile variable" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	
	$pb.Refresh()
	Start-Sleep -Seconds 2
	
	#endregion
	
	#region Install Items
	$installButton.Text = "Installing Updates"
	$installButton.Enabled = $false
	$installButton.Refresh()
	
	$pb.Style = "Continuous"
	$pb.Refresh()
	
	$filecount = Get-ChildItem -Path $tempdir -File -Include *.exe, *.msi, *.msu -Recurse -Force
	$counter = 0
	foreach ($item in $installfiles) {
		$counter++
		[Int]$percentage = ($counter/$filecount.Count) * 100
		$pb.Value = $percentage
		$item = $item.Path
		$pblabel.Text = "Installing " + $item
		$pblabel.Refresh()
		$pb.Refresh()
		
		"$(Get-TimeStamp) | Informa | Determining install procedure for $item" | Out-File -FilePath $logpath -Append -Encoding $encoding
		if ($item -like "*.msu") {
			"$(Get-TimeStamp) | Informa | Install *.msu file $item" | Out-File -FilePath $logpath -Append -Encoding $encoding
			$wusalog = $item + ".evtx"
			wusa $item /quiet /norestart /log:$wusalog | Out-Null
			Get-WinEvent -Path $wusalog -Oldest | Out-File -FilePath $wusatxt -Encoding $encoding -Force
			$wusalogstring = Select-String -Path $wusatxt -Pattern "error"
			if ($wusalogstring -ne $null) {
				$wusalograw = Get-Content -Path $wusatxt
				"$(Get-TimeStamp) | Failure | Could not install $item" | Out-File -FilePath $logpath -Append -Encoding $encoding
				$wusalograw | Out-File -FilePath $logpath -Append -Encoding $encoding
			}
			else {
				$wusalograw = Get-Content -Path $wusatxt
				"$(Get-TimeStamp) | Success | Installed $item" | Out-File -FilePath $logpath -Append -Encoding $encoding
				$wusalograw | Out-File -FilePath $logpath -Append -Encoding $encoding
			}
			Start-Sleep -Seconds 5
			Remove-Item -Path $wusatxt
		}
		
		$pb.Refresh()
		Start-Sleep -Seconds 2
	}
	#endregion
	
	#region Set Registry Properties
	$pblabel.Text = "Setting Registry Properties"
	$pblabel.Refresh()
	[Int]$percentage = 0
	$pb.Value = $percentage
	$pb.Refresh()
	
	"$(Get-TimeStamp) | Informa | Setting Registry Values" | Out-File -FilePath $logpath -Append -Encoding $encoding
	$newname = "Name - " + $patch
	$date = Get-TimeStamp
	
	try {
		New-ItemProperty -Path $reguninstall -Name DisplayName -Value $newname -PropertyType String -Force | Out-Null
		"$(Get-TimeStamp) | Success | Set DisplayName to $newname" | Out-File -FilePath $logpath -Append -Encoding $encoding
		[Int]$percentage = 16.6
		$pb.Value = $percentage
		$pb.Refresh()
	}
	catch {
		"$(Get-TimeStamp) | Failure | Could not set DisplayName to $newname" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	try {
		New-ItemProperty -Path $regroot -Name CurrentPatchLevel -Value $patch -PropertyType String -Force | Out-Null
		"$(Get-TimeStamp) | Success | Set CurrentPatchLevel to $patch" | Out-File -FilePath $logpath -Append -Encoding $encoding
		[Int]$percentage = 32.2
		$pb.Value = $percentage
		$pb.Refresh()
	}
	catch {
		"$(Get-TimeStamp) | Failure | Could not set CurrentPatchLevel to $patch" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	try {
		New-ItemProperty -Path $regroot -Name LastTimePatchesRan -Value $date -PropertyType String -Force | Out-Null
		"$(Get-TimeStamp) | Success | Set LastTimePatchesRan to $date" | Out-File -FilePath $logpath -Append -Encoding $encoding
		[Int]$percentage = 49.8
		$pb.Value = $percentage
		$pb.Refresh()
	}
	catch {
		"$(Get-TimeStamp) | Failure | Could not set LastTimePatchesRan to $date" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	
	try {
		New-ItemProperty -Path $reguninstall -Name VersionMajor -Value "2" -PropertyType DWORD -Force | Out-Null
		"$(Get-TimeStamp) | Success | Set VersionMajor to 2" | Out-File -FilePath $logpath -Append -Encoding $encoding
		[Int]$percentage = 49.8
		$pb.Value = $percentage
		$pb.Refresh()
	}
	catch {
		"$(Get-TimeStamp) | Failure | Could not set VersionMajor to 2" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	try {
		New-ItemProperty -Path $reguninstall -Name DisplayVersion -Value "2" -PropertyType String -Force | Out-Null
		"$(Get-TimeStamp) | Success | Set DisplayVersion to 2" | Out-File -FilePath $logpath -Append -Encoding $encoding
		[Int]$percentage = 49.8
		$pb.Value = $percentage
		$pb.Refresh()
	}
	catch {
		"$(Get-TimeStamp) | Failure | Could not set DisplayVersion to 2" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	#endregion
	
	#region Get Reboot Status
	$pblabel.Text = "Checking Reboot Flag"
	$pblabel.Refresh()
	$pb.Refresh()
	
	"$(Get-TimeStamp) | Informa | Checking Reboot Status" | Out-File -FilePath $logpath -Append -Encoding $encoding
	[Int]$percentage = 66.4
	$pb.Value = $percentage
	$pb.Refresh()
	
	$osuptime = Get-WmiObject Win32_OperatingSystem
	$uptime = (Get-Date) - ($osuptime.ConvertToDateTime($osuptime.lastbootuptime))
	$days = $uptime.Days
	
	[Int]$percentage = 83
	$pb.Value = $percentage
	$pb.Refresh()
	
	$reboot = Select-String -Path $logpath -Pattern "Reboot was required"
	if ($days -gt 30) {
		Reboot
	}
	elseif ($reboot -ne $null) {
		Reboot
	}
	else {
		"$(Get-TimeStamp) | Informa | Workstation does not need reboot" | Out-File -FilePath $logpath -Append -Encoding $encoding
	}
	#endregion
	
	#region Upload Log File
	$ftp = [System.Net.FtpWebRequest]::Create("ftp://domain/Public/Monthly Updates/Logs/$logfile")
	$ftp = [System.Net.FtpWebRequest]$ftp
	$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
	
	$content = [System.IO.File]::ReadAllBytes($logpath)
	$ftp.ContentLength = $content.Length
	$rs = $ftp.GetRequestStream()
	$rs.Write($content, 0, $content.Length)
	$rs.Close()
	$rs.Dispose()
	#endregion
	
	#region Remove Temp Files
	$pblabel.Text = "Removing Temp Files"
	$pblabel.Refresh()
	[Int]$percentage = 99.6
	$pb.Value = $percentage
	$pb.Refresh()
	$remove = $tempdir + "*.*"
	Remove-Item $remove
	#endregion
	
	#region Install Complete
	$pblabel.Text = "Install Complete"
	$pblabel.Refresh()
	[Int]$percentage = 100
	$pb.Value = $percentage
	$pb.Refresh()
	
	$installButton.Text = "Install Complete"
	$installButton.Enabled = $false
	$installButton.Refresh()
	
	[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
	[System.Windows.Forms.MessageBox]::Show("Updates were install.  This workstation does not need to reboot.", "Monthly Updates", 0)
	
	$form.close()
	#endregion
}
#endregion

#region Verify Install

#endregion

#region Build Form
# Start Form Build
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$icon = "C:\Program Files\Folder\Monthly Updates\imgs\logo_icon.ico"
$image = "C:\Program Files\Folder Utilities\Monthly Updates\imgs\logo_b.jpg"

# Form Layout Variables
$form = New-Object system.Windows.Forms.Form
$form.Text = "Name - $patch"
$form.Icon = $icon

$image = [system.drawing.image]::FromFile("$image")

$form.BackColor = "White"
$form.BackgroundImage = $image
$form.BackgroundImageLayout = "none"

$form.Width = 700
$form.Height = 400
$form.FormBorderStyle = "FixedDialog"

$form.MinimizeBox = $false
$form.MaximizeBox = $false
$form.WindowState = "Normal"

$form.StartPosition = "CenterScreen"

# Buttons
$installButton = New-Object System.Windows.Forms.Button

# Progress Bar
$pb = New-Object System.Windows.Forms.ProgressBar
$pb.Name = "PowerShellProgressBar"
$pb.Value = 0
$pb.Style = "Continuous"

$width = 500 - 40
$height = 20
$pb.Size = New-Object System.Drawing.Size($width, $height)
$pb.Left = 110
$pb.Top = 175

# Install Button
$installButton.Name = "installButton"
$installButton.Text = "Install Updates"
$installButton.Location = New-Object System.Drawing.Size(300, 200)
$installButton.Width = 100
$installButton.add_Click($install)

# Progress Bar Text
$pblabel = New-Object System.Windows.Forms.Label
$pblabel.Left = 110
$pblabel.Top = 150
$pblabel.Width = 500 - 20
$pblabel.Height = 15
$pblabel.Font = "Tahoma"

# Add Form Controls
$form.Controls.Add($pblabel)
$form.Controls.Add($pb)
$form.Controls.Add($installButton)

# Show Overall Form
$form.ShowDialog()
#endregion
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell Forms Progress Bar Not Working Correctly

Post by jvierra »

Hi monarch684.

Welcome to Sapien forums. Please attach your PSF as a file. Posting the text is not useful. The file attach controls are at the bottom of the edit form. Just edit your original post and remove the code and add the PSF file.
User avatar
monarch684
Posts: 3
Last visit: Tue Mar 06, 2018 1:36 pm

Re: Powershell Forms Progress Bar Not Working Correctly

Post by monarch684 »

I do not have a PSF file. Just a PS1 file.
MonthlyUpdates.ps1
(33.46 KiB) Downloaded 283 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell Forms Progress Bar Not Working Correctly

Post by jvierra »

I recommend building this as a PSF file. If all of your code is in the form you will find this easier to do. You are trying to mix two things which is making your life harder.

There is no way for us to run your code and it is a bit hard to understand what you are trying to do. As long as you are in an event the form will be frozen. You can try adding "DoEvents()" to the code to allow it to update while in the event.
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked