Powershell Timer - Update gui - powershell

I have created a powershell timer, and after every 1 second, I would like to run a function to execute or post text to a text box log on the interface.
Run the below. When you click Start, every 1 second, the log text area should show "Post to log every 1 second, not as a batch". However, this messages only appear as a batch, all at once, when you click Stop.
This question does not appear to be answered on the internet!
Code:
$global:timer = New-Object System.Timers.Timer
$global:timer.Interval = 1000
function AddToLog($logtext)
{
$txtLog.Text = $txtLog.Text + "`r`n" + $logtext
$txtLog.ScrolltoCaret
}
function startTimer() {
Register-ObjectEvent -InputObject $global:timer -EventName Elapsed -SourceIdentifier theTimer -Action {AddToLog('Post to log every 1 second, not as a batch') }
$global:timer.start()
Write-Host "Start Timer"
}
function stopTimer() {
$global:timer.stop()
Write-Host "Close Function"
Unregister-Event theTimer
}
########################
# Setup User Interface
########################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Timer Example"
$objForm.Size = New-Object System.Drawing.Size(330,380)
$objForm.StartPosition = "CenterScreen"
#Start Button
$btnStart = New-Object System.Windows.Forms.Button
$btnStart.Location = New-Object System.Drawing.Size(10,190)
$btnStart.Size = New-Object System.Drawing.Size(140,35)
$btnStart.Text = "Start"
$btnStart.Add_Click({StartTimer; })
$objForm.Controls.Add($btnStart)
#Stop Button
$btnStop = New-Object System.Windows.Forms.Button
$btnStop.Location = New-Object System.Drawing.Size(150,190)
$btnStop.Size = New-Object System.Drawing.Size(140,35)
$btnStop.Text = "Stop"
$btnStop.Add_Click({StopTimer; })
$objForm.Controls.Add($btnStop)
$btnStop.Enabled = $true
#Log Area
$lblLog = New-Object System.Windows.Forms.Label
$lblLog.Location = New-Object System.Drawing.Size(10,230)
$lblLog.Size = New-Object System.Drawing.Size(80,20)
$lblLog.Text = "Event Log:"
$objForm.Controls.Add($lblLog)
$txtLog = New-Object System.Windows.Forms.Textbox
$txtLog.Location = New-Object System.Drawing.Size(10,250)
$txtLog.Size = New-Object System.Drawing.Size(290,90)
$txtLog.Multiline = $True
$txtLog.Scrollbars = "vertical"
$txtLog.Add_Click({$txtLog.SelectAll(); $txtLog.Copy()})
$objForm.Controls.Add($txtLog)
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
Thanks for your help in advanced.
-R

I took what you had, and looked at this egg_timer project and came up with the following:
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
$timer.add_tick({AddToLog 'Post to log every 1 second, not as a batch'})
function AddToLog($logtext)
{
$txtLog.Text = $txtLog.Text + "`r`n" + $logtext
$txtLog.ScrolltoCaret
}
function startTimer() {
$timer.start()
}
function stopTimer() {
$timer.Enabled = $false
Write-Host "Close Function"
}
########################
# Setup User Interface
########################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Timer Example"
$objForm.Size = New-Object System.Drawing.Size(330,380)
$objForm.StartPosition = "CenterScreen"
#Start Button
$btnStart = New-Object System.Windows.Forms.Button
$btnStart.Location = New-Object System.Drawing.Size(10,190)
$btnStart.Size = New-Object System.Drawing.Size(140,35)
$btnStart.Text = "Start"
$btnStart.Add_Click({StartTimer; })
$objForm.Controls.Add($btnStart)
#Stop Button
$btnStop = New-Object System.Windows.Forms.Button
$btnStop.Location = New-Object System.Drawing.Size(150,190)
$btnStop.Size = New-Object System.Drawing.Size(140,35)
$btnStop.Text = "Stop"
$btnStop.Add_Click({StopTimer; })
$objForm.Controls.Add($btnStop)
$btnStop.Enabled = $true
#Log Area
$lblLog = New-Object System.Windows.Forms.Label
$lblLog.Location = New-Object System.Drawing.Size(10,230)
$lblLog.Size = New-Object System.Drawing.Size(80,20)
$lblLog.Text = "Event Log:"
$objForm.Controls.Add($lblLog)
$txtLog = New-Object System.Windows.Forms.Textbox
$txtLog.Location = New-Object System.Drawing.Size(10,250)
$txtLog.Size = New-Object System.Drawing.Size(290,90)
$txtLog.Multiline = $True
$txtLog.Scrollbars = "vertical"
$txtLog.Add_Click({$txtLog.SelectAll(); $txtLog.Copy()})
$objForm.Controls.Add($txtLog)
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

Try using a System.Windows.Forms.Timer. See this article for info on the differences between .NET timers.
In the case of a WinForms timer, ditch the Register-ObjectEvent and Unregister-Event and add this line before you call ShowDialog():
$timer.add_Tick({AddToLog('Post to log every 1 second, not as a batch')})
Don't forget to change to System.Windows.Forms.Timer. When I make those change, the UI works.
Use of Register-ObjectEvent requires that PowerShell services the event queue at various safe points in its execution. However, since PowerShell is blocked inside some .NET code (Form.ShowDialog()), it doesn't get a chance to service its event queue.

Related

How to return a SelectedItems value when listbox items are inside an "add_SelectedValueChanged" block

After selecting an option from the drop down list ($RCVROption), when choosing an option from the list provided ($listBox), the $listBox.SelectedItems values are always Null. How do I get those values to assign to variable $x
This is the script I tried, my goal is to have the text from $listBox.SelectedItems to be used as a value that represents a file name, so that when you select an option from the list, and click OK, it runs that file.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
$form = New-Object System.Windows.Forms.Form
$form.Text = 'DMP Receiver Tail'
$form.Size = New-Object System.Drawing.Size(400,300)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(115,220)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(190,220)
$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)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(60,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Which Receiver Would You Like To Tail?:'
$form.Controls.Add($label)
$RCVROption = new-object System.Windows.Forms.combobox
$RCVROption.Location = new-object System.Drawing.Size(20,40)
$RCVROption.Size = new-object System.Drawing.Size(335,30)
[void] $RCVROption.Items.Add('Receiver 560')
[void] $RCVROption.Items.Add('Receiver 2560')
$RCVROption.tabIndex = '0'
$RCVROption.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$RCVROption.add_SelectedValueChanged(
{
if($RCVROption.SelectedItem -eq 'Receiver 560')
{
$listBox = New-Object System.Windows.Forms.Listbox
$listBox.Location = New-Object System.Drawing.Point(40,100)
$listBox.Size = New-Object System.Drawing.Size(260,80)
$listBox.SelectionMode = 'MultiExtended'
[void] $listBox.Items.Add('DMP-560-Line1')
[void] $listBox.Items.Add('DMP-560-Line2')
[void] $listBox.Items.Add('DMP-560-Line3')
[void] $listBox.Items.Add('DMP-560-Line4')
[void] $listBox.Items.Add('DMP-560-Line5')
$form.Controls.Add($listBox)
}
ELSEIF($RCVROption.SelectedItem -eq 'Receiver 2560')
{
$listBox = New-Object System.Windows.Forms.Listbox
$listBox.Location = New-Object System.Drawing.Point(40,100)
$listBox.Size = New-Object System.Drawing.Size(260,80)
$listBox.SelectionMode = 'MultiExtended'
[void] $listBox.Items.Add('DMP-2560-Line1')
[void] $listBox.Items.Add('DMP-2560-Line2')
[void] $listBox.Items.Add('DMP-2560-Line3')
[void] $listBox.Items.Add('DMP-2560-Line4')
[void] $listBox.Items.Add('DMP-2560-Line5')
$form.Controls.Add($listBox)
}
}
)
$form.Controls.Add($RCVROption)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox.SelectedItems
$Fpath = 'D:\Toolbar\On Call\DMP-Tail\' + $x + '.exe'
Start-process -filepath $FPath
$Fpath
}
Based on your code, I'm guessing you come from a C# background. PowerShell does something weird where it can normally reach outside of script block's scope {code in scriptblock} and use variables that already exist at the time the scriptblock is executed. But it can't easily create or add new variables in the parent/outer scope.
Your code is creating $listBox inside the $RCVROption.add_SelectedValueChanged({code in scriptblock}) event scriptblock, and adding it to the controls for the form, so it continues to exist in the form, but not at the global/script scope of the your code.
I've reworked your code in a format that I've been learning over the last year. Everything you had should still be there, only in a more condensed form. You don't have to use this format, but I personally really like it. How PowerShell converts things like "Cancel" into [System.Windows.Forms.DialogResult]::Cancel is a bit of a mistery to me, but this type of conversion seems very reliable.
This version of the code creates the $listBox prior to firing of the $RCVROption.add_SelectedValueChanged event, clears the items in $listBox, and then populates the ListBox with the new values. The values are in an array, and piped into the [void] $listBox.Items.Add($_) command, executing it once per item in the array.
using namespace System.Windows.Forms
using namespace System.Drawing
Set-StrictMode -Version 3.0
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[Application]::EnableVisualStyles()
$form = [Form]#{
Text = 'DMP Receiver Tail'
Size = "400,300"
StartPosition = 'CenterScreen'
Topmost = $true
}
$OKButton = [Button]#{
DialogResult = "OK"
Location = "115,220"
Size = "75,23"
Text = 'OK'
}
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = [Button]#{
DialogResult = "Cancel"
Location = "190,220"
Size = "75,23"
Text = 'Cancel'
}
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$label = [Label]#{
Location = "60,20"
Size = "280,20"
Text = 'Which Receiver Would You Like To Tail?:'
}
$form.Controls.Add($label)
$RCVROption = [ComboBox]#{
DropDownStyle = "DropDownList"
Location = "20,40"
Size = "335,30"
tabIndex = '0'
}
[void] $RCVROption.Items.Add('Receiver 560')
[void] $RCVROption.Items.Add('Receiver 2560')
$listBox = [Listbox]#{
Location = "40,100"
SelectionMode = 'MultiExtended'
Size = "260,80"
}
$form.Controls.Add($listBox)
$RCVROption.add_SelectedValueChanged({
if($RCVROption.SelectedItem -eq 'Receiver 560') {
$listBox.Items.Clear()
'DMP-560-Line1', 'DMP-560-Line2', 'DMP-560-Line3', 'DMP-560-Line4', 'DMP-560-Line5' | ForEach-Object {
[void] $listBox.Items.Add($_)
}
}
elseif($RCVROption.SelectedItem -eq 'Receiver 2560') {
$listBox.Items.Clear()
'DMP-2560-Line1', 'DMP-2560-Line2', 'DMP-2560-Line3', 'DMP-2560-Line4', 'DMP-2560-Line5' | ForEach-Object {
[void] $listBox.Items.Add($_)
}
}
})
$form.Controls.Add($RCVROption)
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
$x = $listBox.SelectedItems
$Fpath = 'D:\Toolbar\On Call\DMP-Tail\' + $x + '.exe'
Start-process -filepath $FPath
$Fpath
}

Powershell dialog box with countdown and option to press cancel?

I have been trying to create a Powershell script that performs the following
Shows a dialog box that an update is about to occur
Provide a countdown of say 30 seconds
During the countdown, a user can press "Cancel Update"
If the countdown expires and "Cancel Update" was not pressed, then update will occur
Right before the loop, the window shows if I call $Counter_Form.ShowDialog() and I can click the Cancel button. When clicking, the following should occur.
Window should close after pressing the button. This is correct.
$cancel should be set to $true to indicate that Cancel was pressed. However, it remains $false and this is incorrect. Why is this?
Now, for the problems in the while loop
The window refreshes to show the new delay, but I cannot click "Cancel Update" since it just shows an hourglass icon and seems to be frozen
Script
#Adjust delay here
$delay = 5
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Counter_Form = New-Object System.Windows.Forms.Form
$Counter_Form.Text = "Warning"
#Form size options
$Counter_Form.Width = 350
$Counter_Form.Height = 150
#Centers form on screen
$Counter_Form.StartPosition = "CenterScreen"
#Places form on top of everything else
$Counter_Form.TopMost = $true
$Counter_Label = New-Object System.Windows.Forms.Label
$Counter_Label2 = New-Object System.Windows.Forms.Label
#Label2's text
$Counter_Label2.Text = "Please save all your work"
#Labels size and position
$Counter_Label.AutoSize = $true
$Counter_Label.Location = New-Object System.Drawing.Point(50,60)
$Counter_Label2.AutoSize = $true
$Counter_Label2.Location = New-Object System.Drawing.Point(90,30)
$cancel = $false
$button1 = New-Object System.Windows.Forms.Button
$button1.Text = "Cancel Update";
$button1.Location = New-Object System.Drawing.Point(130,80)
$button1.Add_Click({ $Counter_Form.Close(); $cancel = $true})
$Counter_Form.Controls.Add($Counter_Label)
$Counter_Form.Controls.Add($Counter_Label2)
$Counter_Form.Controls.Add($button1)
#LOOP!
while ($delay -ge 0 -And $cancel -eq $false)
{
$Counter_Form.Show()
#Timer label's text
$Counter_Label.Text = "Update will occur in $($delay) seconds."
start-sleep 1
$delay -= 1
}
$Counter_Form.Close()
For the first question about $cancel not being updated to $true, it is a result of scope.
Add the script scope, with $script:cancel = $true. So that line of code should be:
$button1.Add_Click({ $Counter_Form.Close(); $script:cancel = $true})
I didn't find an exact solution to what I started. A pieced together some code from various sources and found the following worked. I'm posting it here in case it ends up helping someone:
function ShowMsg ($timeout, $message)
{
Add-Type -AssemblyName system.windows.forms
Add-Type -AssemblyName system.drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = "Window Title"
$form.Size = New-Object System.Drawing.Size(300,300)
$form.StartPosition = 'CenterScreen'
$label = New-Object System.Windows.Forms.label
$label.Text = $message
$label.Size = New-Object System.Drawing.Size(280,205)
$form.Controls.Add($label)
#add button to form
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(160,215)
$okButton.Size = New-Object System.Drawing.Size(100,23)
$okButton.Text = 'Cancel Update'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(40,215)
$cancelButton.Size = New-Object System.Drawing.Size(100,23)
$cancelButton.Text = 'Allow Update'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::CANCEL
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = $timeout * 1000
$timer.add_tick({$form.Close()})
$timer.Start()
$form.Topmost = $true
$form.ShowDialog()
$form.Dispose()
}
Calling the function:
$timeout = 60 #seconds
$message_response = ShowMsg -timeout $timeout -message "Hello, update is happening"

Stop-Job Global:job isn't defined as an ID or value

This is a basic ping tool that has gone through many changes, but as it stands the stop function under the first button for some reason isn't defined and doesn't allow the process to stop.
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
$job = $null
$isRunning = $false
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Clevagroup Pinger'
$form.Size = New-Object System.Drawing.Size(350,150)
$form.StartPosition = 'CenterScreen'
$form.Topmost = $true
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Ping"
$Button.Add_Click({
if($global:isRunning -eq $false){
$global:job = Start-Job -ScriptBlock {Ping 8.8.8.8 -t > $env:userprofile\desktop\PingResults}
$Button.Text = "Running"
$global:isRunning = $true
} else {
$Button.Text = "Stop Pinging"
Stop-Job $global:job
$global:isRunning = $false
}
})
$Form.Controls.Add($Button)
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(195,35)
$Button1.Size = New-Object System.Drawing.Size(120,23)
$Button1.Text = "Close"
$Button1.Add_Click({
if($global:job -ne $null){
Stop-Job $global:job
}
})
$Form.Controls.Add($Button1)
$form.Add_Shown({$Button.Select()})
$result = $form.ShowDialog()
Thank you for any help you could give.
I added some comments to help you understand the thought process.
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
# Set a reference hashtable where you can store the Job's object
$jobRef = #{ Job = '' }
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Clevagroup Pinger'
$form.Size = New-Object System.Drawing.Size(350,150)
$form.StartPosition = 'CenterScreen'
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Ping"
$Button.Add_Click({
# Save the Job on the reference hashtable
$jobRef.Job = Start-Job -ScriptBlock {
Ping 8.8.8.8 -t
}
# Disable the Ping Button
$this.Enabled = $false
# Enable the Stop Button
$button1.Enabled = $true
})
$Form.Controls.Add($Button)
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(195,35)
$Button1.Size = New-Object System.Drawing.Size(120,23)
$Button1.Text = "Stop"
# This button should be disabled by Default an only become
# Enabled after 'Ping' button is Clicked
$Button1.Enabled = $false
$Button1.Add_Click({
# Stop the Job
Stop-Job $jobRef.Job
# Receive the Job's result and store them in a Txt file
Receive-Job $jobRef.Job | Out-File $env:userprofile\desktop\PingResults.txt
# Remove the Job
Remove-Job $jobRef.Job
# Enable the Ping Button
$button.Enabled = $true
# Disable this Button
$this.Enabled = $false
})
$form.Controls.Add($Button1)
$form.Add_Shown({
$this.Activate()
$Button.Select()
})
$form.ShowDialog()

Show popup message with auto close and on top of all of the windows

I need to create a function that shows a message to the user and if the user doesn't respond then it automatically closes the windows.
function ShowMsg ($timeout,$message)
{
Add-Type -AssemblyName system.windows.forms
Add-Type -AssemblyName system.drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = "Company Name"
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
$label = New-Object System.Windows.Forms.label
$label.Text = $message
$label.Size = New-Object System.Drawing.Size(200,40)
$form.Controls.Add($label)
#add button to form
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = $timeout * 1000
$timer.add_tick({$form.Close()})
$timer.Start()
$form.Topmost = $true
$form.ShowDialog()
$form.Dispose()
}
$timeout = 30
ShowMsg -message “This is the message you will see on the window`nMessage on new line” -timeout $timeout
$r = ShowMsg -message “This is the message you will see on the window`nMessage on new line” -timeout $timeout
if ($r -eq [System.Windows.Forms.DialogResult]::OK)
{
Write-Host “Press ok now”
}else{
Write-Host "$r"
}
I try to use the above code but after showing the message 3 or 4 times it starts closing the popup and in the output, I see "cancel"
enter image description here
The problem is with the timer.
In the form declaration, where you create the timer object, initially set it to $timer.Enabled = $false.
Next, add a $form.Add_Shown({$timer.Enabled = $true; $timer.Start()}) event handler to start the timer when the form is first shown.
In the Tick event of the timer, tell it to Stop() and close the form.
Don't forget to dispose of the timer aswell:
Try
function ShowMsg ([int]$timeout, [string]$message){
Add-Type -AssemblyName system.windows.forms
Add-Type -AssemblyName system.drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = "Company Name"
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
$form.Topmost = $true
$label = New-Object System.Windows.Forms.label
$label.Text = $message
$label.Location = New-Object System.Drawing.Point(10,10)
$label.Size = New-Object System.Drawing.Size(200,40)
$form.Controls.Add($label)
#add button to form
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.Controls.Add($okButton)
$form.AcceptButton = $okButton
$timer = New-Object System.Windows.Forms.Timer
$timer.Enabled = $false # disabled at first
$timer.Interval = $timeout * 1000
$timer.Add_Tick({ $timer.Stop(); $form.Close() })
# start the timer as soon as the form is shown
$form.Add_Shown({$timer.Enabled = $true; $timer.Start()})
$form.ShowDialog()
# clean-up
$timer.Dispose()
$form.Dispose()
}

Returning multiple values from multiple drop-downs in Powershell

I am a very new to powershell. I was tasked with creating a GUI that takes in several strings from multiple drop-downs and renames the computer according to those choices. I am testing it with two options, $FacilityInitials and $BuildingNumber. Powershell is returning only the $BuildingNumber choice.
I may be doing something wrong with returning? How should I do multiple return fuctions properly? Thank you! :)
I tried checking for typos.
#Edit This item to change the DropDown Values
[array]$DropDownArray1 = "LI", "BE", "HA"
# This Function Returns the Selected Value and Closes the Form
function Return-DropDown {
$script:Choice = $DropDown1.SelectedItem.ToString()
$Form.Close()
}
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 350
$Form.height = 200
$Form.Text = ”Computer Renamer”
$DropDown1 = new-object System.Windows.Forms.ComboBox
$DropDown1.Location = new-object System.Drawing.Size(100,30)
$DropDown1.Size = new-object System.Drawing.Size(130,30)
ForEach ($Item in $DropDownArray1) {
[void] $DropDown1.Items.Add($Item)
}
$Form.Controls.Add($DropDown1)
$DropDown1Label = new-object System.Windows.Forms.Label
$DropDown1Label.Location = new-object System.Drawing.Size(10,30)
$DropDown1Label.size = new-object System.Drawing.Size(100,20)
$DropDown1Label.Text = "Facility Initials:"
$Form.Controls.Add($DropDown1Label)
################################################################################
#Edit This item to change the DropDown Values
[array]$DropDownArray2 = "01", "02", "03"
# This Function Returns the Selected Value and Closes the Form
function Return-DropDown {
$script:Choice2 = $DropDown2.SelectedItem.ToString()
$Form.Close()
}
$DropDown2 = new-object System.Windows.Forms.ComboBox
$DropDown2.Location = new-object System.Drawing.Size(100,60)
$DropDown2.Size = new-object System.Drawing.Size(130,30)
ForEach ($Item in $DropDownArray2) {
[void] $DropDown2.Items.Add($Item)
}
$Form.Controls.Add($DropDown2)
$DropDown2Label = new-object System.Windows.Forms.Label
$DropDown2Label.Location = new-object System.Drawing.Size(10,60)
$DropDown2Label.size = new-object System.Drawing.Size(100,20)
$DropDown2Label.Text = "Building Number:"
$Form.Controls.Add($DropDown2Label)
################################################################################
#Button
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,130)
$Button.Size = new-object System.Drawing.Size(150,20)
$Button.Text = "Rename Computer"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
################################################################################
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
$FacilityInitials = $Choice
$BuildingNumber = $Choice2
$newCompName = $FacilityInitials + "-" + $BuildingNumber
$AdminAcc = ""
#Add Restart and Force later
Rename-Computer -NewName $newCompName -DomainCredential $AdminAcc