Advertisement
bejiitas_wrath

Powershell form.

Oct 31st, 2012
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Set up the form
  2. #Load the required .NET libraries
  3. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  4. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  5.  
  6. function foobar() {
  7.     #$a = gwmi win32_bios
  8.     #echo $a
  9.  
  10.     $comp = '.'
  11.  
  12.     $stuff = gwmi -class "win32_localtime" -namespace "root/CIMV2" -computername $comp
  13.  
  14.     foreach ($thingie in $stuff) {
  15.         [void]$ourlistbox.items.Add('Day' + $thingie.Day)
  16.     }
  17. }
  18.  
  19. function trollface() {
  20.     $last = gwmi -query "select LastBootUpTime from win32_operatingsystem"
  21.     $last.ConvertToDateTime($last.LastBootUpTime)
  22.     [void]$objTextBox.items.Add('Bootup time:' + $last)
  23. }
  24.  
  25. #Create the plain form
  26. $ourForm = New-Object System.Windows.Forms.Form
  27. $ourForm.Text = "Our Practice Powershell Application"
  28. $ourForm.Size = New-Object System.Drawing.Size(1024,1000)
  29. $ourForm.StartPosition = "CenterScreen"
  30. $ourForm.KeyPreview = $True
  31.  
  32. $objLabel2 = new-object System.Windows.Forms.Label
  33. $objLabel2.Location = new-object System.Drawing.Size(10,10)
  34. $objlabel2.Size = new-object System.Drawing.Size(96,20)
  35. $objLabel2.Text = "Misc text."
  36. $ourForm.Controls.Add($objLabel2)
  37.  
  38. #Add listbox
  39. $ourListbox = New-Object System.Windows.Forms.Listbox
  40. $ourListbox.Location = New-Object System.Drawing.Size(10,40)
  41. $ourListbox.Size = New-Object System.Drawing.Size(200,64)
  42. $ourForm.Controls.Add($ourListbox)
  43.  
  44. #Add a command button to close form
  45. $closeButton = New-Object System.Windows.Forms.Button
  46. $closeButton.Location = New-Object System.Drawing.Size(145,150)
  47. $closeButton.Size = New-Object System.Drawing.Size(75,23)
  48. $closeButton.Text = "Close"
  49. $closeButton.Add_Click({$ourForm.Close()})
  50. $ourForm.Controls.Add($closeButton)
  51.  
  52. #Add a command button to activate form
  53. $runButton = New-Object System.Windows.Forms.Button
  54. $runButton.Location = New-Object System.Drawing.Size(230,150)
  55. $runButton.Size = New-Object System.Drawing.Size(75,23)
  56. $runButton.Text = "Run"
  57. $runButton.Add_Click({foobar})
  58. $ourForm.Controls.Add($runButton)
  59.  
  60. $objLabel1 = new-object System.Windows.Forms.Label
  61. $objLabel1.Location = new-object System.Drawing.Size(400,40)
  62. $objlabel1.Size = new-object System.Drawing.Size(96,20)
  63. $objLabel1.Text = "Last bootup time."
  64. $ourForm.Controls.Add($objLabel1)
  65.  
  66. $objTextBox = new-object System.Windows.Forms.TextBox
  67. $objTextBox.Location = new-object System.Drawing.Size(500,40)
  68. $objTextBox.Size = new-object System.drawing.size (260,90)
  69. $ourForm.Controls.Add($objTextBox)
  70.  
  71. #Add a command button to activate form
  72. $printButton = New-Object System.Windows.Forms.Button
  73. $printButton.Location = New-Object System.Drawing.Size(310,150)
  74. $printButton.Size = New-Object System.Drawing.Size(85,23)
  75. $printButton.Text = "Print"
  76. $printButton.Add_Click({trollface})
  77. $printButton.Add_Click({$objTextBox.Text=trollface})
  78. $ourForm.Controls.Add($printButton)
  79.  
  80. #Activate the form
  81. $ourForm.Add_Shown({$ourForm.Activate()})
  82. [void] $ourForm.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement