Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # PROJECTNAME
  3. # Description
  4. # Author
  5. # Updated 00/00/0000
  6. #
  7.  
  8.  
  9. Add-Type -AssemblyName System.Windows.Forms
  10. $wshell = New-Object -comObject Wscript.Shell
  11. [System.Windows.Forms.Application]::EnableVisualStyles()
  12. [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
  13.  
  14.  
  15. ################## Functions
  16.  
  17. # Activity Log (Saves to user's Desktop)
  18. $logLocation = [Environment]::GetFolderPath("Desktop") + "\PROJECTNAME Log.txt"
  19. function logAction ($action) {
  20.     $dateTime = Get-Date
  21.     Add-Content "$logLocation" "$dateTime | $action"
  22. }
  23. # Imports line items from a .txt document list.
  24. function importList {
  25.     logAction "Importing item list from text document."
  26.     $initialDirectory = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop)
  27.     $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
  28.     $OpenFileDialog.initialDirectory = $initialDirectory
  29.     $OpenFileDialog.filter = "All files (*.*)| *.*"
  30.     $OpenFileDialog.ShowDialog() | Out-Null
  31.     $selectedFile = $OpenFileDialog.filename
  32.     $computerList = Get-Content $selectedFile
  33.     foreach ($computer in $computerList) {
  34.         $itemList.Items.Add($computer)
  35.         logAction "$computer was added to the item list. (Imported from file)"
  36.     }
  37.        
  38. }
  39.  
  40. # Adds single user input item to item list.
  41. function addList {
  42.  
  43.     $computerName = [Microsoft.VisualBasic.Interaction]::InputBox('Enter a Host/User', 'Add Item')
  44.     $itemList.Items.Add($computerName)
  45.     logAction "$computerName was added to the item list. (Added manually)"
  46. }
  47.  
  48. # Removes the currently selected item from the item list.
  49. function removeList {
  50.     $computerName = $itemList.SelectedItem
  51.     logAction "$computerName was removed from the item list."
  52.     $index = $itemList.SelectedIndex
  53.     $itemList.Items.RemoveAt($index)
  54.  
  55. }
  56. # Clears all items from the list.
  57. function clearList {
  58.     logAction "All items cleared from list."
  59.     $itemList.Items.Clear()
  60. }
  61. function setFilePath {
  62.     $initialDirectory = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop)
  63.     $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
  64.     $OpenFileDialog.initialDirectory = $initialDirectory
  65.     $OpenFileDialog.filter = "All files (*.*)| *.*"
  66.     $OpenFileDialog.ShowDialog() | Out-Null
  67.     $filePath = $OpenFileDialog.filename
  68.     $filePathBox.text = $filePath
  69. }
  70. # Activates the main function of the tool, targeting each item on the item list.
  71. function activateTool {
  72.     logAction "PROJECTNAME started."
  73.     $msgBoxInput = $wshell.Popup("Are you sure you want to proceed?", 0, "Confirm", 0x1)
  74.     switch ($msgBoxInput) {
  75.         '1' {
  76.             logAction "User confirmed process execution."
  77.              
  78.             # Counters for progress bar calculation
  79.             $ctr = 0
  80.             foreach ($item in $itemList.items) {$ctr++}
  81.             $x = 100 / $ctr
  82.             $y = 0
  83.  
  84.             # Loop to perform action on each item in the list.    
  85.             foreach ($item in $itemList.items) {
  86.                 $statusText.text = "Pinging $item"
  87.                 try {
  88.                     If (test-connection -ComputerName $item -quiet) {
  89.                         logAction "$item ONLINE"
  90.  
  91.                         #INSERT SCRIPT TO PERFORM HERE
  92.                        
  93.                                     }
  94.                     Else {logAction "$item OFFLINE"}
  95.  
  96.                 }
  97.                 catch {
  98.                     logAction "$item | $_.Exception.Message"
  99.                 }
  100.                 $ProgressBar.Value = $y + $x
  101.                 $y = $y + $x
  102.             }
  103.    
  104.             # Operation Completed finishing actions.
  105.             $statusText.text = "Operation Complete"
  106.             logAction "Operation has completed."
  107.             Invoke-Item $logLocation
  108.             Start-Sleep -Seconds 1
  109.             $wshell.Popup("PROJECTNAME has finished running.", 0, "PROJECTNAME Tool")
  110.             $statusText.text = ""
  111.             $ProgressBar.Value = 0
  112.  
  113.         }
  114.         '2' {
  115.             logAction "User aborted the operation. (Confirmation declined)"
  116.         }
  117.     }
  118. }
  119.  
  120.  
  121. ################## GUI Form Elements
  122.  
  123. $Form = New-Object system.Windows.Forms.Form
  124. $Form.ClientSize = '400,300' #Width,Height
  125. $Form.text = "PROJECTNAME"
  126. $Form.FormBorderStyle = 'Fixed3D'
  127. $Form.MaximizeBox = $false
  128. $Form.TopMost = $false
  129.  
  130. # Generates the application icon
  131. [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
  132. $bmp = New-Object System.Drawing.Bitmap(16, 16)
  133. $g = [System.Drawing.Graphics]::FromImage($bmp)
  134. $g.drawline([System.Drawing.Pens]::Red, 4, 7, 4, 15)
  135. $g.drawline([System.Drawing.Pens]::Red, 5, 7, 5, 14)
  136. $g.drawline([System.Drawing.Pens]::Red, 10, 4, 10, 15)
  137. $g.drawline([System.Drawing.Pens]::Red, 11, 3, 11, 14)
  138. $g.drawline([System.Drawing.Pens]::Red, 0, 4, 14, 4)
  139. $g.drawline([System.Drawing.Pens]::Red, 1, 3, 15, 3)
  140. $ico = [System.Drawing.Icon]::FromHandle($bmp.GetHicon())
  141. $Form.Icon = $ico
  142.  
  143. $itemList = New-Object system.Windows.Forms.ListBox
  144. $itemList.width = 380
  145. $itemList.height = 190
  146. $itemList.location = New-Object System.Drawing.Point(10, 10)
  147. $Form.Controls.Add($itemList)
  148.  
  149. $buttonAdd = New-Object system.Windows.Forms.Button
  150. $buttonAdd.Text = "Add.."    
  151. $buttonAdd.width = 70
  152. $buttonAdd.height = 24
  153. $buttonAdd.location = New-Object System.Drawing.Point(10, 200)
  154. $buttonAdd.Font = 'Microsoft Sans Serif,10'
  155. $buttonAdd.Add_Click( { addList })
  156. $Form.Controls.Add($buttonAdd)
  157.  
  158. $buttonRemove = New-Object system.Windows.Forms.Button
  159. $buttonRemove.Text = "Remove"    
  160. $buttonRemove.width = 90
  161. $buttonRemove.height = 24
  162. $buttonRemove.location = New-Object System.Drawing.Point(85, 200)
  163. $buttonRemove.Font = 'Microsoft Sans Serif,10'
  164. $buttonRemove.Add_Click( { removeList })
  165. $Form.Controls.Add($buttonRemove)
  166.  
  167. $buttonClear = New-Object system.Windows.Forms.Button
  168. $buttonClear.Text = "Clear All"    
  169. $buttonClear.width = 100
  170. $buttonClear.height = 24
  171. $buttonClear.location = New-Object System.Drawing.Point(180, 200)
  172. $buttonClear.Font = 'Microsoft Sans Serif,10'
  173. $buttonClear.Add_Click( { clearList })
  174. $Form.Controls.Add($buttonClear)
  175.  
  176. $buttonImport = New-Object system.Windows.Forms.Button
  177. $buttonImport.Text = "Import List.."    
  178. $buttonImport.width = 105
  179. $buttonImport.height = 24
  180. $buttonImport.location = New-Object System.Drawing.Point(285, 200)
  181. $buttonImport.Font = 'Microsoft Sans Serif,10'
  182. $buttonImport.Add_Click( { importList })
  183. $Form.Controls.Add($buttonImport)
  184.  
  185. $ProgressBar = New-Object system.Windows.Forms.ProgressBar
  186. $ProgressBar.width = 380
  187. $ProgressBar.height = 17
  188. $ProgressBar.location = New-Object System.Drawing.Point(10, 230)
  189. $Form.Controls.Add($ProgressBar)
  190.  
  191. $statusText = New-Object system.Windows.Forms.Label
  192. $statusText.text = ""
  193. $statusText.AutoSize = $true
  194. $statusText.width = 25
  195. $statusText.height = 10
  196. $statusText.ForeColor = "Red"
  197. $statusText.location = New-Object System.Drawing.Point(50, 250)
  198. $statusText.Font = 'Microsoft Sans Serif,10'
  199. $Form.Controls.Add($statusText)
  200.  
  201. $buttonGo = New-Object system.Windows.Forms.Button
  202. $buttonGo.Text = "Go"    
  203. $buttonGo.width = 60
  204. $buttonGo.height = 24
  205. $buttonGo.location = New-Object System.Drawing.Point(170, 270)
  206. $buttonGo.Font = 'Microsoft Sans Serif,10'
  207. $buttonGo.Add_Click( { activateTool })
  208. $Form.Controls.Add($buttonGo)
  209.  
  210. # Verifies the application is being run with ADMIN- Account
  211. #If ($env:UserName -like "ADMIN ACCOUNT PREFIX*") {
  212.     [void]$Form.ShowDialog()
  213. #}
  214. #Else {
  215. #   $wshell.Popup("Please launch using ADMIN-Account.", 0, "Security Error")
  216. #    $formMain.Close()
  217. #}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement