Next: How to build a window form in Powershell

How to build a window form in Powershell


Here the code:



 #----------------------------------------------  
 #region Application Functions  
 #----------------------------------------------  
    
 function OnApplicationLoad {  
     #Note: This function is not called in Projects  
     #Note: This function runs before the form is created  
     #Note: To get the script directory in the Packager use: Split-Path $hostinvocation.MyCommand.path  
     #Note: To get the console output in the Packager (Windows Mode) use: $ConsoleOutput (Type: System.Collections.ArrayList)  
     #Important: Form controls cannot be accessed in this function  
     #TODO: Add snapins and custom code to validate the application load  
       
       
     return $true #return true for success or false for failure  
 }  
    
 function OnApplicationExit {  
     #Note: This function is not called in Projects  
     #Note: This function runs after the form is closed  
     #TODO: Add custom code to clean up and unload snapins when the application exits  
       
     $script:ExitCode = 0 #Set the exit code for the Packager  
 }  
    
 #endregion Application Functions  
    
 #----------------------------------------------  
 # Generated Form Function  
 #----------------------------------------------  
 function myWindows {  
    
     ###############################  
     #region Import the Assemblies  
     ###############################  
     [void][reflection.assembly]::Load("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")  
     [void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")  
     [void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")  
     [void][reflection.assembly]::Load("System.Data, 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("System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")  
     [void][reflection.assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")  
     [void][reflection.assembly]::Load("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")  
     [void][reflection.assembly]::Load("System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")  
     ###############################  
     #endregion Import Assemblies  
     ###############################  
    
       
     [System.Windows.Forms.Application]::EnableVisualStyles()  
     $formMyWindow = New-Object 'System.Windows.Forms.Form'  
     $button1 = New-Object 'System.Windows.Forms.button'  
     $statusbar1 = New-Object 'System.Windows.Forms.StatusBar'  
     $textbox1 = New-Object 'System.Windows.Forms.TextBox'  
     $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'  
     #endregion Generated Form Objects     
       
     ###############################   
     #Begin Event   
     ###############################  
     $formMyWindow_Load={  
     }  
       
       
     $button1_Click={   
       $statusbar1.Text = 'The value is: ' + $textbox1.text  
     }  
       
      
     $Form_StateCorrection_Load=  
     {  
         #Correct the initial state of the form to prevent the .Net maximized form issue  
         $formMyWindow.WindowState = $InitialFormWindowState  
     }  
       
     $Form_Cleanup_FormClosed=  
     {  
         #Remove all event handlers from the controls  
         try  
         {  
             $button1.remove_Click($button1_Click)  
             $formMyWindow.remove_Load($formMyWindow_Load)  
             $formMyWindow.remove_Load($Form_StateCorrection_Load)  
             $formMyWindow.remove_FormClosed($Form_Cleanup_FormClosed)  
         }  
         catch [Exception]  
         { }  
     }  
     ###############################          
     #End Event  
     ###############################  
   
    
     $formMyWindow.Controls.Add($button1)  
     $formMyWindow.Controls.Add($statusbar1)  
     $formMyWindow.Controls.Add($textbox1)  
     $formMyWindow.ClientSize = '372, 188'  
     $formMyWindow.FormBorderStyle = 'Fixed3D'  
     $formMyWindow.Name = "formMyWindow"  
     $formMyWindow.Text = "My Window"  
     $formMyWindow.add_Load($formMyWindow_Load)  
   
       
     $textbox1.Location = '12, 40'  
     $textbox1.Name = "textbox1"  
     $textbox1.Size = '265, 20'  
     $textbox1.TabIndex = 0  
       
     $button1.Location = '283, 40'  
     $button1.Name = "button1"  
     $button1.Size = '81, 20'  
     $button1.TabIndex = 1  
     $button1.Text = "Click Me"  
     $button1.UseVisualStyleBackColor = $True  
     $button1.add_Click($button1_Click)      
       
     $statusbar1.Location = '0, 166'  
     $statusbar1.Name = "statusbar1"  
     $statusbar1.Size = '372, 22'  
     $statusbar1.TabIndex = 3      
       
    
     #Save the initial state of the form  
     $InitialFormWindowState = $formMyWindow.WindowState  
       
     #Init the OnLoad event to correct the initial state of the form  
     $formMyWindow.add_Load($Form_StateCorrection_Load)  
       
     #Clean up the control events  
     $formMyWindow.add_FormClosed($Form_Cleanup_FormClosed)  
       
     #Show the Form  
     return $formMyWindow.ShowDialog()  
    
 } #End Function  
    
 if((OnApplicationLoad) -eq $true)  
 {  
     #Call the form  
     myWindows | Out-Null  
     #Perform cleanup  
     OnApplicationExit  
 }