I've been looking around for the past half hour on how to show the text inside of a GUI ProgressBar in Powershell, and everything I've tried has failed. I've even been referencing MSoft docs on it.
Am I doing something wrong? How do I add in the text?
This isn't my full script or exactly how I'll be using it - I just made an example so I could try to get it working.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Size = '500,300'
$form.StartPosition = 'CenterScreen'
$form.Topmost = $true
$computerList = 'server01', 'server02', 'server03', 'server04', 'server05', 'server06', 'server07', 'server08', 'server09', 'server10'
$progressbar1 = New-Object System.Windows.Forms.ProgressBar
$progressbar1.Size = '300, 20'
$progressbar1.Location = '20,60'
$progressbar1.Text = "Processing..."
$progressbar1.Maximum = $computerList.Count
$progressbar1.Step = 1
$progressbar1.Value = 0
foreach ($computer in $computerList){
$progressbar1.PerformStep()
}
$form.Controls.Add($progressbar1)
$form.ShowDialog()
I guess the easiest way is to have a Label control above the progressbar and update the text in there:
$progressLabel = New-Object System.Windows.Forms.Label
$progressLabel.Size = '300, 20'
$progressLabel.Location = '20,40'
$progressLabel.Text = "Processing..."
$form.Controls.Add($progressLabel)
foreach ($computer in $computerList){
$progressLabel.Text = "Doing stuff on computer '$computer'.."
$progressbar1.PerformStep()
# perform your action on $computer here
}
Related
I am currently working on an auto installer for newly configured computers where I work. It has a GUI which allows you to choose which programs will be installed. When the file paths are correct the GUI is blank but when I purposefully mess up the file paths the checkboxes will pop back up.
Sorry if this seems like a very dumb question. I am extremely new to this and have gotten a lot of help from various different websites. This is basically me working off of another auto installer script that I found on this website.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(600,700)
$Form.text ="Software Installer"
############################################## Start group boxes
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size (10,20)
$groupBox.text = "Available Software to Install:"
$Form.Controls.Add($groupBox)
$Checkboxes += New-Object System.Windows.Forms.CheckBox
$Checkboxes = New-Object System.Drawing.Size (10,20)
$Button1 = New-Object System.Windows.Forms.Button
$Button1.text = "Install"
$Button1.width = 100
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(245,585)
$Button1.Font = 'Microsoft Sans Serif,10'
$apps = [PSCustomObject]#{
Name = ''
Path = ''
PreTransfer = ''
}
$apps = Import-Csv -Path C:\Users\Zach\Desktop\Installers\appslist.csv
$groupBox.Controls.Add($Button1)
$Checkboxes = #()
$y = 20
foreach ($a in $apps)
{
$Checkbox = New-Object System.Windows.Forms.CheckBox
$Checkbox.name = $a.Name
$Checkbox.Text = $a.Name
$Checkbox.Location = New-Object System.Drawing.Size(10,$y)
$y += 30
$groupBox.Controls.Add($Checkbox)
$Checkboxes += $Checkbox
}
$groupbox.size = New-Object System.Drawing.Size(564,(624*$checkboxes.Count))
#to look at each box
$Button1.Add_Click({
foreach ($i in $Checkboxes){
if ($i.checked -eq $true) {
Start-Process $i.AccessibleDescription
}
}
})
$form.ShowDialog()| Out-Null
After months of "scripting" I finally got my script working as I need it to, except it only does what I want when running it from ISE. When I start it using powershell.exe it throws a fit something about unable to find [system.windows.forms. "dialogresult]".
I have attached the relevant portion of the script, TYIA
$cred = Get-Credential
$Job = Start-Job -ScriptBlock {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Admin Tools’
$form.Size = New-Object System.Drawing.Size(900,600)
$form.StartPosition = 'CenterScreen'
$form.AutoSize = $true
$form.MaximizeBox = $false
$form.FormBorderStyle = 'FixedSingle'
$img = [System.Drawing.Image]::Fromfile("c:\users\$env:username\Pictures\logo.png")
$pictureBox = New-Object Windows.Forms.PictureBox
$pictureBox.Width = $img.Size.Width
$pictureBox.Height = $img.Size.Height
$pictureBox.Location = New-Object System.Drawing.Size(600,465)
$pictureBox.Image = $img
$form.controls.add($pictureBox)
$ADUCButton = New-Object System.Windows.Forms.Button
$ADUCButton.Location = New-Object System.Drawing.Point(10,25)
$ADUCButton.Size = New-Object System.Drawing.Size(300,100)
$ADUCButton.Font = New-Object System.Drawing.Font(“Times New Roman”,14, [System.Drawing.Fontstyle]::Bold)
$ADUCButton.Text = ' Active Directory Users and Computers '
$ADUCButton.Add_Click({Start-Process -filepath 'c:\windows\system32\cmd.exe' -WindowStyle maximized})
$ADUCButton.FlatAppearance.BorderColor = [System.Drawing.Color]::DarkBlue
$ADUCButton.BackColor = [System.Drawing.Color]::CornflowerBlue
$form.Controls.Add($ADUCButton)
$label = New-Object System.Windows.Forms.Label
$label.location = New-Object System.Drawing.Point(100,500)
$label.Size = New-Object System.Drawing.Size(280,70)
$label.Font = New-Object System.Drawing.Font("Lucida Console",8,
[System.Drawing.FontStyle]::Italic)
$label.Text = 'Created by a PowerShell Novice'
$form.Controls.Add($label)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(850,300)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Close'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox.SelectedItems
$x
}
} -Credential $cred
Recieve-Job $job
Any assistance you can provide is greatly appreciated.
When you are in the ISE it will autoload modules needed to do many things, the consoelhost does not.
If you have form code in your scripts, you must at needed resources, for it to properly run in the consolehost.
Put this at the top of your script. Here is a snippet I keep in for functions
# Initialize GUI resources
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName presentationframework
Add-type -AssemblyName microsoft.VisualBasic
[System.Windows.Forms.Application]::EnableVisualStyles()
Add-Type -AssemblyName System.Drawing
# Required for use with web SSL sites
[Net.ServicePointManager]::
SecurityProtocol = [Net.ServicePointManager]::
SecurityProtocol -bor
[Net.SecurityProtocolType]::
Tls12
You don't need them all, depending on what you are doing or plan to. At the minimum, you need this...
Add-Type -AssemblyName System.Windows.Forms
BTW, this is probably a posting error, but this line is not syntactically correct.
if ($result -eq [System.Windows.Forms.DialogResult}::OK)
It should be this...
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
Open/Close bracket type must match
I am using a Powershell form to output some data and I am wondering how I can get the output in color?
I am not using write-host. That is not what I am looking for. I know you can use -ForegroundColor for that.
It's for Get-ADUser -Filter "UserPrincipalName -like 'Username'" | Select Enabled
If output is False it needs to be in Red. If output is true just regular color.
Anyone who can help me?
Many thanks.
Ralph.
A follow-up to my comment
#region Begin functions and code behind
function RunCode {
$ProcessList = (Get-Process).Name
If ($ProcessList -ge 10)
{$DataSet.ForeColor = 'red'}
else {$DataSet.ForeColor = 'black'}
[void] $DataSet.Items.Addrange($ProcessList)
}
#endregion End functions and code behind
#region Begin GUI code
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '511,501'
$Form.text = "Form"
$Form.TopMost = $false
$RunCode = New-Object system.Windows.Forms.Button
$RunCode.text = "RunCode"
$RunCode.width = 90
$RunCode.height = 30
$RunCode.location = New-Object System.Drawing.Point(19,17)
$RunCode.Font = 'Microsoft Sans Serif,10'
$DataSet = New-Object system.Windows.Forms.ListBox
$DataSet.text = "listBox"
$DataSet.width = 204
$DataSet.height = 144
$DataSet.location = New-Object System.Drawing.Point(17,98)
$Form.controls.AddRange(#(
$RunCode,
$DataSet
))
$RunCode.Add_Click({ RunCode })
#endregion Begin GUI code
# Call the GUI
[void]$Form.ShowDialog()
I want to show the picturebox, but it does not show once I use form.show(). But if I change to Form.showdialog, the picturebox will show but, the process can not continue until I close the GUI. The picture box show but it does not moving, it stuck like picture.
Function Handling
{
$Form.Close()
$Form.Dispose()
$Form = New-Object system.Windows.Forms.Form
$Form.ControlBox = $true
$Form.BackColor = "#d0021b"
$Form.WindowState = "Maximized"
$Form.TopMost = $false
[void]$Form.Show()
# Message Box
[System.Windows.MessageBox]::Show("OK", "[Error]", "0", "Error")
$ExitCode = "1"
if($ExitCode -ne "107A")
{
$Form.Close()
$Form.Dispose()
Exit
}
else{
$Form.Close()
$Form.Dispose()
Exit
}
}
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$form.BackgroundImageLayout = 'Center'
$Form.WindowState = 'Maximized'
$Form.BackColor = "#ffffff"
$file2 = Get-ChildItem -Path "D:\3.png"
$cover = [Drawing.Image]::FromFile($file2)
$form.BackgroundImage = $img2
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file = (get-item 'D:\6.gif')
$img = [System.Drawing.Image]::Fromfile($file)
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Image = $img
$pictureBox.SizeMode = "Autosize"
$pictureBox.Anchor = "Bottom, left"
$Form.controls.add($pictureBox)
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file3 = (get-item 'D:\6.gif')
$img3 = [System.Drawing.Image]::Fromfile($file3)
$pictureBox2 = new-object Windows.Forms.PictureBox
$pictureBox2.Image = $img3
$pictureBox2.SizeMode = "Autosize"
$pictureBox2.Anchor = "Bottom, right"
$Form.controls.add($pictureBox2)
$form.Show()
Write-Host "next process"
####
# some process
###
Start-Sleep -s 2
Handling
Anyone can give me idea please. Really appreciate for your help. Thank you.
Can you try like this? Put your background operation in place of Start-Sleep -s 2 and the gif file still show moving.
$Form = New-Object system.Windows.Forms.Form
$Form.Location= New-Object System.Drawing.Size(100,100)
$Form.Size= New-Object System.Drawing.Size(550,170)
$Form.StartPosition = "Manual"
$Form.Visible=$false
$Form.Enabled = $true
$Form.Add_Shown({$Form.Activate()})
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file = (get-item 'D:\6.gif')
$img = [System.Drawing.Image]::Fromfile($file);
[System.Windows.Forms.Application]::EnableVisualStyles();
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Location = New-Object System.Drawing.Size(0,1)
$pictureBox.Size = New-Object System.Drawing.Size($img.Width,$img.Height)
$pictureBox.Image = $img
$Form.controls.add($pictureBox)
$WaitForm.Topmost = $True
$rs = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()
$rs.Open()
$rs.SessionStateProxy.SetVariable("Form", $Form)
$data = [hashtable]::Synchronized(#{text=""})
$rs.SessionStateProxy.SetVariable("data", $data)
$p = $rs.CreatePipeline({ [void] $Form.ShowDialog()})
$p.Input.Close()
$p.InvokeAsync()
## Enter the rest of your script here while you want the form to display
Start-Sleep -s 2
$WaitForm.close()
I have created a function that creates a specific UI element with text input forms. This form also has three buttons:
One is supposed to display the text inputs again to do the same function again.
Another is supposed to finish and close the UI, passing the text input into the variables.
The last is supposed to cancel the UI element, doing nothing with anything in the text input forms.
Now I know the loop in the code isn't really complete, but I am having issues with it even performing the loop as well as passing the text forms into the variables. I know its something I am doing but it seems correct to me when I look at it.
Changed from an if loop, a while loop, and now a do/while loop.
Changed the position of the variables between the do section into the while section. Same for the if and while loops.
do {
ChangeDesc
}
while ($result -eq [System.Windows.Forms.DialogResult]::Retry)
{
$PC = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $HNInputBox.Text
$PC.Description = $DescInputBox.Text
$PC.Put()
}
ChangeDesc is the name of the function and works just as intended.
Expected to work is to loop the function 'ChangeDesc', and then when the 'Retry' or 'Ok' button is pressed, pass those forms to the variables as shown.
Currently, it will display the form, and when the 'Retry' button is pressed, the forms are passed properly and then the UI is closed out, the 'Ok' button does not pass any input and the 'Cancel' does the same thing.
Below is the rest of my lines of code for clarification.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
function ChangeDesc {
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,210)
$form.StartPosition = 'CenterScreen'
$AnotherButton = New-Object System.Windows.Forms.Button
$AnotherButton.Location = New-Object System.Drawing.Point(15,130)
$AnotherButton.Size = New-Object System.Drawing.Size(75,23)
$AnotherButton.Text = 'Another?'
$AnotherButton.DialogResult = [System.Windows.Forms.DialogResult]::Retry
$form.AcceptButton = $AnotherButton
$form.Controls.Add($AnotherButton)
$FinishedButton = New-Object System.Windows.Forms.Button
$FinishedButton.Location = New-Object System.Drawing.Point(100,130)
$FinishedButton.Size = New-Object System.Drawing.Size(75,23)
$FinishedButton.Text = 'Finished'
$FinishedButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.CancelButton = $FinishedButton
$form.Controls.Add($FinishedButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(185,130)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$HNLabel = New-Object System.Windows.Forms.Label
$HNLabel.Location = New-Object System.Drawing.Point(10,20)
$HNLabel.Size = New-Object System.Drawing.Size(280,20)
$HNLabel.Text = 'Enter Host Name:'
$form.Controls.Add($HNLabel)
$HNInputBox = New-Object System.Windows.Forms.TextBox
$HNInputBox.Location = New-Object System.Drawing.Point(10,40)
$HNInputBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($HNInputBox)
$DescLabel = New-Object System.Windows.Forms.Label
$DescLabel.Location = New-Object System.Drawing.Point(10,70)
$DescLabel.Size = New-Object System.Drawing.Size(280,20)
$DescLabel.Text = 'Enter Description:'
$form.Controls.Add($DescLabel)
$DescInputBox = New-Object System.Windows.Forms.TextBox
$DescInputBox.Location = New-Object System.Drawing.Point(10,90)
$DescInputBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($DescInputBox)
$form.Topmost = $true
$form.Add_Shown({$HNInputBox.Select()})
$result = $form.ShowDialog()
}
Your form is already closed when the loop terminates, and the variables you're trying to use are local to your function. Assign the values you're trying to use to script- or global-scope variables at the end of the function, and the code should do what you expect:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
function ChangeDesc {
$form = New-Object Windows.Forms.Form
...
$script:result = $form.ShowDialog()
$script:hostname = $HNInputBox.Text
$script:description = $DescInputBox.Text
}
do {
ChangeDesc
} while ($script:result -eq [Windows.Forms.DialogResult]::Retry)
$PC = Get-WmiObject Win32_OperatingSystem -Computer $script:hostname
$PC.Description = $script:description
$PC.Put()