With the below code, my goal is to ping a hostname and have it return if successful or unsuccessful, and project that on the GUI screen itself. At the moment I am getting errors on the "-TargetName".
Add-Type -assembly System.Windows.Forms
# Create window form
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text = 'Device Info'
$main_form.Width = 600
$main_form.Height = 400
$main_form.AutoSize = $true
# Start of code
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Hostname"
$Label.Location = New-Object System.Drawing.Point(10, 30)
$Label.AutoSize = $true
$main_form.Controls.Add($Label)
# Adding textbox
$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Location = New-Object System.Drawing.Point(90, 30)
$TextBox.Width = 300
$main_form.Controls.Add($TextBox)
# Adding check button
$CheckButton = New-Object System.Windows.Forms.Button
$CheckButton.Location = New-Object System.Drawing.Size(400, 30)
$CheckButton.Size = New-Object System.Drawing.Size(120, 23)
$CheckButton.Text = "Check"
$main_form.Controls.Add($CheckButton)
# Perform event when check button is clicked
$ComputerName = $TextBox.Text
$CheckButton.Add_Click(
{
Test-Connection -TargetName $ComputerName -IPv4
}
)
# Show Window
$main_form.ShowDialog()
You need to grab the value from $TextBox.Text when the button is clicked, and then use the correct parameter name (-ComputerName rather than -TargetName)!
Change this:
$ComputerName = $TextBox.Text
$CheckButton.Add_Click(
{
Test-Connection -TargetName $ComputerName -IPv4
}
)
To:
$CheckButton.Add_Click(
{
$ComputerName = $TextBox.Text
Test-Connection -ComputerName $ComputerName -IPv4
}
)
Related
This what I have so far it brings up a Syntax screen for wusa. I have confirmed that the Trim is working. If I leave out the remote computer name is works on the local computer. I will be adding this to a much larger script just trying to get this working before trying to add it.
<#
.NAME
Template
#>
$comp = "Remote Pc Name Goes Here"
$str = $Hotfix_TextBox.Text. Trim("K","B")
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(400,400)
$Form.text = "Form"
$Form.TopMost = $false
$gethotfix = New-Object system.Windows.Forms.Button
$gethotfix.text = "Get Hotfixes"
$gethotfix.width = 120
$gethotfix.height = 30
$gethotfix.location = New-Object System.Drawing.Point(100,81)
$gethotfix.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$removehotfix = New-Object system.Windows.Forms.Button
$removehotfix.text = "Remove Hotfix"
$removehotfix.width = 120
$removehotfix.height = 30
$removehotfix.location = New-Object System.Drawing.Point(100,120)
$removehotfix.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Hotfix_TextBox = New-Object system.Windows.Forms.TextBox
$Hotfix_TextBox.Text = ""
$Hotfix_TextBox.multiline = $false
$Hotfix_TextBox.width = 174
$Hotfix_TextBox.height = 20
$Hotfix_TextBox.location = New-Object System.Drawing.Point(12,235)
$Hotfix_TextBox.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
<#
$Trimmed_TextBox = New-Object system.Windows.Forms.TextBox
#$Trimmed_TextBox.Text = "$str"
$Trimmed_TextBox.multiline = $false
$Trimmed_TextBox.width = 174
$Trimmed_TextBox.height = 20
$Trimmed_TextBox.location = New-Object System.Drawing.Point(12,265)
$Trimmed_TextBox.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
#>
$Form.controls.AddRange(#($gethotfix,$removehotfix,$Hotfix_TextBox))
$gethotfix.Add_Click({ GetHotfix })
$removehotfix.Add_Click({ RemoveHotfix })
#region Logic
function GetHotfix {$Hotfix_TextBox.Text = Get-Hotfix -Computername $comp |
Select-Object -ExpandProperty 'HotFixID'|
Out-GridView -Title 'Installed Hotfixes' -PassThru }
#$Hotfix_TextBox.Text. Trim("K","B")
#$Hotfix_TextBox.Text = "$str"
function RemoveHotfix{
#$Trimmed_TextBox.Text = "$str"
$comp = "dus-xtdfed9r386"
#Uninstall-HotFix -ComputerName $comp
wusa -computername /$comp | /uninstall | /kb:$str
}
#endregion
[void]$Form.ShowDialog()
This turned out to be a fiasco. Even when I could get it to work there are many KBs that cannot be removed, as well as we had policies that would not allow us to remove many others. Just not worth the effort. Perhaps someone with less strengent policies in place can do something with this. Have Fun.
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()
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
I have observed a difference in behavior between PowerShell 2.0 code and 3.0 code but can't seem to fix it. I am hoping I can get some help. Please see code below.
If you run this in V2 (please change ServerA and ServerB to a server on your network) you will see after selecting and clicking the button, write-host shows them as seperate names. In version 3 the listbox populates it as 1.
$Servers = #()
[array]$ServerstoPing = ("ServerA","ServerB")
# Generate the form
Add-Type -AssemblyName System.Windows.Forms
$Form1 = New-Object system.Windows.Forms.Form
$Form1.Text = "Ping Some Servers"
$Font = New-Object System.Drawing.Font("Calibri",18,[System.Drawing.FontStyle]::regular)
$Fontboxes = New-Object System.Drawing.Font("Lucida Sans",8,[System.Drawing.FontStyle]::regular)
$Fontdate = New-Object System.Drawing.Font("Lucida Sans",11,[System.Drawing.FontStyle]::bold)
$FontProgress = New-Object System.Drawing.Font("Lucida Sans",11,[System.Drawing.FontStyle]::bold)
$Form1.Font = $Font
$Serverlistbox = New-Object System.Windows.Forms.Listbox
$Serverlistbox.Location = New-Object System.Drawing.Size(11,137)
$Serverlistbox.Size = New-Object System.Drawing.Size(200,170)
$Serverlistbox.SelectionMode = "MultiExtended"
$Serverlistbox.font = $Fontboxes
foreach($item in $ServerstoPing){
[void] $Serverlistbox.Items.Add("$item")
}
$Form1.Controls.Add($Serverlistbox)
$SubmitButton = New-Object System.Windows.Forms.button
$SubmitButton.text = "Ping Servers"
$SubmitButton.Location = New-Object System.Drawing.Size(171,305)
$SubmitButton.AutoSize = $True
$Form1.Controls.Add($SubmitButton)
$Form1.StartPosition = "CenterScreen"
$OutputPing = New-Object System.Windows.Forms.TextBox
$OutputPing.MultiLine = $True
$OutputPing.ScrollBars = "Vertical"
$OutputPing.text = "Server ping status will be displayed here once done'."
$OutputPing.font = $Fontboxes
$OutputPing.Location = New-Object System.Drawing.Size(226,40)
$OutputPing.Size = New-Object System.Drawing.Size(200,255)
$Form1.Controls.Add($OutputPing)
$SubmitButton.add_click({
#foreach ($objItem in $ServerstoPing)
foreach ($objItem in $Serverlistbox.SelectedItems)
{
$Servers += $objItem
}
write-host $ServerstoPing
Foreach($server in $servers)
{
ping -n 1 -w 1000 $server | out-null
if($? -eq $true){
write-host ("$server ping success")
}
else
{
write-host ("$server did not respond to ping, not extracting events")
}
}
})
$Form1.ShowDialog()
If I give one system, it is working. If I give multiple systems it is showing RPC error. If I give my system twice, then it also is not working. Any idea?
Function Get_Systeminfo() {
$TxtBox.Visible = $false
$ColName = #{Expression={$_.CSNAME};Label="SERVER NAME"},
#{Expression={$_.Caption};Label="OS NAME"; width =25},
#{Expression={$_.OSArchitecture};Label="OS TYPE"}
$ResBox = New-Object System.Windows.Forms.RichTextBox
$ResBox.Location = New-Object System.Drawing.Size(50,50)
$ResBox.Size = New-Object System.Drawing.Size(480, 280)
$ResBox.Font = "lucida console"
$objForm.Controls.Add($ResBox)
$ResBox.Text = foreach ($list in $TxtBox.Text) {
Get-WmiObject Win32_OperatingSystem -ComputerName $list |
Format-Table $ColName -Auto | Out-String
}
}
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "SystemInfo"
$objForm.Size = New-Object System.Drawing.Size(600,400)
$objForm.StartPosition = "CenterScreen"
$TxtBox = New-Object System.Windows.Forms.TextBox
$TxtBox.Location = New-Object System.Drawing.Size(20, 20)
$TxtBox.Size = New-Object System.Drawing.Size(300,100)
$TxtBox.Multiline = $true
$objForm.Controls.Add($TxtBox)
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(20,340)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$objForm.Controls.Add($OKButton)
$OKButton.Add_Click({Get_Systeminfo})
$objForm.ShowDialog()
Change
$ResBox.Text = foreach ($list in $TxtBox.Text){
to
$ResBox.Text = foreach ($list in $TxtBox.Lines){
and it will genereate output, IF you are giving a single name per line.