Creating a form from the same code

Answered Creating a form from the same code

  • Friday, March 02, 2012 10:53 AM
     
      Has Code

    Hello,

    I've created a form that display disk info.

    If you run the script like this ".\script.ps1 toto" it will monitor disks from the toto server. A refresher it updating the data every 15 seconds

    in the form i also included a textbox. If you fill the textbox with a server name, a 2nd form is created, and it will monitor a 2nd server.

    BUt when the 2nd form is launched the 1st is not refreshed anymore ... (i put a write-host in the refresh )

    I'm sure i'm not doing this right, so could someone tell me how to generate the 2nd form to do the same thing on a 2nd server, without the first one being stopped ?!

    the form was generated with primalforms CE

    #Generated Form Function
    function GenerateForm {
      Param(
        [String]$Computer1 = "."
      )
    [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
    [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    
    #region Generated Form Objects
    $form1 = New-Object System.Windows.Forms.Form
    $textBox1 = New-Object System.Windows.Forms.TextBox
    $button2 = New-Object System.Windows.Forms.Button
    $button1 = New-Object System.Windows.Forms.Button
    $dataGrid1 = New-Object System.Windows.Forms.DataGrid
    $timer1 = New-Object System.Windows.Forms.Timer
    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
    #endregion Generated Form Objects
    
    #Provide Custom Code for events specified in PrimalForms.
    $LoadForm=
    {
        get-diskspace -TargetComputer $Computer1
    }
    $button1_OnClick= 
    {
        $Form1.Close()
    }
    $button2_OnClick= 
    {
        $b = $textBox1.text
        write-host $b
        $textBox1.Clear() 
        GenerateForm -Computer1 $b #<------ here i generate the 2nd form !!!!!
    }
    
    $Update= 
    {
        $a = date
        write-host $Computer1 " " $a
        get-diskspace -TargetComputer $Computer1
    }
    
    $OnLoadForm_StateCorrection=
    {#Correct the initial state of the form to prevent the .Net maximized form issue
    	$form1.WindowState = $InitialFormWindowState
    }
    
    #----------------------------------------------
    #region Generated Form Code
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 201
    $System_Drawing_Size.Width = 554
    $form1.ClientSize = $System_Drawing_Size
    $form1.DataBindings.DefaultDataSourceUpdateMode = 0
    $form1.Name = "form1"
    $form1.Text = "Primal Form"
    $form1.add_Load($LoadForm)
    
    $textBox1.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 111
    $System_Drawing_Point.Y = 166
    $textBox1.Location = $System_Drawing_Point
    $textBox1.Name = "textBox1"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 20
    $System_Drawing_Size.Width = 123
    $textBox1.Size = $System_Drawing_Size
    $textBox1.TabIndex = 3
    
    $form1.Controls.Add($textBox1)
    
    
    $button2.DataBindings.DefaultDataSourceUpdateMode = 0
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 166
    $button2.Location = $System_Drawing_Point
    $button2.Name = "button2"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 23
    $System_Drawing_Size.Width = 93
    $button2.Size = $System_Drawing_Size
    $button2.TabIndex = 2
    $button2.Text = "Monitor Server"
    $button2.UseVisualStyleBackColor = $True
    $button2.add_Click($button2_OnClick)
    
    $form1.Controls.Add($button2)
    
    
    $button1.DataBindings.DefaultDataSourceUpdateMode = 0
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 448
    $System_Drawing_Point.Y = 166
    $button1.Location = $System_Drawing_Point
    $button1.Name = "button1"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 23
    $System_Drawing_Size.Width = 94
    $button1.Size = $System_Drawing_Size
    $button1.TabIndex = 1
    $button1.Text = "Close"
    $button1.UseVisualStyleBackColor = $True
    $button1.add_Click($button1_OnClick)
    
    $form1.Controls.Add($button1)
    
    $dataGrid1.DataBindings.DefaultDataSourceUpdateMode = 0
    $dataGrid1.DataMember = ""
    $dataGrid1.HeaderForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 12
    $dataGrid1.Location = $System_Drawing_Point
    $dataGrid1.Name = "dataGrid1"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 121
    $System_Drawing_Size.Width = 530
    $dataGrid1.Size = $System_Drawing_Size
    $dataGrid1.TabIndex = 0
    
    $form1.Controls.Add($dataGrid1)
    
    $timer1.Enabled = $True
    $timer1.Interval = 15000
    $timer1.add_Tick($Update)
    
    #endregion Generated Form Code
    
    #Save the initial state of the form
    $InitialFormWindowState = $form1.WindowState
    #Init the OnLoad event to correct the initial state of the form
    $form1.add_Load($OnLoadForm_StateCorrection)
    #Show the Form
    $form1.ShowDialog()| Out-Null
    
    } #End Function
    
    #Call the Function
    function Get-DiskSpace{
      Param(
        [String]$TargetComputer = "."
      )
    $array = New-Object System.Collections.ArrayList
    $Script:DiskSpace = Get-WMIObject  -ComputerName $TargetComputer Win32_LogicalDisk `
    | select VolumeName, Name, DriveType, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}} `
    | Where-Object {$_.DriveType -eq 3}
    $array.AddRange($DiskSpace)
    $dataGrid1.DataSource = $array
    $form1.refresh()
    }
    
    #Faire un petit controle de ping sur le serveur qd même...
    #On jette les autres arguments
    if(!($args.count -eq 0)){
        GenerateForm -Computer1 $args.get(0)
    }else{
        GenerateForm
    }
    

    Thx

All Replies

  • Friday, March 02, 2012 1:12 PM
     
     Answered Has Code

    Alright i found a way ...

    i replaced this code:

    $button2_OnClick= 
    {
        $b = $textBox1.text
        write-host $b
        $textBox1.Clear() 
        GenerateForm -Computer1 $b #<------ here i generate the 2nd form !!!!!
    }

    with this:

    $button2_OnClick= 
    {
        $b = $textBox1.text
        $textBox1.Clear()
        If(!($b.length -eq 0)){
            write-host "b contient: $b"
            New-Monitor-Form -ParamComputer $b
        }
    }


    and this function :

    function New-Monitor-Form{
      Param(
        [String]$ParamComputer = "."
      )
        $pi = New-Object system.Diagnostics.ProcessStartInfo
        $pi.FileName = "powershell.exe"
        $pi.Arguments = "-noprofile -command .\script.ps1 $ParamComputer"
        [system.Diagnostics.Process]::Start($pi)
    }

    and it works fine !

    tell me what you think about this solution. Can it be done in an other way ?


    • Edited by TonQ Friday, March 02, 2012 1:13 PM
    • Marked As Answer by TonQ Friday, March 02, 2012 2:00 PM
    •  
  • Friday, March 02, 2012 6:31 PM
    Moderator
     
     
    This is a good solution as you are spawning the new form in a different process. The reason why your other method wasn't working is because the new form that you created took over the existing thread, leaving the other form to wait for an infinite amount of time to get access to the thread. I would imagine that if you closed the new form, the original form would start processing again.

    Boe Prox

    Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use Vote as Helpful.

    Looking for a script? Check out the Script Repository
    Need a script written for you? Submit a request at the Script Request Page