locked
Powershell 3.0 Unable to get Test-connection Monitor Form to update picture box RRS feed

  • Question

  • Good Day. This is my first post here and I am not a Powershell expert by a long shot. I have researched this fro several weeks and asked on other forums but no one seems to be able to help me. I figured that Scripting Guys from Microsoft should be able to give me pointers. I created a script (using a GUI) that checks if a group of servers are up, using test-connection.  The script creates labels in a form (GUI) from a source file (server names) and puts a yellow status box (image) in front of each server name.  When I click on the Start button, it should replace/update the yellow box to a green box is the server responds and red if it does not.  I know that I have to assign each picturebox it's own variable to be able to modify it depending on the test-connection results.  My main problem is that I can't wrap around my head on how to do this without having to set them manually.  Can someone help or at least let me know if it is feasible or not?  Thanks in advance.

    Here is my code (not perfect I know):

    # Load modules to be able to create Windows Forms (GUI)
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    # Hide or show PowerShell host (console) funcrion
    $script:showWindowAsync = Add-Type –memberDefinition @” 
    [DllImport("user32.dll")] 
    public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); 
    “@ -name “Win32ShowWindowAsync” -namespace Win32Functions –passThru
    
    function Show-PowerShell() { 
        $null = $showWindowAsync::ShowWindowAsync((Get-Process –id $pid).MainWindowHandle, 10) 
    }
    function Hide-PowerShell() { 
        $null = $showWindowAsync::ShowWindowAsync((Get-Process –id $pid).MainWindowHandle, 2) 
    }
    
    # Minimize PowerShell Host
    Hide-PowerShell
    
    CLS
    $array = @()
    $Targetdir = $PSScriptRoot
    $InputFile = "$Targetdir\ServerListShort.ini"
    $OutputFile = "$Targetdir\PingResults_PS.txt"
    
    If (Test-Path $OutputFile){
    	Remove-Item $OutputFile
    }
    Write-Output "Ping Report started: $(Get-Date -Format 'hh:mm:ss, dd/MM/yyyy')" >> $OutputFile
    Write-Output `n >> $OutputFile
    
    $imgGreenButton = [System.Drawing.Image]::Fromfile("$Targetdir\Green_Button.png")
    $imgRedButton = [System.Drawing.Image]::Fromfile("$Targetdir\Red_Button.png")
    $imgYellowButton = [System.Drawing.Image]::Fromfile("$Targetdir\Yellow_Button.png")
    
    $array = get-content $InputFile
    
    Function Close {
    $Form.Close()
    }
    
    Function PingServers {
    $Col = 5
    $Line = 52
    $StartButton.Enabled = $False
    ForEach ($ServerName In $array) {
    	if (test-Connection -ComputerName $ServerName -Count 2 -Quiet ) {	    
            $pictureBox = New-object Windows.Forms.PictureBox        
            $pictureBox.Width = $imgGreenButton.Size.Width 
            $pictureBox.Height = $imgGreenButton.Size.Height
            $pictureBox.Location = New-Object System.Drawing.Point ($Col,$Line)    
            $pictureBox.Image = $imgGreenButton
            $Form.controls.add($pictureBox)        
            Write-Output "$ServerName		: Online" >> $OutputFile        
    	    }
    	else {        
            $pictureBox = New-object Windows.Forms.PictureBox        
            $pictureBox.Width = $imgRedButton.Size.Width 
            $pictureBox.Height = $imgRedButton.Size.Height
            $pictureBox.Location = New-Object System.Drawing.Point ($Col,$Line)    
            $pictureBox.Image = $imgRedButton
            $Form.controls.add($pictureBox)
            Write-Output "$ServerName		: *** Not responding ***" >> $OutputFile
            }    
        $Line+=30
      if ($Line -ge 650) {    
        $Col += 175
        $Line = 52
        }
      }
    write-host    ""
    Write-Output `n >> $OutputFile
    Write-Output "Ping Report ended: $(Get-Date -Format 'hh:mm:ss, dd/MM/yyyy')" >> $OutputFile
    $Form.Controls.Remove($StartButton)
    $CloseButton = New-Object System.Windows.Forms.Button
    $CloseButton.Location = New-Object System.Drawing.Point (160,5)
    $CloseButton.Size = New-Object System.Drawing.Size(75,23)
    $CloseButton.Text = "Close"
    $CloseButton.Add_Click({Close})
    $Form.Controls.Add($CloseButton)
    Invoke-Item "$Targetdir\PingResults_PS.txt"
    }
    
        $Form = New-Object System.Windows.Forms.Form
        $Form.Text = "Ping Server Monitor"
        $Form.AutoSize = $True
        $Form.AutoSizeMode = "GrowAndShrink"
        $Form.StartPosition = "CenterScreen"
        $Image = [system.drawing.image]::FromFile("$Targetdir\Gray_Wallpaper.jpg")
        $Form.BackgroundImage = $Image
        $Form.BackgroundImageLayout = "Stretch"
    
        $Label = New-Object System.Windows.Forms.Label
        $Label.Font = New-Object System.Drawing.Font("Times New Roman",18, [System.Drawing.FontStyle]::Bold)
        $Label.Text = "Server Status"
        $Label.AutoSize = $True
        $Label.TextAlign = "MiddleCenter"    
        $Form.Controls.Add($Label)
        
        $StartButton = New-Object System.Windows.Forms.Button
        $StartButton.Location = New-Object System.Drawing.Point (160,5)
        $StartButton.Size = New-Object System.Drawing.Size(75,23)
        $StartButton.Text = "Start"
        $StartButton.Add_Click({PingServers})
        $Form.Controls.Add($StartButton)
    
    $Col = 5
    $Line = 52
    $x = 25
    $y = 50
    
    ForEach ($ServerName in $array) {    
        $labelServer = New-Object System.Windows.Forms.Label
        $labelServer.Location = New-Object System.Drawing.Point ($x,$y)
        $labelServer.Size = New-Object System.Drawing.Size(145,23)
        $labelServer.Text = "$ServerName"    
        $Font = New-Object System.Drawing.Font("Times New Roman",12,[System.Drawing.FontStyle]::Regular)
        $labelServer.font = $Font    
        $labelServer.BorderStyle = "Fixed3D"
        $Form.Controls.Add($labelServer)
    
        # Initial PictureBox
    
        $pictureBox = New-object Windows.Forms.PictureBox               
        $pictureBox.Width = $imgYellowButton.Size.Width 
        $pictureBox.Height = $imgYellowButton.Size.Height
        $pictureBox.Location = New-Object System.Drawing.Point ($Col,$Line)    
        $pictureBox.Image = $imgYellowButton            
        $Form.controls.add($pictureBox)
            
        $y+=30
        $Line+=30
      if ($y -ge 650) {
        $x += 175
        $y = 50
        $Col += 175
        $Line = 52
        }
    }
    
    $Form.ShowDialog()

    I tried adding the initial and final images of the form but I can't since my account has not been verified yet.  Sorry.

    • Edited by BenitoB Friday, January 6, 2017 4:55 PM
    Friday, January 6, 2017 4:45 PM

Answers

  • Just change the image. Use three colored images and assign the correct color to the image box.

    The better way to do this is to use a DataGridView and fill it in with a record as you go.  YOu can add the correct image to the grid when you add the new record.

    Go here to learn how to design forms for admin tools.  It has many good tutorials and background on most controls.


    \_(ツ)_/

    • Marked as answer by BenitoB Friday, January 6, 2017 5:34 PM
    Friday, January 6, 2017 5:21 PM

All replies

  • Just change the image. Use three colored images and assign the correct color to the image box.

    The better way to do this is to use a DataGridView and fill it in with a record as you go.  YOu can add the correct image to the grid when you add the new record.

    Go here to learn how to design forms for admin tools.  It has many good tutorials and background on most controls.


    \_(ツ)_/

    • Marked as answer by BenitoB Friday, January 6, 2017 5:34 PM
    Friday, January 6, 2017 5:21 PM
  • Excellent.  Thanks you.
    Friday, January 6, 2017 5:34 PM