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()
CodePudding user response:
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
}
)