locked
GUP monitoring-GUI RRS feed

  • Question

  • Hi All,

    I am creating a GUP monitor using power-shell for my Symantec Anti Virus monitoring. I am still in learning phase with the powershell

    You can refer to this link to know the details:

    GUP-Monitor

    I was almost done with it and now I am trying to get the same to a GUI mode. But I am getting the error 

    Get-Content : Cannot bind argument to parameter 'Path' because it is an empty string.
    At E:\PSF\GUPMonitor-GUI.ps1:144 char:24
    [void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
    [void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
    
    #begin to draw forms
    $Form = New-Object System.Windows.Forms.Form
    $Form.Text = “SYmantec GUP Monitor”
    $Form.Size = New-Object System.Drawing.Size(500,150)
    $Form.StartPosition = “CenterScreen”
    
    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq “Enter”)
    {ping_computer}})
    $Form.Add_KeyDown({if ($_.KeyCode -eq “Escape”)
    {$Form.Close()}})
    
    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Size(5,5)
    $label.Size = New-Object System.Drawing.Size(240,30)
    $label.Text = “Enter a PC name or IP to test if it can respond to ping. Click ‘OK’ or hit ENTER to run…”
    $Form.Controls.Add($label)
    
    $textbox = New-Object System.Windows.Forms.TextBox
    $textbox.Location = New-Object System.Drawing.Size(5,40)
    $textbox.Size = New-Object System.Drawing.Size(120,20)
    $textbox.Text = “”
    $Form.Controls.Add($textbox)
    
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(140,65)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = “OK”
    $OKButton.Add_Click({$computername |ForEach-Parallel})
    $Form.Controls.Add($OKButton)
    
    $result_label = New-Object System.Windows.Forms.label
    $result_label.Location = New-Object System.Drawing.Size(5,65)
    $result_label.Size = New-Object System.Drawing.Size(240,30)
    $result_label.Text = “Results:”
    $Form.Controls.Add($result_label)
    
    $BrowseButton = New-Object System.Windows.Forms.Button 
    $BrowseButton.Location = New-Object System.Drawing.Size(140,40) 
    $BrowseButton.Size = New-Object System.Drawing.Size(75,20) 
    $BrowseButton.TabIndex = 2 
    
    $BrowseButton.Text = "Browse" 
    $BrowseButton.Add_Click({ 
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null 
    $dialog = New-Object System.Windows.Forms.OpenFileDialog 
    $dialog.DefaultExt = '.txt' 
    $dialog.Filter = 'All Files|*.*' 
    $dialog.FilterIndex = 0 
    $dialog.InitialDirectory = $home 
    $dialog.Multiselect = $false 
    $dialog.RestoreDirectory = $true 
    $dialog.Title = "Select a Input file" 
    $dialog.ValidateNames = $true 
    $dialog.ShowDialog() 
    $textbox.Text= $dialog.FileName;}) 
    $Form.Controls.Add($BrowseButton) #>
    
    
    
    
    function ForEach-Parallel {
        param(
            [Parameter(Mandatory=$true,position=0)]
            [System.Management.Automation.ScriptBlock] $ScriptBlock,
            [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
            [PSObject]$InputObject,
            [Parameter(Mandatory=$false)]
            [int]$MaxThreads=5
        )
        BEGIN {
            $iss = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
            $pool = [Runspacefactory]::CreateRunspacePool(1, $maxthreads, $iss, $host)
            $pool.open()
            $threads = @()
            $ScriptBlock = $ExecutionContext.InvokeCommand.NewScriptBlock("param(`$_)`r`n" + $Scriptblock.ToString())
        }
        PROCESS {
            $powershell = [powershell]::Create().addscript($scriptblock).addargument($InputObject)
            $powershell.runspacepool=$pool
            $threads+= @{
                instance = $powershell
                handle = $powershell.begininvoke()
            }
        }
        END {
            $notdone = $true
            while ($notdone) {
                $notdone = $false
                for ($i=0; $i -lt $threads.count; $i++) {
                    $thread = $threads[$i]
                    if ($thread) {
                        if ($thread.handle.iscompleted) {
                            $thread.instance.endinvoke($thread.handle)
                            $thread.instance.dispose()
                            $threads[$i] = $null
                        }
                        else {
                            $notdone = $true
                        }
                    }
                }
            }
        }
    }
    
    $ErrorActionPreference = "Stop";
    
    
    
    
    #ping the comptuer from $textbox.Text
    $computername=gc -path $textbox.Text
    
    $computername |ForEach-Parallel -MaxThreads 100{
    {
    try{
    if ($computername -ne “”)
     {
                    (test-connection $_ -Count 1).IPV4Address.IPAddressToString
                    Test-Connection $_ -Count 1 -ErrorAction silentlycontinue
    
    if (Test-Connection $_ -quiet -Count 1)
    {
    
                $result_label.Text = “$Computer has network connection”
                #write-host “$ComputerName has network connection”
    
    }
    else
    {
    #write-host “$ComputerName does not have network connection”
    $result_label.Text = “$Computer does not have network connection”
    }
    }
    else{
    $result_label.Text = “No comptuer name entered”
    }
    }
    Catch{
       "Caught an exception!" +
       "Exception Type: $($_.Exception.GetType().FullName)" +
        "Exception Message: $($_.Exception.Message)" 
          } 
    }
    }
    
    #Show form
    $Form.Topmost = $True
    $Form.Add_Shown({$Form.Activate()})
    [void] $Form.ShowDialog()
    I am sorry for this lengthy code. Guys please need your help in rectifying the issue and if possible a bit development tips for the code. Thanks !!!!!!!!!!!!!


    Naveen Basati

    Thursday, November 27, 2014 2:53 AM

Answers

All replies

  • You will need to learn how to debug a script.  It is notlikely that anyonehere is gong to debug a 150+ lines of code for you.

    Narrow the script down to only the lines that recreate the problem then someone can probably help you.


    ¯\_(ツ)_/¯

    Thursday, November 27, 2014 3:21 AM
  • A quick look tells me that you have not learned howto build a forms based script. You cannot have code in the root block of a form.  All code has to be in an event handler.

    Start here: http://www.sapien.com/blog/topics/user-interface-design-for-administrators/


    ¯\_(ツ)_/¯

    • Marked as answer by BASATI Thursday, November 27, 2014 3:34 AM
    Thursday, November 27, 2014 3:26 AM
  • Hi JRV,

    Thanks for the quick reply. And sorry for crapping up the things. I will narrow it down and check it out for the issue. Thanks for the link mate. In case of any issues, I will just get back to it.


    Naveen Basati

    Thursday, November 27, 2014 3:34 AM
  • Hi Jrv,

    I have narrowed it down and found a solution for it. Thanks anyway. Could you please provide me with few-more links regarding forms based scripts for a beginner like me.  


    Naveen Basati

    Saturday, November 29, 2014 3:12 AM