与形式Powershell GUI交互的潜在响应

时间:2017-11-25 16:36:49

标签: powershell

所以我已经完成了假期休假个人项目。

我有一个在设定的时间间隔内刷新的表单(解决了最后一个问题将开始创建配置文件)

在我添加on_tick函数和允许我想要的功能的加载函数之前,程序运行正常,如果不是我想要的结果。当我的意思是跑得很好我的意思是,如果我点击关闭(" X"右上角的按钮)或想要移动窗口我可以

在我添加修复后,表单窗口现在没有响应或者将永远注册"关闭"命令

########################################################################
#Purpose to have a UI running at a 30 second intervel to update on 
#network and machine performance metrics 
########################################################################
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
    $timer = New-Object System.Windows.Forms.Timer
    $timer.Interval = 30000
    $form.ForeColor = "#25c420"
    $form.BackColor = "#000000"

    #tick function - code that actually gets the metrics all other code is for the silly gui
    Function Timer_Tick(){
            $date  = Get-Date -Format "HH:mm:ss"
            $form.Text = "Network and Health Monitor ($date)"
            $outputBox.ForeColor = "#25c420"
            $outputBox.BackColor = "#000000"
            $outputBox.Text += "`n"
            $pySpeedDir = "C:\Users\CentralTerminal\AppData\Local\Programs\Python\Python36\Scripts\pyspeedtest.exe"
            $speed = & $pySpeedDir
            $table = Get-ComputerStats | Select-Object ComputerName,AverageCpu,MemoryUsage,PercentFree | Out-String
            #$outputBox.Text += get-date
            $outputBox.Text += "$date`n"
            $outputBox.Text += "CPU and Memory Utilization`n"
            $outputBox.Text += $table
            $outputBox.Text += "`r`n"
            $outputBox.Text += "Network Test in Progress...`n"
            $outputBox.Text += $speed[0]+ "`n" + $speed[1] + "`n" + $speed[2] + "`n" + $speed[3]
            $outputBox.Text += "`r`n"
            $outputBox.Select()
            $outputBox.SelectionStart = $outputBox.Text.Length
            $outputBox.ScrollToCaret()
            $outputBox.Refresh()
    }

    Function Form_Load{
        #$form.Text = "Timer started"
        Timer_Tick
        $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)
    $form.BackgroundImageLayout = "None"
    $form.add_Load({Form_Load})
    $timer.Add_Tick({Timer_Tick})


    $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
}

我的问题是我没有被抛出的例外。我将在这个系统中添加一个try catch,但是没有任何错误消息或任何技术上让我开始调查的东西。如果你也知道从哪里开始调试也会有所帮助,因为我想在生产支持方面做得更好

感谢您阅读

0 个答案:

没有答案