Author Posts

August 16, 2016 at 5:19 pm

So I am new to PowerShell so I have been tinkering a bit at work trying to see what all I can do, so I got a GUI script with some check boxes, modified it a bit and have them spit out some info. Right now I have it to where a user can type in a PC name and hit enter, it will tell him who is logged in, if he clicks a check box it will do what the checkbox says, if he hits the admin console button it will launch the MMC.

What I would like is to know how I can have all of the text output in the output box stay there and not clear when a button is clicked. I have it to where it will stack just the users logged in, but I would like it to stack all of the output no matter what is clicked.

Here is my code:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$tooltip1 = New-Object System.Windows.Forms.ToolTip

$Form = New-Object System.Windows.Forms.Form
$Form.Text = "I can't come up with a good name"
$Form.Size = New-Object System.Drawing.Size(200,200)
$form.AutoScalemode = "Dpi"
$Form.Autosize = $true
$Form.AutoSizeMode = "GrowAndShrink"

############################################## Start functions

function userInfo {

#Check Boxes
if ($RadioButton1.Checked -eq $true) {Connect-RDP}
If ($RadioButton2.Checked -eq $true) {GetOsInfo}
If ($RadioButton3.Checked -eq $true) {GetCmd}
If ($RadioButton1.Checked -eq $false -AND $RadioButton2.Checked -eq $false -AND $RadioButton3.Checked -eq $false) {CurrentLoggedUser}
} #End Check Boxes

#Get Username of Current Logged in User
function CurrentLoggedUser {
$computer=$InputBox.text;
$useron=gwmi win32_computersystem -comp $computer -erroraction 'silentlycontinue' | select -ExpandProperty UserName;

#Courtesy of reddit user wahoorider
if ($outputBox.Text) {
$outputbox.Text = $outputbox.Text + [System.Environment]::NewLine + $useron
} else {
$outputbox.Text = $useron}
} #Current User
#Start RDP
function Connect-RDP {
$computer=$InputBox.text
$outputBox.text= "Running RDP"
mstsc.exe /v $computer /f
}
#End RDP

#Admin Console MMC Calls Batch – Checks for MMC if it exists opens it, if not creates new
function adminConsole {
$scriptpath = $MyInvocation.MyCommand.Path
$dir = $PSScriptRoot
start-process "$dir\tools\runconsole.bat"
$outputBox.text= "Admin Console will now start in new window"
} #end Admin Console MMC

#Get Uptime.
function GetOsInfo {
$computer=$InputBox.text
$CSinfo = Get-CimInstance -classname Win32_OperatingSystem -ComputerName $computer -ErrorAction 'silentlycontinue' | Select-Object -expandproperty LastBootUpTime
$outputbox.Text = "Uptime For: $computer – $CSinfo".ToUpper()
} #End Uptime

#Launch Command Prompt
function GetCmd {
Invoke-Item C:\Windows\System32\cmd.exe
$outputbox.Text = "Launching Command Prompt"
} #End Command Prompt

#Clear Output Box Button
function ClearOutputBox {
$outputbox.Text = ""
}
#End Clear Button

############################################## end functions

############################################## Start group boxes

$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(270,25)
$groupBox.size = New-Object System.Drawing.Size(80,80)
$groupBox.text = "Options:"
$Form.Controls.Add($groupBox)

############################################## end group boxes

############################################## Start radio buttons

$RadioButton1 = New-Object System.Windows.Forms.checkbox
$RadioButton1.Location = new-object System.Drawing.Point(15,15)
$RadioButton1.size = New-Object System.Drawing.Size(75,20)
$RadioButton1.Checked = $false
$RadioButton1.Text = "RDP"
$tooltip1.SetToolTip($RadioButton1, "RDP Into Machine")
$groupBox.Controls.Add($RadioButton1)

$RadioButton2 = New-Object System.Windows.Forms.checkbox
$RadioButton2.Location = new-object System.Drawing.Point(15,35)
$RadioButton2.size = New-Object System.Drawing.Size(75,20)
$RadioButton2.Checked = $false
$RadioButton2.Text = "Uptime"
$tooltip1.SetToolTip($RadioButton2, "Get Uptime of PC")
$groupBox.Controls.Add($RadioButton2)

$RadioButton3 = New-Object System.Windows.Forms.checkbox
$RadioButton3.Location = new-object System.Drawing.Point(15,55)
$RadioButton3.size = New-Object System.Drawing.Size(75,20)
$RadioButton3.Checked = $false
$RadioButton3.Text = "CMD"
$tooltip1.SetToolTip($RadioButton3, "Launch Command Prompt")
$groupBox.Controls.Add($RadioButton3)

############################################## end radio buttons

############################################## Start text fields

$InputBox = New-Object System.Windows.Forms.TextBox
$InputBox.Location = New-Object System.Drawing.Size(20,50)
$InputBox.Size = New-Object System.Drawing.Size(150,20)
$Form.Add_Shown({$Form.Activate(); $InputBox.focus()})
$Form.Controls.Add($InputBox)

$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10,150)
$outputBox.Size = New-Object System.Drawing.Size(565,200)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Vertical"
$Form.Controls.Add($outputBox)

############################################## end text fields

############################################## Start buttons

$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(400,30)
$Button.Size = New-Object System.Drawing.Size(80,80)
$Button.Text = "Send"
$Button.Add_Click({userInfo})
$Form.Controls.Add($Button)

$ClearOutputButton = New-Object System.Windows.Forms.Button
$ClearOutputButton.Location = New-Object System.Drawing.Size(10,128)
$ClearOutputButton.Size = New-Object System.Drawing.Size(40,17)
$ClearOutputButton.Text = "Clear"
$ClearOutputButton.Add_Click({ClearOutputBox})
$tooltip1.SetToolTip($ClearOutputButton, "Clear The Output Box")
$Form.Controls.Add($ClearOutputButton)

############################################## end buttons

############################################## Start admin console buttons

$Button2 = New-Object System.Windows.Forms.Button
$Button2.Location = New-Object System.Drawing.Size(500,30)
$Button2.Size = New-Object System.Drawing.Size(80,80)
$Button2.Text = "Launch Admin Console"
$tooltip1.SetToolTip($Button2, "Launch MMC")
$Button2.Add_Click({adminConsole})
$Form.Controls.Add($Button2)

############################################## end buttons

$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

Also, any feedback helps if there is anything anyone sees that could be changed that would be great. Like I said still learning so I am up for suggestions.

August 16, 2016 at 6:15 pm

Use a listbox and append to it. Textbox isn't really the ideal control for this, plus limited to 32676 characters.

  • This reply was modified 2 years, 2 months ago by  Dan Potter.

August 16, 2016 at 7:25 pm

These lines:

$outputbox.Text = "Uptime For: $computer – $CSinfo".ToUpper()

Are setting the text box contents. Using += instead of = would make them append, rather than overwriting.

August 24, 2016 at 2:16 pm

That works fantastic thank you!

Sorry for the delayed response I was on vacation for a few days. I just added the =+ and [System.Environment]::NewLine to put them on a new line and it works great!