如何显示PowerShell输出中PowerShell表单文本框数据的输出



我写了一个powershell脚本来ping一个ip地址,我想在powershell表单textboxt中显示一个ping结果。我尝试了下面的脚本。脚本正在工作,但未清除输出。

在这里,我附加了powershell输出和powershell表单文本框输出。

[PowerShell输出][1]

# Load required assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Create Form to contain elements
$Form = New-Object System.Windows.Forms.Form
# Set Form Titlebar text
$Form.Text = "PING"
# Set size of form
# Size(<length>,<height>) in pixels
$Form.Size = New-Object System.Drawing.Size(600,600)

# Create an Input textbox
$inputBoxui = New-Object System.Windows.Forms.TextBox
$inputBoxui.Location = New-Object System.Drawing.Size(20,50)
$inputBoxui.Size = New-Object System.Drawing.Size(100,20)
# Initialize the textbox inside the Form
$Form.Controls.Add($inputBoxui)

# Create Instruction Label for inputBox
$Labelui = New-Object System.Windows.Forms.Label
$Labelui.Text = "Enter IP Address"
$Labelui.Location = New-Object System.Drawing.Size(20,30)
$Labelui.BackColor = "Transparent"
$Labelui.AutoSize = $true
# Initialize Label
$Form.Controls.Add($Labelui)
# Create an Output textbox, 10 pixels in from Form Boundary and 150 pixels down
# As we want a multiline output set textbox size to 565 px x 200 px
# .Multiline declares the textbox is multi-line
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10,250)
$outputBox.Size = New-Object System.Drawing.Size(565,300)
$outputBox.MultiLine = $True
$outputBox.Scrollbars = "Vertical"
# Initialize the textbox inside the Form
$Form.Controls.Add($OutputBox)


# Add a Button which can be used to generate an action from our textboxes
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(20,80)
$Button.Size = New-Object System.Drawing.Size(100,40)
$Button.Text = "PING Start"
# Declare the action to occur when button clicked
$Button.Add_Click( { pingstart } )
# Initialize the button inside the Form
$Form.Controls.Add($Button)
# Create a Function
function pingstart {
$outputBox.Clear()
# Variable to store what user types into Input textbox
$Inputui = $inputBoxui.Text
while ($true) {
$Computer = $Inputui
$Ping = Test-Connection -Count 3 -ComputerName $Computer
ForEach ($Result in $Ping) {
If ($Result.ResponseTime -lt 100) {
$Result | Select-Object -Property Address,BufferSize,ResponseTime | Write-Host -BackgroundColor Green
}
If ( ($Result.ResponseTime -ge 100) -and ($Result.ResponseTime -lt 200) ) {
$Result | Select-Object -Property Address,BufferSize,ResponseTime | Write-Host -BackgroundColor Yellow
}
If ($Result.ResponseTime -ge 200) {
$Result | Select-Object -Property Address,BufferSize,ResponseTime | Write-Host -BackgroundColor Red
}
}
# Assign Result to OutputBox
$outputBox.Text = $Ping
}
}
# Initialize form and show it
# [void] used to suppress other messages generated by Form actions
[void] $Form.ShowDialog()  

不相关,但这个似乎有一个错误

初始化表单内的文本框

$Form.Controls.Add($OutputBox(

盖帽锁定。$outputBox=新对象System.Windows.Forms.TextBox

相关内容

  • 没有找到相关文章

最新更新