I'm using a PowerShell form to prompt users to close down Adobe Reader so it can be upgraded. It's executed via SCCM, so runs in the System context. It works if the user is logged on and the session is active, but if the screen is locked, the form does not display when I unlock the screen, although the script is running.
I originally used $Form.ShowDialog() to display it but saw some other posts that said it wasn't reliable, so I switched to [void][System.Windows.Forms.Application]::Run($Form) but it still doesn't work. Can anyone help me fix this please?
Below is the trimmed-down script with only the code relevant to rendering the form.
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
$Font = New-Object System.Drawing.Font('Segoe UI Light',14,[System.Drawing.FontStyle]::Regular)
$FontBold = New-Object System.Drawing.Font('Segoe UI',13,[System.Drawing.FontStyle]::Bold)
Function EndForm
{
$Timer.Stop();
$Timer.Dispose();
$Label.Dispose();
$CountdownLabel.Dispose();
$ProgressBar.Dispose();
$OKButton.Dispose();
$Form.Close();
$Form.Dispose();
}
$OnOKClick =
{
$MsgBoxInput = [System.Windows.Forms.MessageBox]::Show("Are you sure you want to close and uninstall Adobe Reader now?","Confirm Adobe Reader Removal","YesNo","Warning")
If ($MsgBoxInput -eq "Yes") {EndForm}
}
$Script:Countdown = 3600
$OKToolTip = "Click OK to close and uninstall Adobe Reader now"
$CountdownSubtitle = "Adobe Reader will close and uninstall in"
$Label = New-Object System.Windows.Forms.Label
$Label.AutoSize = $False
$Label.Height = 120
$Label.Width = 630
$Label.Font = $Font
$Label.BackColor = "White"
$Label.ForeColor = "#4D4C5C"
$Label.Location = New-Object System.Drawing.Size(52,180)
$Label.Text = "Your computer will soon be upgraded to newer versions of Windows 10 and Office 365. In order to succeed, the upgrade process requires that Adobe Reader be closed and uninstalled first. It will not be available for use again until after the upgrade has completed."
$CountdownLabel = New-Object System.Windows.Forms.Label
$CountdownLabel.AutoSize = $False
$CountdownLabel.Height = 40
$CountdownLabel.Width = 530
$CountdownLabel.Font = $Font
$CountdownLabel.BackColor = "White"
$CountdownLabel.ForeColor = "#4D4C5C"
$CountdownLabel.Location = New-Object System.Drawing.Size(120,380)
$ProgressBar = New-Object System.Windows.Forms.ProgressBar
$ProgressBar.Location = New-Object System.Drawing.Size(52,340)
$ProgressBar.Size = New-Object System.Drawing.Size(635,35)
$ProgressBar.Style = "Continuous"
$ProgressBar.Maximum = $Script:Countdown
$ProgressBar.Minimum = 0
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Name = "OK_Button"
$OKButton.Size = New-Object System.Drawing.Size(80,40)
$OKButton.Location = New-Object System.Drawing.Size(330,450)
$OKButton.UseVisualStyleBackColor = $True
$OKButton.Text = "OK"
$OKButton.DataBindings.DefaultDataSourceUpdateMode = 0
$OKButton.Add_Click($OnOKClick)
$ToolTip = New-Object System.Windows.Forms.ToolTip
$ToolTip.IsBalloon = $False
$ToolTip.InitialDelay = 100
$ToolTip.ReshowDelay = 200
$ToolTip.SetToolTip($OKButton,$OKToolTip)
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Contoso LLC"
$Form.Font = $Font
$Form.Width = 770
$Form.Height = 580
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.ControlBox = $False
$Form.ShowIcon = $False
$Form.WindowState = "Normal"
$Form.FormBorderStyle = "Fixed3D"
$Form.ShowInTaskbar = $True
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
$Form.Controls.Add($ProgressBar)
$Form.Controls.Add($Label)
$Form.Controls.Add($CountdownLabel)
$Form.Controls.Add($OKButton)
$Timer = New-Object System.Windows.Forms.Timer
$Timer.Interval = 1000
$Timer.Add_Tick(
{
If ($Script:Countdown -eq 0) {EndForm}
$ProgressBar.Value = $Script:Countdown
$TimeRemain = New-Timespan -Seconds $Script:Countdown
$HrsRemain = $TimeRemain.Hours
$MinsRemain = $TimeRemain.Minutes
$SecsRemain = $TimeRemain.Seconds
If ($HrsRemain -ge 2) {$HrsText = "$HrsRemain hours"}
If ($HrsRemain -eq 1) {$HrsText = "$HrsRemain hour"}
If ($MinsRemain -ge 2) {$MinsText = "$MinsRemain minutes"}
If ($MinsRemain -eq 1) {$MinsText = "$MinsRemain minute"}
If ($SecsRemain -ge 2) {$SecsText = "$SecsRemain seconds"}
If ($SecsRemain -eq 1) {$SecsText = "$SecsRemain second"}
$CountdownLabel.Text = "$CountdownSubtitle $HrsText $MinsText $SecsText"
If ($HrsRemain -ge 1 -And $MinsRemain -eq 0) {$CountdownLabel.Text = "$CountdownSubtitle $HrsText $SecsText"}
If ($HrsRemain -eq 0) {$CountdownLabel.Text = "$CountdownSubtitle $MinsText $SecsText"}
If ($HrsRemain -eq 0 -And $MinsRemain -eq 0) {$CountdownLabel.Text = "$CountdownSubtitle $SecsText"}
$Script:Countdown--
})
$Timer.Start()
[void][System.Windows.Forms.Application]::Run($Form)
I have decided to 'work around' this issue by detecting for the presence of the logonui process, which means the screen is locked. In that case, I will just close Adobe Reader anyway.
Related
I want to monitor the date and time of a file. I wrote the code that do the job as I want but I can't reposition the gui window. I tried all I could find like "start-job" or create a new runspace but I don't get any results in richtextbox. Any suggestion is welcome.
$targetFile = "full path"
# Function - Add Text to RichTextBox
function Add-RichTextBox{
[CmdletBinding()]
param ($text)
#$richtextbox_output.Text += "`tCOMPUTERNAME: $ComputerName`n"
$richtext.Text += "$text"
$richtext.Text += "`n"
}
# Windows Form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Monitor script"
$form.Size = New-Object System.Drawing.Size(400,300)
$form.StartPosition = 'CenterScreen'
$Font = New-Object System.Drawing.Font("Tahoma",11)
$Form.Font = $Font
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(20,40)
$label.Size = New-Object System.Drawing.Size(200,20)
$label.Text = (Get-Date).ToString()
$form.Controls.Add($label)
$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Point(150,220)
$StartButton.Size = New-Object System.Drawing.Size(100,33)
$StartButton.Text = 'Start'
#$StartButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $StartButton
$form.Controls.Add($StartButton)
$StartButton.Add_click({
while($true){
$lastdate = Get-ChildItem $targetFile
$Date = $lastdate.LastAccessTime.ToString()
Add-RichTextBox "$date"
$richtext.SelectionStart = $richtext.TextLength
$richText.ScrollToCaret()
#$richtext.refresh()
#$form.refresh()
Start-sleep 30
}
})
## the Rich text box
$richtext = new-object System.Windows.Forms.RichTextBox
$richtext.Location = New-Object System.Drawing.Point(20,60)
$richtext.multiline = $true
$richtext.Name = "Results"
$richtext.text = "Results:`n"
$richtext.scrollbars = "Both"
$richtext.Height = 120
$richtext.width = 350
$richtext.font = new-object system.drawing.font "Lucida Console",10
$Form.controls.add($richtext)
$Form.Add_Shown({$Form.Activate()})
$form.ShowDialog()
Continuing from my comment to use a Timer on your form (if you absolutely do not want a FileSystemWatcher), here's how you can do that:
$targetFile = "D:\Test\blah.txt"
$lastAccessed = (Get-Date) # a variable to keep track of the last LastAccessTime of the file
# Windows Form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Monitor script"
$form.Size = New-Object System.Drawing.Size(400,300)
$form.StartPosition = 'CenterScreen'
$Font = New-Object System.Drawing.Font("Tahoma",11)
$form.Font = $Font
# Label
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(20,40)
$label.Size = New-Object System.Drawing.Size(200,20)
$label.Text = (Get-Date).ToString()
$form.Controls.Add($label)
# Button
$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Point(150,200)
$StartButton.Size = New-Object System.Drawing.Size(100,33)
$StartButton.Text = 'Start'
$form.Controls.Add($StartButton)
$StartButton.Add_click({
$timer.Enabled = $true
$timer.Start()
})
# RichTextBox
$richtext = New-Object System.Windows.Forms.RichTextBox
$richtext.Location = New-Object System.Drawing.Point(20,60)
$richtext.Multiline = $true
$richtext.Name = "Results"
$richtext.Text = "Results:`r`n"
$richtext.ScrollBars = "Both"
$richtext.Height = 120
$richtext.Width = 350
$richtext.Font = New-Object System.Drawing.Font "Lucida Console",10
$form.Controls.Add($richtext)
# Timer
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 30000 # 30 seconds
$timer.Enabled = $false # disabled at first
$timer.Add_Tick({
$file = Get-Item -Path $targetFile -ErrorAction SilentlyContinue
# if we can find the file and its last AccessedTime is not
# the same as we already stored in variable $lastAccessed
# use script-scoping here, so $script:lastAccessed instead of $lastAccessed
if ($file -and $file.LastAccessTime -gt $script:lastAccessed) {
$richtext.AppendText("$($file.LastAccessTime.ToString())`r`n")
$script:lastAccessed = $file.LastAccessTime # remember this new datetime
}
})
$form.ShowDialog()
# Important: Clean up
$timer.Stop()
$timer.Dispose()
$richtext.Dispose()
$form.Dispose()
I'm creating a script for silent installation of some applications, with a choice box!
I also created a custom button, but I need help to create the button logic.
I thank everyone!
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object System.Windows.Forms.Form
$Form.Text ='Softwares Padrão'
$Form.Width = 500
$Form.Height = 300
$Form.AutoSize = $true
$Painel = New-Object System.Windows.Forms.Panel
$Painel.Height = 200
$Painel.Width = 154
$Painel.Location = New-Object System.Drawing.Point(24,45)
$Painel.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")
$CheckBox_Selec = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec.Text = "Selecionar Todos"
$CheckBox_Selec.Width = 200
$CheckBox_Selec.Height = 20
$CheckBox_Selec.AutoSize = $true
$CheckBox_Selec.Location = New-Object System.Drawing.Size(34, 20)
$CheckBox_Selec.Checked = $false
$Form.Controls.Add($CheckBox_Selec)
$CheckBox_Selec1 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec1.Text = "Winrar"
$CheckBox_Selec1.AutoSize = $true
$CheckBox_Selec1.Location = New-Object System.Drawing.Size(10, 10)
$CheckBox_Selec1.Checked = $false
$Form.Controls.Add($CheckBox_Selec1)
$CheckBox_Selec2 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec2.Text = "Java"
$CheckBox_Selec2.AutoSize = $true
$CheckBox_Selec2.Location = New-Object System.Drawing.Size(10, 30)
$CheckBox_Selec2.Checked = $false
$Form.Controls.Add($CheckBox_Selec2)
$CheckBox_Selec3 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec3.Text = "Adobe Reader"
$CheckBox_Selec3.AutoSize = $true
$CheckBox_Selec3.Location = New-Object System.Drawing.Size(10, 50)
$CheckBox_Selec3.Checked = $false
$Form.Controls.Add($CheckBox_Selec3)
$CheckBox_Selec4 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec4.Text = "Office 2013x32"
$CheckBox_Selec4.AutoSize = $true
$CheckBox_Selec4.Location = New-Object System.Drawing.Size(10, 70)
$CheckBox_Selec4.Checked = $false
$Form.Controls.Add($CheckBox_Selec4)
$CheckBox_Selec5 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec5.Text = "Google Chrome"
$CheckBox_Selec5.AutoSize = $true
$CheckBox_Selec5.Location = New-Object System.Drawing.Size(10, 90)
$CheckBox_Selec5.Checked = $false
$Form.Controls.Add($CheckBox_Selec5)
$CheckBox_Selec6 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec6.Text = "CutePdf"
$CheckBox_Selec6.AutoSize = $true
$CheckBox_Selec6.Location = New-Object System.Drawing.Size(10, 110)
$CheckBox_Selec6.Checked = $false
$Form.Controls.Add($CheckBox_Selec6)
$CheckBox_Selec7 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec7.Text = "Oracle11gx32"
$CheckBox_Selec7.AutoSize = $true
$CheckBox_Selec7.Location = New-Object System.Drawing.Size(10, 130)
$CheckBox_Selec7.Checked = $false
$Form.Controls.Add($CheckBox_Selec7)
$CheckBox_Selec8 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec8.Text = "Piramide"
$CheckBox_Selec8.AutoSize = $true
$CheckBox_Selec8.Location = New-Object System.Drawing.Size(10, 150)
$CheckBox_Selec8.Checked = $false
$Form.Controls.Add($CheckBox_Selec8)
$CheckBox_Selec9 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec9.Text = "Rh3"
$CheckBox_Selec9.AutoSize = $true
$CheckBox_Selec9.Location = New-Object System.Drawing.Size(10, 170)
$CheckBox_Selec9.Checked = $false
$Form.Controls.Add($CheckBox_Selec9)
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Text = "Instalar"
$Button1.AutoSize = $true
$Button1.Width = 60
$Button1.Height = 30
$Button1.Location = New-Object System.Drawing.Point(290,215)
$Button1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$Button1.Enabled = $false
$Form.Controls.Add($Button1)
$Form.controls.AddRange(#($Painel,$CheckBox_Selec,$Button1))
$Painel.controls.AddRange(#($CheckBox_Selec1,$CheckBox_Selec2,$CheckBox_Selec3,$CheckBox_Selec4,$CheckBox_Selec5,$CheckBox_Selec6,$CheckBox_Selec7,$CheckBox_Selec8,$CheckBox_Selec9))
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec1.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec2.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec3.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec4.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec5.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec6.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec7.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec8.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec9.Checked = $CheckBox_Selec.Checked})
Function Test-AnyButtonCheked {
if (
$CheckBox_Selec.Checked -or
$CheckBox_Selec1.Checked -or
$CheckBox_Selec2.Checked -or
$CheckBox_Selec3.Checked -or
$CheckBox_Selec4.Checked -or
$CheckBox_Selec5.Checked -or
$CheckBox_Selec6.Checked -or
$CheckBox_Selec7.Checked -or
$CheckBox_Selec8.Checked -or
$CheckBox_Selec9.Checked
) {
$Button1.Enabled = $true
}
else {
$Button1.Enabled = $false
}
}
$CheckBox_Selec.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec1.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec2.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec3.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec4.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec5.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec6.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec7.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec8.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec9.Add_CheckedChanged( { Test-AnyButtonCheked})
[void] $Form.ShowDialog()
This is my structure, I know it's not in the best practices but it's running very well!
I would like to install the applications according to the checkbox chosen by the user!
I would like to configure the button so that if the user selects, "install all" he will install all applications or just the chosen checkbox.
Thank you for your help!
I have been stuck in this for whole day. I am creating a UI using PowerShell forms. In that:
user select an option from 1st combobox. click button Go
Based on the selection, a panel will appear having another combobox.
if user select another option in 1st combobox then another panel appears with another combobox
After selecting options from panel comboboxes, user clicks on start button.
This leads to a function which stores the selected options to a variable.
Problem
Now when user selects the options from the comboboxes of panels, I am using $combobox.selecteditem.Tostring to get the values.
But it gives me NULL result.
Here is my code..
$global:Button1Clicked = 0;
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.FormClosingEventHandler]
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1500,800'
$Form.text = "NextUI "
$Form.BackColor = 'White'
$Form.TopMost = $true
$Form.add_closing(
{
if($global:Button2Clicked)
{
$global:Button2Clicked = 0;
$_.cancel = $true;
}
})
$panel1 = New-Object system.Windows.Forms.Panel
$panel1.AutoSize = $true
$panel1.Width = 1200
$panel1.Height = 200
$panel1.location = New-Object System.Drawing.Point(50,100)
$panel1.Visible = $false
$panel1.Controls.Add($label3)
$panel1.Controls.Add($comboBox2)
$panel1.BorderStyle = 1
$panel2 = New-Object system.Windows.Forms.Panel
$panel2.AutoSize = $true
$panel2.Width = 1200
$panel2.Height =200
$panel2.location = New-Object system.Drawing.Point(50,100)
$panel2.Visible = $false
$panel2.Controls.Add($label4)
$panel2.Controls.Add($ComboBox3)
$panel2.BorderStyle = 1
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Select Module"
$Label1.AutoSize = $true
$Label1.location = New-Object System.Drawing.Point(35,50)
$Label1.Font = 'segoe ui,9.5'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "SharePoint Settings"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(35,15)
$Label2.Font = 'Segoe UI Semibold,9.5'
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Choose file and folder permission option"
$Label3.AutoSize = $true
$Label3.location = New-Object System.Drawing.Point(100,200)
$Label3.Font = 'segoe ui,9.5'
$Label4 = New-Object system.Windows.Forms.Label
$Label4.AutoSize = $True
$Label4.text = "File Permission"
$Label4.location = New-Object System.Drawing.Point(100,200)
$Label4.Font = 'Segoe UI ,9.5'
################ Module combo box ################
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = "Select Module"
$ComboBox1.width = 200
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(310, 45)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$combobox1.items.Add("ControlSettings")
$combobox1.items.Add("NextSettings")
######## file and folder permission #############
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = "select an option"
$ComboBox2.width = 200
$ComboBox2.height = 20
$ComboBox2.location = New-Object System.Drawing.Point(450, 200)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$ComboBox2.items.Add("View")
$ComboBox2.items.Add("Edit")
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = "select an option"
$ComboBox3.width = 200
$ComboBox3.height = 20
$ComboBox3.location = New-Object System.Drawing.Point(450, 200)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$ComboBox3.items.Add("View")
$ComboBox3.items.Add("Edit")
Function Button2_Click()
{
if ($ComboBox1.SelectedIndex -eq 0)
{
$panel2.Visible = $true
$panel1.Visible = $false
}
if ($ComboBox1.SelectedIndex -eq 1)
{
$panel1.Visible = $true
$panel2.Visible = $false
}
}
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Start"
$Button1.width = 150
$Button1.height = 30
$Button1.BackColor = '#F6CEE3'
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button1.location = New-Object System.Drawing.Point(500, 700)
$Button1.Font = 'segoe ui,10'
$Button1.Add_Click({
Button1_Click;
$global:Button1Clicked = 1;
})
############# Button 'Go' #############
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Go"
$Button2.width = 100
$Button2.height = 30
$Button2.BackColor = '#F6CEE3'
$Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button2.location = New-Object System.Drawing.Point(680, 43)
$Button2.Font = 'segoe ui,10'
$Button2.Add_Click({
Button2_Click;
$global:Button2Clicked = 1;
})
######### code Starts ###########
function Button1_Click()
{
$link = $comboBox2.SelectedItem.ToString();
$linktype = $comboBox3.SelectedItem.ToString();
}
####### end of code ######
$form.Controls.Add($Panel1)
$form.Controls.Add($Panel2)
$form.Controls.Add($button1)
$form.Controls.Add($comboBox1)
$form.Controls.Add($button2)
$form.Controls.Add($pictureBox1)
$form.Controls.Add($label1)
$form.Controls.Add($label2)
[void]$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()
The issue is that your variables are being set in a function, so they are scoped to that function. There's really no reason to put that in a function, put it directly in the .add_click() instead. Or if you feel that you need to keep it in the function you can scope the variable like you did with $global:ButtonClicked and set them to $global:Link and $global:LinkType.
Edit: Order does make a difference in PowerShell, so I did move some stuff around in your script to make it work right, but I was able to get values to reflect fine when I put them in the global scope.
$global:Button1Clicked = 0;
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.FormClosingEventHandler]
######### code Starts ###########
function Button1_Click()
{
write-host "Link = $global:link"
write-host "Link Type = $global:linktype"
}
Function Button2_Click()
{
if ($ComboBox1.SelectedIndex -eq 0)
{
$panel2.Visible = $true
$panel1.Visible = $false
}
if ($ComboBox1.SelectedIndex -eq 1)
{
$panel1.Visible = $true
$panel2.Visible = $false
}
}
####### end of code ######
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1500,800'
$Form.text = "NextUI "
$Form.BackColor = 'White'
$Form.TopMost = $true
$Form.add_closing(
{
if($global:Button2Clicked)
{
$global:Button2Clicked = 0;
$_.cancel = $true;
}
})
################ Module combo box ################
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = "Select Module"
$ComboBox1.width = 200
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(310, 45)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$combobox1.items.Add("ControlSettings")
$combobox1.items.Add("NextSettings")
######## file and folder permission #############
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = "select an option"
$ComboBox2.width = 200
$ComboBox2.height = 20
$ComboBox2.location = New-Object System.Drawing.Point(450, 200)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$ComboBox2.items.AddRange(#("View","Edit") )
#$ComboBox2.items.Add("Edit")
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = "select an option"
$ComboBox3.width = 200
$ComboBox3.height = 20
$ComboBox3.location = New-Object System.Drawing.Point(450, 200)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$ComboBox3.items.Add("View")
$ComboBox3.items.Add("Edit")
$panel1 = New-Object system.Windows.Forms.Panel
$panel1.AutoSize = $true
$panel1.Width = 1200
$panel1.Height = 200
$panel1.location = New-Object System.Drawing.Point(50,100)
$panel1.Visible = $false
$panel1.Controls.Add($label3)
$panel1.Controls.Add($comboBox2)
$panel1.BorderStyle = 1
$panel2 = New-Object system.Windows.Forms.Panel
$panel2.AutoSize = $true
$panel2.Width = 1200
$panel2.Height =200
$panel2.location = New-Object system.Drawing.Point(50,100)
$panel2.Visible = $false
$panel2.Controls.Add($label4)
$panel2.Controls.Add($ComboBox3)
$panel2.BorderStyle = 1
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Select Module"
$Label1.AutoSize = $true
$Label1.location = New-Object System.Drawing.Point(35,50)
$Label1.Font = 'segoe ui,9.5'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "SharePoint Settings"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(35,15)
$Label2.Font = 'Segoe UI Semibold,9.5'
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Choose file and folder permission option"
$Label3.AutoSize = $true
$Label3.location = New-Object System.Drawing.Point(100,200)
$Label3.Font = 'segoe ui,9.5'
$Label4 = New-Object system.Windows.Forms.Label
$Label4.AutoSize = $True
$Label4.text = "File Permission"
$Label4.location = New-Object System.Drawing.Point(100,200)
$Label4.Font = 'Segoe UI ,9.5'
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Start"
$Button1.width = 150
$Button1.height = 30
$Button1.BackColor = '#F6CEE3'
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button1.location = New-Object System.Drawing.Point(500, 700)
$Button1.Font = 'segoe ui,10'
$Button1.Add_Click({
$global:link = $comboBox2.SelectedItem.ToString();
$global:linktype = $comboBox3.SelectedItem.ToString();
Button1_Click;
$global:Button1Clicked = 1;
})
############# Button 'Go' #############
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Go"
$Button2.width = 100
$Button2.height = 30
$Button2.BackColor = '#F6CEE3'
$Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button2.location = New-Object System.Drawing.Point(680, 43)
$Button2.Font = 'segoe ui,10'
$Button2.Add_Click({
Button2_Click;
$global:Button2Clicked = 1;
})
$form.Controls.Add($Panel1)
$form.Controls.Add($Panel2)
$form.Controls.Add($button1)
$form.Controls.Add($comboBox1)
$form.Controls.Add($button2)
$form.Controls.Add($pictureBox1)
$form.Controls.Add($label1)
$form.Controls.Add($label2)
[void]$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()
When I ran that, set the two combobox values, and clicked Start I got text in the ISE saying what I set the Link and LinkType variables to, and was able to echo $global:link and $global:linktype to see the values correctly assigned.
I want to prevent my Powershell Form from closing after a submit button is pressed. After the function "search_PC" there is no more code and the form closes. I want to still be able to write stuff in there after the function is finished. I have already tried the pause function and while($true) loops. I would be very happy about an answer.
Import-Module ActiveDirectory
# Assembly for GUI
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Create Form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Size = New-Object System.Drawing.Size(400,500)
$objForm.Backcolor="white"
$objForm.StartPosition = "CenterScreen"
$objForm.Text = "Example Software"
$objForm.Icon="C:\Users\...\icon.ico"
# Label
$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(80,30)
$objLabel1.Size = New-Object System.Drawing.Size(60,20)
$objLabel1.Text = "UserID:"
$objForm.Controls.Add($objLabel1)
# Textbox
$objTextBox1 = New-Object System.Windows.Forms.TextBox
$objTextBox1.Location = New-Object System.Drawing.Size(160,28)
$objTextBox1.Size = New-Object System.Drawing.Size(100,20)
$objForm.Controls.Add($objTextBox1)
# Label
$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(80,72)
$objLabel2.Size = New-Object System.Drawing.Size(80,20)
$objLabel2.Text = "PC-Name:"
$objForm.Controls.Add($objLabel2)
# Textbox
$objTextBox2 = New-Object System.Windows.Forms.TextBox
$objTextBox2.Location = New-Object System.Drawing.Size(160,70)
$objTextBox2.Size = New-Object System.Drawing.Size(100,20)
$objForm.Controls.Add($objTextBox2)
# Button for closing
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(100,220)
$CancelButton.Size = New-Object System.Drawing.Size(163,25)
$CancelButton.Text = "Exit"
$CancelButton.Name = "Exit"
$CancelButton.DialogResult = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
# Checkbox
$objTypeCheckbox1 = New-Object System.Windows.Forms.Checkbox
$objTypeCheckbox1.Location = New-Object System.Drawing.Size(80,120)
$objTypeCheckbox1.Size = New-Object System.Drawing.Size(500,20)
$objTypeCheckbox1.Text = "Internal Session Support"
$objTypeCheckbox1.TabIndex = 1
$objTypeCheckbox1.Add_Click({$objTypeCheckbox2.Checked = $false})
$objForm.Controls.Add($objTypeCheckbox1)
# Checkbox
$objTypeCheckbox2 = New-Object System.Windows.Forms.Checkbox
$objTypeCheckbox2.Location = New-Object System.Drawing.Size(80,140)
$objTypeCheckbox2.Size = New-Object System.Drawing.Size(500,20)
$objTypeCheckbox2.Text = "Session Support Internal & external"
$objTypeCheckbox2.TabIndex = 2
$objTypeCheckbox2.Add_Click({$objTypeCheckbox1.Checked = $false})
$objForm.Controls.Add($objTypeCheckbox2)
# Button fo checking the User Input
$SubmitButton = New-Object System.Windows.Forms.Button
$SubmitButton.Location = New-Object System.Drawing.Size(100,190)
$SubmitButton.Size = New-Object System.Drawing.Size(163,25)
$SubmitButton.Text = "Submit"
$SubmitButton.Name = "Submit"
$SubmitButton.DialogResult = "OK"
$objForm.AcceptButton = $SubmitButton
$SubmitButton.Add_Click({
$User = $objTextBox1.Text
$PC = $objTextBox2.Text
# Check if all User Inputs are filled out
if ( !($User.trim()) -or !($PC.trim()) ) {
[void] [Windows.Forms.MessageBox]::Show("Please fill out every value!", "Error", [Windows.Forms.MessageBoxButtons]::ok, [Windows.Forms.MessageBoxIcon]::Warning)}
if($objTypeCheckbox1.checked -eq $true) {$Software = "Internal"}
elseif($objTypeCheckbox2.checked -eq $true) {$Software = "Internal&External"}
else {[void] [Windows.Forms.MessageBox]::Show("Please fill out every value!", "Error", [Windows.Forms.MessageBoxButtons]::ok, [Windows.Forms.MessageBoxIcon]::Warning)}
search_user
})
$objForm.Controls.Add($SubmitButton)
$statusBar1 = New-Object System.Windows.Forms.StatusBar
$objForm.controls.add($statusBar1)
function createFile
{
$Date = Get-Date -Format d.M.yyyy
$Time = (Get-Date).ToString(„HH:mm:ss“)
$Time2 = (Get-Date).ToString(„HH-mm-ss“)
$Date2 = Get-Date -Format yyyy-M-d;
$FileName = $Time2 + " " + $Date2
$wrapper = New-Object PSObject -Property #{ 1 = $Date; 2 = $Time; 3 = $User; 4 = $PC; 5 = $Software }
Export-Csv -InputObject $wrapper -Path C:\Users\...\$FileName.csv -NoTypeInformation
[void] [Windows.Forms.MessageBox]::Show("Successful!")
}
function search_user
{
if (#(Get-ADUser -Filter {SamAccountName -eq $User}).Count -eq 0) {
$objTextBox1.BackColor = "Red";
$statusBar1.Text = "Couldn't find user!"
$objForm.Dispose()
}
else{$objTextBox1.BackColor = "Green"
search_PC}
}
function search_PC
{
$Client = $PC
if (#(Get-ADComputer -Filter {DNSHostName -eq $Client}).Count -eq 0) {
$objTextBox2.BackColor = "Red";
$statusBar1.Text = "Couldn't find PC!"
$objForm.Dispose()
}
else{$objTextBox2.BackColor = "Green"
createFile}
}
$objForm.ShowDialog() #Showing Form and elements
Screenshot of the form:
This line is your problem:
$SubmitButton.DialogResult = "OK"
It should be:
$SubmitButton.DialogResult = [System.Windows.Forms.DialogResult]::None
Or just be removed.
DialogResult
The script is a simple administration tool to indicate the status of three windows services and to toggle them.
Everything works fine but I don't get it done to implement the refreshment of the GUI.
I want to show up the related status in labels and to hide the non-clickable buttons. I was playing around so far with a timer, but the GUI still doesn't refresh..
#variables
$ums = get-service "UMS Server"
$mySQL = get-service "mySQL56"
$maria = get-service "MariaDB_10.1.21"
function OnApplicationLoad {
return $true
}
function OnApplicationExit {
$script:ExitCode = 0
}
function generateForm {
Add-Type -AssemblyName System.Windows.Forms
$UMSDatabaseadministration = New-Object system.Windows.Forms.Form
$UMSDatabaseadministration.Text = "UMS Database administration"
$UMSDatabaseadministration.TopMost = $true
$UMSDatabaseadministration.Width = 614
$UMSDatabaseadministration.Height = 316
$UMSDatabaseadministration.StartPosition = "CenterScreen"
$timer = New-Object System.Windows.Forms.Timer #$UMSDatabaseadministration
$timer.Interval = 1000 # once per second
$timer.Add_Tick({ $UMSDatabaseadministration.Refresh() })
#button 1 start UMS
$button11 = New-Object system.windows.Forms.Button
$button11.Text = "start"
$button11.Width = 60
$button11.Height = 30
if ($ums.Status -eq "Running"){
$button11.visible = $false
}
$button11.Add_Click({
$ums.start()
})
$button11.location = new-object system.drawing.point(303,38)
$button11.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($button11)
#button 2 stop UMS
$button15 = New-Object system.windows.Forms.Button
$button15.Text = "stop"
$button15.Width = 60
$button15.Height = 30
if ($ums.Status -eq "Stopped"){
$button15.visible = $false
}
$button15.Add_Click({$ums.stop()})
$button15.location = new-object system.drawing.point(409,39)
$button15.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($button15)
#button 3 start mySQL
$button12 = New-Object system.windows.Forms.Button
$button12.Text = "start"
$button12.Width = 60
$button12.Height = 30
if ($maria.Status -eq "Running" -Or $mySQL.Status -eq "Running"){
$button12.visible = $false
}
$button12.Add_Click({$mySQL.start()})
$button12.location = new-object system.drawing.point(303,98)
$button12.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($button12)
#button 4 stop mySQL
$button14 = New-Object system.windows.Forms.Button
$button14.Text = "stop"
$button14.Width = 60
$button14.Height = 30
if ($mySQL.Status -eq "Stopped"){
$button14.visible = $false
}
$button14.Add_Click({$mySQL.stop()})
$button14.location = new-object system.drawing.point(410,99)
$button14.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($button14)
#button 5 start mariaDB
$button13 = New-Object system.windows.Forms.Button
$button13.Text = "start"
$button13.Width = 60
$button13.Height = 30
if ($mySQL.Status -eq "Running" -Or $maria.Status -eq "Running"){
$button13.visible = $false
}
$button13.Add_Click({$maria.start()})
$button13.location = new-object system.drawing.point(302,147)
$button13.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($button13)
#button 6 stop mariaDB
$button16 = New-Object system.windows.Forms.Button
$button16.Text = "stop"
$button16.Width = 60
$button16.Height = 30
if ($maria.Status -eq "Stopped"){
$button16.visible = $false
}
$button16.Add_Click({$maria.stop()})
$button16.location = new-object system.drawing.point(410,148)
$button16.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($button16)
$button17 = New-Object system.windows.Forms.Button
$button17.Text = "shut down UMS Server and toggle DB`'s"
$button17.Add_Click({
#variables
$ums = get-service "UMS Server"
$mySQL = get-service "mySQL56"
$maria = get-service "MariaDB_10.1.21"
if ($ums.Status -eq "Running") {$ums.stop()}
if ($mySQL.Status -eq "Running") {$mySQL.stop(); $maria.start()}
if ($maria.Status -eq "Running") {$maria.stop(); $mySQL.start()}
})
$button17.Width = 166
$button17.Height = 42
$button17.location = new-object system.drawing.point(303,209)
$button17.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($button17)
$label18 = New-Object system.windows.Forms.Label
$label18.Text = "UMS Server is:"
$label18.AutoSize = $true
$label18.Width = 25
$label18.Height = 10
$label18.location = new-object system.drawing.point(33,38)
$label18.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($label18)
$label19 = New-Object system.windows.Forms.Label
$label19.Text = "mySQL is:"
$label19.AutoSize = $true
$label19.Width = 25
$label19.Height = 10
$label19.location = new-object system.drawing.point(33,95)
$label19.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($label19)
$label20 = New-Object system.windows.Forms.Label
$label20.Text = "mariaDB is:"
$label20.AutoSize = $true
$label20.Width = 25
$label20.Height = 10
$label20.location = new-object system.drawing.point(34,146)
$label20.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($label20)
#status UMS red
$label24 = New-Object system.windows.Forms.Label
$label24.Text = $ums.status
$label24.AutoSize = $true
$label24.ForeColor = "#fe0004"
$label24.Width = 25
$label24.Height = 10
if ($ums.status -eq "Running"){
$label24.visible = $false
}
$label24.location = new-object system.drawing.point(152,37)
$label24.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($label24)
#status UMS green
$label25 = New-Object system.windows.Forms.Label
$label25.Text = $ums.status
$label25.AutoSize = $true
$label25.ForeColor = "#149600"
$label25.Width = 25
$label25.Height = 10
if ($ums.status -eq "Stopped"){
$label25.visible = $false
}
$label25.location = new-object system.drawing.point(153,40)
$label25.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($label25)
#status mySQL red
$label26 = New-Object system.windows.Forms.Label
$label26.Text = $mySQL.status
$label26.AutoSize = $true
$label26.ForeColor = "#ff0004"
$label26.Width = 25
$label26.Height = 10
if ($mySQL.status -eq "Running"){
$label26.visible = $false
}
$label26.location = new-object system.drawing.point(152,94)
$label26.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($label26)
#status mySQL green
$label27 = New-Object system.windows.Forms.Label
$label27.Text = $mySQL.status
$label27.AutoSize = $true
$label27.ForeColor = "#149600"
$label27.Width = 25
$label27.Height = 10
if ($mySQL.status -eq "Stopped"){
$label27.visible = $false
}
$label27.location = new-object system.drawing.point(152,96)
$label27.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($label27)
#status mariaDB red
$label28 = New-Object system.windows.Forms.Label
$label28.Text = $maria.status
$label28.AutoSize = $true
$label28.ForeColor = "#ff0004"
$label28.Width = 25
$label28.Height = 10
if ($maria.status -eq "Running"){
$label28.visible = $false
}
$label28.location = new-object system.drawing.point(151,145)
$label28.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($label28)
#status mariaDB green
$label29 = New-Object system.windows.Forms.Label
$label29.Text = $maria.status
$label29.AutoSize = $true
$label29.ForeColor = "#149600"
$label29.Width = 25
$label29.Height = 10
if ($maria.status -eq "Stopped"){
$label29.visible = $false
}
$label29.location = new-object system.drawing.point(151,145)
$label29.Font = "Microsoft Sans Serif,10"
$UMSDatabaseadministration.controls.Add($label29)
[void]
$UMSDatabaseadministration.ShowDialog()
$UMSDatabaseadministration.Dispose()
}
if(OnApplicationLoad -eq $true)
{
GenerateForm | Out-Null
$timer.Start()
OnApplicationExit
}
First I must say that your code needs a lot of TLC. You shouldn't label your buttons or labels 1-20. Makes the code look messy and 15 days down the line you will have no idea what the code says. At lines 2-4 and 128-130 you define your variables twice.
What I have done now is create this Service GUI that will not just help you out, but anyone looking to do the same kind of task. You simple add the services you want into the Param function.
First we see if you are an Administrator, because not everyone is.
Add-Type -AssemblyName System.Windows.Forms
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) {
if ($elevated)
{
# tried to elevate, did not work, aborting
}
else {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
We create a central location to have the parameters.
Note that you see that I use the $Global: You can read about that here:
https://msdn.microsoft.com/en-us/powershell/reference/3.0/microsoft.powershell.core/about/about_scopes
Function Param{
$Global:Option1 = get-service "Hamachi2Svc"
$Global:Option2 = get-service "NitroUpdateService"
$Global:Option3 = get-service "TeamViewer"
$Global:Option1Txt=$Global:Option1.Displayname
$Global:Option2Txt=$Global:Option2.Displayname
$Global:Option3Txt=$Global:Option3.Displayname
}
Now we create the Form
Function ServiceAdminForm {
$Form.Close()
$Form.Dispose()
Test-Admin
MakeForm
}
Now we define the functions that our buttons will use.
Function Option1 {
if ($Global:Option1.Status -eq "Running"){ Stop-Service $Global:Option1
}
else {Start-Service $Global:Option1}
}
Function Option2 {
if ($Global:Option2.Status -eq "Running"){ Stop-Service $Global:Option2
}
else {Start-Service $Global:Option2}
}
Function Option3 {
if ($Global:Option3.Status -eq "Running"){ Stop-Service $Global:Option3
}
else {Start-Service $Global:Option3}
}
Function Toggle {
if ($Global:Option1.Status -eq "Running") {$Global:Option1.stop()}
if ($Global:Option2.Status -eq "Running") {$Global:Option2.stop(); $Global:Option3.start()}
if ($Global:Option3.Status -eq "Running") {$Global:Option3.stop(); $Global:Option2.start()}
}
Now we make the layout of the GUI
Calling the parameters and the button functions.
Function MakeForm {
Param
$script:Form = New-Object system.Windows.Forms.Form
$Form.Text = "Service Administration"
$Font = New-Object System.Drawing.Font("Times New Roman",12,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
#Label for Option1
$Global:Option1lbl = New-Object system.windows.Forms.Label
$Global:Option1lbl.Text = $Global:Option1.Status
$Global:Option1lbl.AutoSize = $true
$Global:Option1lbl.Width = 25
$Global:Option1lbl.Height = 10
$Global:Option1lbl.location = new-object system.drawing.point(5,90)
#Label for Option2
$Global:Option2lbl = New-Object system.windows.Forms.Label
$Global:Option2lbl.Text = $Global:Option2.Status
$Global:Option2lbl.AutoSize = $true
$Global:Option2lbl.Width = 25
$Global:Option2lbl.Height = 10
$Global:Option2lbl.location = new-object system.drawing.point(5,150)
#Label for Option3
$Global:Option3lbl = New-Object system.windows.Forms.Label
$Global:Option3lbl.Text = $Global:Option3.Status
$Global:Option3lbl.AutoSize = $true
$Global:Option3lbl.Width = 25
$Global:Option3lbl.Height = 10
$Global:Option3lbl.location = new-object system.drawing.point(5,210)
#Refresh/Reload
$Reloadbtn = New-Object System.Windows.Forms.Button
$Reloadbtn.Location = New-Object System.Drawing.Size(5,10)
$Reloadbtn.AutoSize = $true
$Reloadbtn.Text = "Reload"
$Reloadbtn.Add_Click({ServiceAdminForm})
#Toggle
$Togglebtn = New-Object System.Windows.Forms.Button
$Togglebtn.Location = New-Object System.Drawing.Size(80,10)
$Togglebtn.AutoSize = $true
$Togglebtn.Text = "Toggle"
$Togglebtn.Add_Click({Toggle})
#Button Option1
$Global:Option1btn = New-Object System.Windows.Forms.Button
$Global:Option1btn.Location = New-Object System.Drawing.Size(5,60)
$Global:Option1btn.AutoSize = $true
$Global:Option1btn.Text = "$Global:Option1Txt"
$Global:Option1btn.Add_Click({Option1})
#Button Option2
$Global:Option2btn = New-Object System.Windows.Forms.Button
$Global:Option2btn.Location = New-Object System.Drawing.Size(5,120)
$Global:Option2btn.AutoSize = $true
$Global:Option2btn.Text = "$Global:Option2Txt"
$Global:Option2btn.Add_Click({Option2})
#Button Option3
$Global:Option3btn = New-Object System.Windows.Forms.Button
$Global:Option3btn.Location = New-Object System.Drawing.Size(5,180)
$Global:Option3btn.AutoSize = $true
$Global:Option3btn.Text = "$Global:Option3Txt"
$Global:Option3btn.Add_Click({Option3})
#Form Controls
$Form.Controls.Add($Global:Option1lbl)
$Form.Controls.Add($Global:Option2lbl)
$Form.Controls.Add($Global:Option3lbl)
$Form.Controls.Add($Reloadbtn)
$Form.Controls.Add($Togglebtn)
$Form.Controls.Add($Global:Option1btn)
$Form.Controls.Add($Global:Option2btn)
$Form.Controls.Add($Global:Option3btn)
$Form.ShowDialog()
}
MakeForm
Maybe someone has a better way to refresh, but reloading the form does the job.