【问题标题】:How to Display output to PowerShell form textbox data from PowerShell output如何从 PowerShell 输出显示输出到 PowerShell 表单文本框数据
【发布时间】:2022-07-23 20:10:42
【问题描述】:

我已经编写了一个 powershell 脚本来 ping 一个 IP 地址,我想将一个 ping 结果显示到 powershell 表单文本框。我在脚本下面尝试了以下脚本。脚本正在运行,但输出不清晰。

在此我附上了 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()  

【问题讨论】:

  • 看起来 $outputBox.Text = $Ping 由于 while($true) 循环而无法访问。

标签: powershell


【解决方案1】:

不相关,但似乎有错误

初始化表单内的文本框

$Form.Controls.Add($OutputBox)

大写锁定。 $outputBox = 新对象 System.Windows.Forms.TextBox

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多