PowerShell-处理Windows表单事件



作为后端脚本/devops的人,我并不像我曾经是JR软件工程师那样熟悉的.NET。但是,我喜欢指标,并想到为我的VPN和CPU和RAM使用编写网络监视器,因为某些网站已导致Google泄漏内存,直到我在CPU和RAM

的99%之前,我才注意到它。

所以我有这个脚本,我决定也尝试制作GUI,因为您知道每个人都想要一个完整的堆栈开发人员。

我无法确定如何使计时器和事件对象在形式负载上发射以创建我的准无限循环

我一直在使用此源来了解事件计时器和注册的对象

########################################################################
# Modified by Derek from source below. All I did was the psexec query and cleaned it up a bit.
# Original basis for gui updating created by:
# Code Generated By: SAPIEN Technologies, Inc., PrimalForms 2009 v1.1.10.0
# Generated On: 23/10/2010 08:37
# Generated By: KST
# http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/780aaa2e-4edf-495b-a63e-d8876eab9b25
########################################################################
function OnApplicationLoad {
    return $true 
}
function OnApplicationExit {
    $script:ExitCode = 0 
}
function Get-ComputerStats {
    process {
        $avg = Get-WmiObject win32_processor | 
            Measure-Object -property LoadPercentage -Average | 
            Foreach {$_.Average}
        $mem = Get-WmiObject win32_operatingsystem |
            Foreach {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory) * 100) / $_.TotalVisibleMemorySize)}
        $free = Get-WmiObject Win32_Volume -Filter "DriveLetter = 'C:'" |
            Foreach {"{0:N2}" -f (($_.FreeSpace / $_.Capacity) * 100)}
        [pscustomobject] [ordered] @{ 
            ComputerName = $env:computername
            AverageCpu   = $avg
            MemoryUsage  = $mem
            PercentFree  = $free
        }
    }
}
function GenerateForm {
    [void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    [void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
    [void][reflection.assembly]::Load("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    [void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $form = New-Object System.Windows.Forms.Form
    $button = New-Object System.Windows.Forms.Button
    $outputBox = New-Object System.Windows.Forms.RichTextBox
    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
    $formEvent_Load = {
        $Timer = New-Object -Type Timers.Timer
        $Timer.Interval = 30000
        $timer.AutoReset = $true
        $timeout = 0
        $handler = {
            $pySpeedDir = "C:UsersCentralTerminalAppDataLocalProgramsPythonPython36Scriptspyspeedtest.exe"
            $speed = & $pySpeedDir
            $table = Get-ComputerStats 
            $outputBox.Text += get-date
            $outputBox.Text += "`n"
            $outputBox.Text += $speed
            $outputBox.Text += $table
            $outputBox.Select()
            $outputBox.SelectionStart = $outputBox.Text.Length
            $outputBox.ScrollToCaret()
            $outputBox.Refresh()
            $Timer.Stop()
            sleep -s 1  
        }
        $start = Register-ObjectEvent -InputObject $timer -SourceIdentifier TimerElapsed -EventName Elapsed -Action $handler
        $Timer.Start()
    }

    $form_StateCorrection_Load =
    {
        $form.WindowState = $InitialFormWindowState
    }

    $form.Controls.Add($outputBox)
    $form.Text = "Network and Machine Load"
    $form.Name = "GUM"
    $form.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation 
    $form.ClientSize = New-Object System.Drawing.Size(800, 400)
    #$Icon = New-Object system.drawing.icon ("brandimage.ICO")
    #$form.Icon = $Icon
    #$Image = [system.drawing.image]::FromFile("poweredbyit.jpg")
    #$form.BackgroundImage = $Image
    $form.BackgroundImageLayout = "None"
    $form.add_Load($formEvent_Load)

    $outputBox.Name = "outputBox"
    $outputBox.Text = ""
    $outputBox.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation 
    $outputBox.Location = New-Object System.Drawing.Point(5, 35)
    $outputBox.Size = New-Object System.Drawing.Size(785, 320) 
    $outputBox.font = "lucida console"
    $outputBox.TabIndex = 0
    $InitialFormWindowState = $form.WindowState
    $form.add_Load($form_StateCorrection_Load)
    return $form.ShowDialog()
} 
if (OnApplicationLoad -eq $true) {
    GenerateForm | Out-Null
    OnApplicationExit
}

自从我发布以来已经有一段时间了,所以我为格式不佳的不良格式表示歉意。感谢您查看

要注册GUI对象的事件,您可以使用以下任何选项:

•选项1

$form.Add_Load({$form.Text = "Form Load Event Handled!"})

•选项2

$Form_Load = {$form.Text = "Form Load Event Handled!"}    
$form.Add_Load($Form_Load)

•选项3

$form.Add_Load({Form_Load})
Function Form_Load
{
    $form.Text = "Form Load Event Handled!"
}

示例

如果您要从计时器事件中更新GUI,请使用 System.Windows.Forms.Timer并处理其Tick事件:

Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Size = "400, 400"
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
Function Timer_Tick()
{
    $form.Text = Get-Date -Format "HH:mm:ss"
}
Function Form_Load
{
    $form.Text = "Timer started"
    $timer.Start()
}
$form.Add_Load({Form_Load})
$timer.Add_Tick({Timer_Tick})
$form.ShowDialog()

最新更新