powershell winforms函数输出到文本框

时间:2018-12-12 03:58:33

标签: winforms powershell

我正在寻找有关正在构建的Powershell GUI的帮助。我有一个轮询远程计算机带宽的函数。我试图弄清楚如何使其输出到文本框。我目前仅通过单独运行此功能而获得的回报如下。我只想将值显示在文本框中

InstanceName                             value
------------                             -----
intel[r] i350 gigabit network connection 11.85

该函数的最后一行还将启动循环过程(启动监视),并每10秒运行一次。我在Powershell GUI中还有其他功能,并且“启动监视”不在功能范围内,因此无法正常工作。我认为从底部开始监视启动并将其附加到文本框将解决此问题

Function Start-Monitoring
{
    $Username = 'domain\user'
    $Password = 'password'
    $pass = ConvertTo-SecureString -AsPlainText $Password -Force

    $SecureString = $pass
    # Uses your password securely
    $MySecureCreds = New-Object -TypeName 
    System.Management.Automation.PSCredential -ArgumentList $Username, $SecureString


        While ($true)
    {
                # Do things lots
                Invoke-command {Get-Counter -Counter "\Network Interface(intel[r] i350 gigabit network connection)\Bytes Received/sec" -sampleinterval 6 |select -exp countersamples|ft -a instancename,@{l="value";e={[math]::round($_.cookedvalue/.1MB,2)}}} -Credential $MySecureCreds -Verbose -ComputerName ipaddress

                # Add a pause so the loop doesn't run super fast and use lots of CPU 
                Start-Sleep 10
    }
}
Start-Monitoring

这是我为此创建的文本框

#BandTextBox
#
$BandTextBox.Location = '175, 75'
$BandTextBox.Name = 'BandTextBox'
$BandTextBox.Size = '40, 20'
$BandTextBox.TabIndex = 4
$BandTextBox.Text = ''
#

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

将输出保存在变量中并更新文本框。

Function Start-Monitoring
{
    $Username = 'domain\user'
    $Password = 'password'
    $pass = ConvertTo-SecureString -AsPlainText $Password -Force

    $SecureString = $pass
    # Uses your password securely
    $MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $SecureString


    While ($true) {
            # Do things lots
            Invoke-command {Get-Counter -Counter "\Network Interface(intel[r] i350 gigabit network connection)\Bytes Received/sec" -sampleinterval 6 |select -exp countersamples|ft -a instancename,@{l="value";e={[math]::round($_.cookedvalue/.1MB,2)}}} -Credential $MySecureCreds -Verbose -ComputerName ipaddress

            # Add a pause so the loop doesn't run super fast and use lots of CPU 
            Start-Sleep 10
    }
}
$Output = Start-Monitoring


$BandTextBox.Location = '175, 75'
$Ban1dTextBox.Name = 'BandTextBox'
$BandTextBox.Size = '40, 20'
$BandTextBox.TabIndex = 4
$BandTextBox.Text = $Output.Value