繁体   English   中英

Powershell Winforms函数输出到文本框

[英]powershell winforms function output to textbox

我正在使用我正在构建的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 = ''
#

任何帮助是极大的赞赏!

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM