Progress Bar for a Function

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 6 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
dank42
Posts: 61
Last visit: Tue Apr 26, 2022 7:49 am

Progress Bar for a Function

Post by dank42 »

Hi,

I have a function called "Send-Email" which generates a htm file which is then sent out via email using the Send-MailMessage cmdlet

The htm file is created using the Add-Content cmdlet

Code: Select all

function Send-Email
{
	
	$date = Get-Date -Format f
	
	$agent = Get-ADUser -Identity $CurrentUser -Properties DisplayName | Select-Object DisplayName -ExpandProperty DisplayName
	
	$UserDescription = Get-ADUser $ADUser -Properties Description | Select-Object Description -ExpandProperty Description
	
	$Report = New-Item -Path ("$logs\$ADUser.htm") -Type File
	
	#Builds the headers and formatting for the report
	Add-Content $report "<html>"
	Add-Content $report "<head>"
	Add-Content $report "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
	Add-Content $report '<title>Disabled User Alert</title>'
	Add-content $report '<STYLE TYPE="text/css">'
	Add-content $report  "<!--"
	Add-content $report  "td {"
	Add-content $report  "font-family: Verdana;"
	Add-content $report  "font-size: 11px;"
	Add-content $report  "border-top: 1px solid #999999;"
	Add-content $report  "border-right: 1px solid #999999;"
	Add-content $report  "border-bottom: 1px solid #999999;"
	Add-content $report  "border-left: 1px solid #999999;"
	Add-content $report  "pAdding-top: 0px;"
	Add-content $report  "pAdding-right: 0px;"
	Add-content $report  "pAdding-bottom: 0px;"
	Add-content $report  "pAdding-left: 0px;"
	Add-content $report  "}"
	Add-content $report  "body {"
	Add-content $report  "margin-left: 5px;"
	Add-content $report  "margin-top: 5px;"
	Add-content $report  "margin-right: 0px;"
	Add-content $report  "margin-bottom: 10px;"
	Add-content $report  ""
	Add-content $report  "table {"
	Add-content $report  "border: thin solid #000000;"
	Add-content $report  "}"
	Add-content $report  "-->"
	Add-content $report  "</style>"
	Add-Content $report "</head>"
	Add-Content $report "<body>"
	
	$date.ToString()
	
	Add-content $report  "<table width='100%'>"
	Add-content $report  "<tr bgcolor='#CCCCCC'>"
	Add-content $report  "<td colspan='7' height='25' align='center'>"
	Add-content $report  "<font face='Verdana' color='#41B9B9' size='4'><strong>The following user has just been disabled by $agent</strong></font>"
	Add-content $report  "</td>"
	Add-content $report  "</tr>"
	Add-content $report  "</table>"
	Add-content $report  "<table width='100%'>"
	Add-Content $report "<tr bgcolor=#CCCCCC>"
	Add-Content $report  "<td width='20%' align='left'>Account Name</td>"
	Add-Content $report "<td width='10%' align='center'>Disabled Date</td>"
	Add-Content $report "<td width='50%' align='center'>Description</td>"
	Add-Content $report "</tr>"
	
	Add-Content $report "<tr>"
	Add-Content $report "<td>$ADUser</td>"
	Add-Content $report "<td>$date</td>"
	Add-Content $report "<td>$UserDescription</td>"
	
	
	
	Add-Content $report "</body>"
	Add-Content $report "</html>"
	
	#Assembles and sends alert
	$emailFrom = "<emailaddress>"
	$emailTo = "<emailaddress>" 
	#$cc = "<emailaddress>"
	$subject = "Disabled User Alert"
	$smtpServer = "server"
	$body = Get-Content $report | Out-String
	
	
	Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -BodyAsHtml -Body $body -SmtpServer $smtpServer
When this function starts there's a few seconds delay...I would like a way of showing that something is happening during this period, is it possible using a ProgressBar against the function "Send-Email"?

Cheers
User avatar
dank42
Posts: 61
Last visit: Tue Apr 26, 2022 7:49 am

Re: Progress Bar for a Function

Post by dank42 »

Hi,

This helped me out:

https://www.sapien.com/blog/2011/07/14/ ... r-control/

I used:

Code: Select all

    $progressbar1.Maximum = 4
	$progressbar1.Step = 1
	$progressbar1.Value = 0
And added $progressbar1.PerformStep() between the add-content section 4 times

It does the trick.

Thanks
This topic is 6 years and 6 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