I am trying to build a simple gui form that has some buttons. So far the buttons I have created I would like to hide until the tab that they are on is clicked. Everything is working and this code is still a work in progress. I would like to keep comments about the code to just the situation about the buttons please.
# Functions
## Get Windows Product Key
### Written by Outside Source
function Get-WindowsProductKey([string]$computer)
{
$Reg = [WMIClass] ("\\" + $computer + "\root\default:StdRegProv")
$values = [byte[]]($reg.getbinaryvalue(2147483650,"SOFTWARE\Microsoft\Windows NT\CurrentVersion","DigitalProductId").uvalue)
$lookup = [char[]]("B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9")
$keyStartIndex = [int]52;
$keyEndIndex = [int]($keyStartIndex + 15);
$decodeLength = [int]29
$decodeStringLength = [int]15
$decodedChars = new-object char[] $decodeLength
$hexPid = new-object System.Collections.ArrayList
for ($i = $keyStartIndex; $i -le $keyEndIndex; $i++){ [void]$hexPid.Add($values[$i]) }
for ( $i = $decodeLength - 1; $i -ge 0; $i--)
{
if (($i + 1) % 6 -eq 0){$decodedChars[$i] = '-'}
else
{
$digitMapIndex = [int]0
for ($j = $decodeStringLength - 1; $j -ge 0; $j--)
{
$byteValue = [int](($digitMapIndex * [int]256) -bor [byte]$hexPid[$j]);
$hexPid[$j] = [byte] ([math]::Floor($byteValue / 24));
$digitMapIndex = $byteValue % 24;
$decodedChars[$i] = $lookup[$digitMapIndex];
}
}
}
$STR = ''
$decodedChars | % { $str+=$_}
$STR
}
### Start In-House Coding
# Start Form Build
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Form Layout Variables
$form = New-Object system.Windows.Forms.Form # Create Main Form
# Main Tab Creation
$tab1 = New-Object System.Windows.Forms.TabPage # Computer Information Tab
$tab2 = New-Object System.Windows.Forms.TabPage # OS Information Tab
$tab3 = New-Object System.Windows.Forms.TabPage # Memory Information Tab
$tab4 = New-Object System.Windows.Forms.TabPage # HDD Information Tab
$tab5 = New-Object System.Windows.Forms.TabPage # Network Information Tab
$tab6 = New-Object System.Windows.Forms.TabPage # Service Information Tab
$tabcontrol = New-Object System.Windows.Forms.TabControl
# Form Data Variables
## Output Textbox Variables
$pcoutput = New-Object System.Windows.Forms.TextBox # Computer Information Textbox
$osoutput = New-Object System.Windows.Forms.TextBox # OS Information Textbox
$memoutput = New-Object System.Windows.Forms.TextBox # Memory Information Textbox
$hddoutput = New-Object System.Windows.Forms.TextBox # HDD Information Textbox
$netoutput = New-Object System.Windows.Forms.TextBox # Network Information Textbox
$seroutput = New-Object System.Windows.Forms.RichTextBox # Service Information Textbox
## Output Button Variables
$exit = New-Object System.Windows.Forms.Button # Create Exit Button
$aservices = New-Object System.Windows.Forms.Button # Create Get All Services Button
$rservices = New-Object System.Windows.Forms.Button # Create Get Running Services Button
$sservices = New-Object System.Windows.Forms.Button # Create Get Stopped Services Button
## Output Label Variables
$authur = New-Object System.Windows.Forms.Label # Create Authur Label
## Computer Information
$pc = Get-WmiObject Win32_ComputerSystem # Get Computer Information
$pcmanuf = $pc.Manufacturer
$pcmodel = $pc.Model
$pcuser = $pc.UserName
## OS Information
$os = Get-WmiObject Win32_OperatingSystem # Get OS Information
$oscap = $os.Caption
$osarc = $os.OSArchitecture
$osorg = $os.Organization
$osuser = $os.RegisteredUser
$osname = $os.CSName
## Regular Variables
$pkey = Get-WindowsProductKey . # Get OS Product Key
$license = Get-WmiObject -query "Select * from SoftwareLicensingProduct Where LicenseStatus = 1" # Get All License Information With Status of 1
$memory = Get-WmiObject Win32_PhysicalMemory # Get Physical Memory Information
$memarray = Get-WmiObject Win32_PhysicalMemoryArray # Get Overall Physical Memory Information
$drives = Get-WmiObject -query "Select * from Win32_LogicalDisk Where DriveType = 3" # Get All Local HDDs
$netadapterconfig = Get-WmiObject -query "Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True" # Get All Network Adapter Information
$services = Get-WmiObject Win32_Service | sort DisplayName | select-object SystemName,DisplayName,StartMode,State,PathName # Get All Registered Services
$tbsize = New-Object System.Drawing.Size(725,400) # Textbox Size
$break = "==================================`r`n" # Line Break
$tbbcolor = "White" # Textbox Background Color
# Variables for Math
$bytes = "1073741824"
$bytes6 = "1000000" # Bytes 10^6
$kb = "1048576"
$mb = "1024"
# Convert Install Date to Something Readable
$instally = $os.InstallDate.Substring(0,4) # Get Year
$installm = $os.InstallDate.Substring(4,2) # Get Month
$installd = $os.InstallDate.Substring(6,2) # Get Day
$installh = $os.InstallDate.Substring(8,2) # Get Hours
$installmm = $os.InstallDate.Substring(10,2) # Get Minutes
$installs = $os.InstallDate.Substring(12,2) # Get Seconds
$install = "$installm/$installd/$instally $installh`:$installmm`:$installs" # Create Readable Date/Time Stamp
# Convert Last Reboot to Something Readable
$rebooty = $os.LastBootUpTime.Substring(0,4) # Get Year
$rebootm = $os.LastBootUpTime.Substring(4,2) # Get Month
$rebootd = $os.LastBootUpTime.Substring(6,2) # Get Day
$rebooth = $os.LastBootUpTime.Substring(8,2) # Get Hours
$rebootmm = $os.LastBootUpTime.Substring(10,2) # Get Minutes
$reboots = $os.LastBootUpTime.Substring(12,2) # Get Seconds
$reboot = "$rebootm/$rebootd/$rebooty $rebooth`:$rebootmm`:$reboots" # Create Readable Date/Time Stamp
# Size Conversions
$maxcapacity = $memarray.MaxCapacity / $memory.Count # Get memory max capacity and devide it by the number of slots
$maxcapacity = $maxcapacity / $kb # Convert memory max capacity to MB
# Get License Status
foreach($status in $license) {
if($status.LicenseStatus -eq 1) {
$lstatus = "Product Licensed Properly"
}
else {
$lstatus = "Product Not Licensed Properly"
}
}
# Button Functions
## Exit Button
$exit_OnClick = {
$form.Close() # Close Form
}
## Services Buttons
### Get All Services
$aservices_OnClick = {
$aservices = Get-WmiObject Win32_Service | sort DisplayName | select-object SystemName,DisplayName,StartMode,State,PathName # Get All Registered Services
$seroutput.Clear()
foreach($aservice in $aservices) {
$sername = $aservice.DisplayName
$sermode = $aservice.StartMode
$serstate = $aservice.State
$seroutput.Text += "Service Name: $sername`r`n"
$seroutput.Text += "Service Start Mode: $sermode`r`n"
$seroutput.Text += "Service State: $serstate`r`n"
$seroutput.Text += "-------------------------------`r`n"
}
}
### Get Running Services
$rservices_OnClick = {
$rservices = Get-WmiObject Win32_Service -Filter "State = 'Running'" | sort DisplayName
$seroutput.Clear()
foreach($rservice in $rservices) {
$sername = $rservice.DisplayName
$sermode = $rservice.StartMode
$serstate = $rservice.State
$seroutput.Text += "Service Name: $sername`r`n"
$seroutput.Text += "Service Start Mode: $sermode`r`n"
$seroutput.Text += "Service State: $serstate`r`n"
$seroutput.Text += "-------------------------------`r`n"
}
}
### Get Stopped Services
$sservices_OnClick = {
$sservices = Get-WmiObject Win32_Service -Filter "State = 'Stopped'" | sort DisplayName
$seroutput.Clear()
foreach($sservice in $sservices) {
$sername = $sservice.DisplayName
$sermode = $sservice.StartMode
$serstate = $sservice.State
$seroutput.Text += "Service Name: $sername`r`n"
$seroutput.Text += "Service Start Mode: $sermode`r`n"
$seroutput.Text += "Service State: $serstate`r`n"
$seroutput.Text += "-------------------------------`r`n"
}
}
# Form Layout
$form.Text = "Computer Information v0.2b" # Form Title
$form.AutoSize = $True # Turn on AutoSize
$form.AutoSizeMode = "GrowandShrink" # AutoSize Will Grow and Shrink Overall Form
# Tab 1 Data
$tab1.Text = "Computer Information" # Tab 1 Title
$tab1.Name = "TabPage1"
$tab1.TabIndex = 0 # Index Value
## Tab 1 Output
$pcoutput.MultiLine = $True # Create Textbox
$pcoutput.Size = $tbsize # Textbox Size
$pcoutput.ScrollBars = "Vertical" # Vertical Scrollbars
$pcoutput.Text = "Computer Information`r`n$break"
$pcoutput.Text += "Manufacturer: $pcmanuf`r`n" # PC Manufacturer
$pcoutput.Text += "Model: $pcmodel" # PC Model
$pcoutput.ReadOnly = $True # Textbox Ready-Only
$pcoutput.BackColor = "$tbbcolor" # Textbox Background Color
$tab1.Controls.Add($pcoutput) # Create Total Tab Output
# Tab 2 Data
$tab2.Text = "OS Information" # Tab 2 Title
$tab2.Name = "TabPage2"
$tab2.TabIndex = 1 # Index Value
# Tab 2 Output
$osoutput.MultiLine = $True # Create Textbox
$osoutput.Size = $tbsize # Textbox Size
$osoutput.ScrollBars = "Vertical" # Vertical Scrollbars
$osoutput.Text = "OS Information`r`n$break"
$osoutput.Text += "Operating System: $oscap $osarc`r`n" # Operating System and Architechure
$osoutput.Text += "Product Key: $pkey`r`n" # OS Product Key
$osoutput.Text += "License Status: $lstatus`r`n" # OS License Status
$osoutput.Text += "Install Date: $install`r`n" # OS Install Date
$osoutput.Text += "Last Reboot: $reboot`r`n" # OS Last Reboot
$osoutput.Text += "Organization: $osorg`r`n" # Registered Organizaton
$osoutput.Text += "Registered Owner: $osuser`r`n" # Registered User
$osoutput.Text += "Computer Name: $osname`r`n" # Current Computer Name
$osoutput.Text += "Current User: $pcuser" # Current User Number
$osoutput.ReadOnly = $True # Textbox Read-Only
$osoutput.BackColor = "$tbbcolor" # Textbox Background Color
$tab2.Controls.Add($osoutput) # Create Total Tab Output
# Tab 3 Data
$tab3.Text = "Memory Information" # Tab 3 Title
$tab3.Name = "TabPage3"
$tab3.TabIndex = 2 # Index Value
# Tab 3 Output
$memoutput.MultiLine = $True # Create Textbox
$memoutput.Size = $tbsize # Textbox Size
$memoutput.ScrollBars = "Vertical" # Vertical Scrollbars
$memoutput.Text = "Memory Information`r`n$break"
foreach($stick in $memory) { # Get Each Memory Module
# Convert Capacity
$capacity = $stick.Capacity / $bytes # Total Stick capacity converted to GB
$membank = $stick.DeviceLocator # Determine Which Slot Module is In
$memmanu = $stick.Manufacturer # Get Module Manafacturer
$mempn = $stick.PartNumber # Get Module Part Number
$memsn = $stick.SerialNumber # Get Module Serial Number
$memoutput.Text += "Location: $membank`r`n"
$memoutput.Text += "Manufacturer: $memmanu`r`n"
$memoutput.Text += "Part Number: $mempn`r`n"
$memoutput.Text += "Serial Number $memsn`r`n"
$memoutput.Text += "Capacity: $capacity GB`r`n"
$memoutput.Text += "Max Capacity: $maxcapacity GB`r`n"
$memoutput.Text += "-------------------------------`r`n"
}
$memoutput.ReadOnly = $True # Textbox Read-Only
$memoutput.BackColor = "$tbbcolor" # Textbox Background Color
$tab3.Controls.Add($memoutput) # Create Total Tab Output
# Tab 4 Data
$tab4.Text = "Hard Drive Information" # Tab 4 Title
$tab4.Name = "TabPage4"
$tab4.TabIndex = 3 # Index Value
# Tab 4 Output
$hddoutput.MultiLine = $True # Create Textbox
$hddoutput.Size = $tbsize # Textbox Size
$hddoutput.ScrollBars = "Vertical" # Vertical Scrollbars
$hddoutput.Text = "Hard Drive Information`r`n$break"
foreach($drive in $drives) {
$deviceid = $drive.DeviceID # HDD Letter
$size = $drive.Size / $bytes # HDD Max Size
$fspace = $drive.FreeSpace / $bytes # HDD Free Space
$uspace = $size - $fspace # Calculate HDD Used Space
$size = [System.Math]::round($size, 2) # Rounds Decimal Places
$fspace = [System.Math]::round($fspace, 2) # Rounds Decimal Places
$uspace = [System.Math]::round($uspace, 2) # Rounds Decimal Places
if($size -eq 0) {
# Filter out results with HDD Max Size of 0
""
}
else {
# Console Output
$hddoutput.Text += "Drive: $deviceid`r`n" # Get Drive Letter
$hddoutput.Text += "Total HDD Size: $size GB`r`n" # Get HDD Total Size
$hddoutput.Text += "Total HDD Free Space: $fspace GB`r`n" # Get HDD Free Space
$hddoutput.Text += "Total HDD Used Space: $uspace GB`r`n" # Get HDD Used Space
}
}
$hddoutput.ReadOnly = $True
$hddoutput.BackColor = "$tbbcolor"
$tab4.Controls.Add($hddoutput)
# Tab 5 Data
$tab5.Text = "Network Information"
$tab5.Name = "TabPage5"
$tab5.TabIndex = 4
# Tab 5 Output
$netoutput.MultiLine = $True
$netoutput.Size = $tbsize # Textbox Size
$netoutput.ScrollBars = "Vertical"
$netoutput.Text = "Network Adapter Information`r`n$break"
foreach($adapter in $netadapterconfig) {
# Console Output
$netdesc = $adapter.Description
$netipv4 = $adapter.IPAddress[0]
$netsub = $adapter.IPSubnet[0]
$netgate = $adapter.DefaultIPGateway
$netmac = $adapter.MACAddress
$netoutput.Text += "Name: $netdesc`r`n" # Get Network Adapter Name
$netoutput.Text += "IP Address: $netipv4`r`n" # Get IPv4 Address
$netoutput.Text += "Subnet Mask: $netsub`r`n" # Get Subnet
$netoutput.Text += "Gateway: $netgate`r`n" # Get Gateway
$netoutput.Text += "MAC: $netmac`r`n"# Get Mac Address
$netoutput.Text += "-------------------------------`r`n"
}
$netoutput.ReadOnly = $True
$netoutput.BackColor = "$tbbcolor"
$tab5.Controls.Add($netoutput)
# Tab 6 Data
$tab6.Text = "Services Information"
$tab6.Name = "TabPage6"
$tab6.TabIndex = 5
# Tab 6 Output
$seroutput.MultiLine = $True
$seroutput.Size = $tbsize # Textbox Size
$seroutput.ScrollBars = "Vertical"
$seroutput.Text = "Services Information`r`n$break"
foreach($service in $services) {
$sername = $service.DisplayName
$sermode = $service.StartMode
$serstate = $service.State
$seroutput.Text += "Service Name: $sername`r`n"
$seroutput.Text += "Service Start Mode: $sermode`r`n"
$seroutput.Text += "Service State: $serstate`r`n"
$seroutput.Text += "-------------------------------`r`n"
}
$seroutput.ReadOnly = $True
$seroutput.BackColor = "$tbbcolor"
$tab6.Controls.Add($seroutput)
# Tab Creation
$tabcontrol.Size = New-Object System.Drawing.Size(750,440)
$tabcontrol.Controls.Add($tab1)
$tabcontrol.Controls.Add($tab2)
$tabcontrol.Controls.Add($tab3)
$tabcontrol.Controls.Add($tab4)
$tabcontrol.Controls.Add($tab5)
$tabcontrol.Controls.Add($tab6)
$tabcontrol.SelectedIndex = 0
# Create All Services Button
$aservices.Name = "AllServices"
$aservices.Text = "All Services"
$aservices.Location = New-Object System.Drawing.Size(5,450)
$aservices.Width = 100
$aservices.add_Click($aservices_OnClick)
# Create Running Services Button
$rservices.Name = "RunningServices"
$rservices.Text = "Running Services"
$rservices.Location = New-Object System.Drawing.Size(105,450)
$rservices.Width = 100
$rservices.add_Click($rservices_OnClick)
# Create Stopped Services Button
$sservices.Name = "StoppedServices"
$sservices.Text = "Stopped Services"
$sservices.Location = New-Object System.Drawing.Size(205,450)
$sservices.Width = 100
$sservices.add_Click($sservices_OnClick)
# Exit Button Creation
$exit.Name = "Exit"
$exit.Text = "Exit"
$exit.Location = New-Object System.Drawing.Size(675,500)
$exit.add_Click($exit_OnClick)
# Authur Label Creation
$authur.Text = "Written By: Joseph Monarch"
$authur.AutoSize = $True
$authur.Location = New-Object System.Drawing.Size(5,500)
# Add All Controls
$form.Controls.Add($tabcontrol)
$form.Controls.Add($aservices)
$form.Controls.Add($rservices)
$form.Controls.Add($sservices)
$form.Controls.Add($authur)
$form.Controls.Add($exit)
# Create Form
$form.ShowDialog() | Out-Null
$aservices, $rservices and $sservices are the buttons I would like to hide until the Services Tab is clicked.
I have tried several things with no avail. Any help is much appreciated.