Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, August 4, 2016 3:57 PM
I have been creating a small GUI to deploy scripts to get a bit more familiar with Powershell. And I am having trouble with a part of the script.
I want to have the user updated as the script deploys to different computers in our AD. To do so I have created a form with a rich text box that opens when you send the deployment. As the deployment hits different computers I send the information to the text box. The trouble is, that I can not get the textbox in the form to update in real time. I have used some different methods including .Update(), .Refresh(), and .AppendText() without success.
I think that .Refresh() is what I need, because it repaints the entire form, but my implementation must be a bit off. Do I need to refresh the textbox instead of the form? Should I do both? Do I need to add sleep sections to the code so that it has time to execute?
Here are the relevant snippets from my code:
#This is the function that will write to the textbox ($DSTextbox)
Function _DeployScript
{
#GUI window that appears on deployment and uses $DSTextBox to update user
$DeploymentStatus.ShowDialog()
#Here I display num of computers identified
$DeployList = Get-Content $ComputerList
$num = $DeployList.Length
$DSTextBox.Text = "$num computers identified`n" #Textbox displaying the real time info
$DeploymentStatus.Refresh()
#I then create a log for the computers hit with deployment
{
***code to create a log
}
#Then I deploy the file
ForEach ($ComputerEntry in $DeployList)
{
$DSTextBox.Text = $DSTextBox.Text + "`nDeploying to $ComputerEntry ..."
$DeploymentStatus.Refresh()
{
***code to deploy to the computer
}
}
}
#Once the user has supplied information to the GUI they hit this button, which calls the function above
$FinishBtn.Add_Click({
_DeployScript
$DSTextBox.Text = $DSTextBox.Text + "`n`nDeployment Complete."
$DeploymentStatus.Refresh()
})
All replies (6)
Thursday, August 4, 2016 7:54 PM ✅Answered
You cannot access the form after you execute ShowDialog. Place you script inside the form and it can write to the textbox.
\(ツ)_/
Thursday, August 4, 2016 9:29 PM ✅Answered
Add-Type -AssemblyName System.Windows.Forms
$form1=New-Object System.Windows.Forms.Form
$form1.StartPosition='CenterScreen'
$fp = New-Object System.Windows.Forms.FlowLayoutPanel
$form1.Controls.Add($fp)
$fp.Dock = 'Fill'
$btn = New-Object System.Windows.Forms.Button
$btn.Text = 'Run Processes'
$fp.Controls.Add($btn)
$btn.add_Click({
$textbox1.Lines += 'Start'
for($i=1; $i -lt 5; $i++){
$textbox1.Lines+= 'Step ' + $i
$textbox1.ScrollToCaret()
[System.Windows.Forms.Application]::DoEvents()
sleep 2
}
$textbox1.Lines += 'Finished'
})
$textbox1 = New-Object System.Windows.Forms.TextBox
$textbox1.Width = $form1.Width
$textbox1.Multiline = $true
$textbox1.WIdth =
$fp.COntrols.Add($textbox1)
$form1.ShowDialog()
\(ツ)_/
Thursday, August 4, 2016 4:44 PM
I encourage you to visit the forums over at sapien and the blog posts, you'll find a lot more info in dealing with forms.
https://www.sapien.com/blog/2012/05/16/powershell-studio-creating-responsive-forms/
Dan
Thursday, August 4, 2016 6:19 PM
Thanks! That is actually a really cool site. I'll see what I can find there. Still looking for some pointers on this specific code though if anyone can look it over.
Thursday, August 4, 2016 7:56 PM
Got ya. So the function execution needs to be embedded in the form itself. Cool. I'll try that out.
Tuesday, August 9, 2016 4:49 PM
This also works! Thanks!