Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # PROJECTNAME
- # Description
- # Author
- # Updated 00/00/0000
- #
- Add-Type -AssemblyName System.Windows.Forms
- $wshell = New-Object -comObject Wscript.Shell
- [System.Windows.Forms.Application]::EnableVisualStyles()
- [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
- ################## Functions
- # Activity Log (Saves to user's Desktop)
- $logLocation = [Environment]::GetFolderPath("Desktop") + "\PROJECTNAME Log.txt"
- function logAction ($action) {
- $dateTime = Get-Date
- Add-Content "$logLocation" "$dateTime | $action"
- }
- # Imports line items from a .txt document list.
- function importList {
- logAction "Importing item list from text document."
- $initialDirectory = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop)
- $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
- $OpenFileDialog.initialDirectory = $initialDirectory
- $OpenFileDialog.filter = "All files (*.*)| *.*"
- $OpenFileDialog.ShowDialog() | Out-Null
- $selectedFile = $OpenFileDialog.filename
- $computerList = Get-Content $selectedFile
- foreach ($computer in $computerList) {
- $itemList.Items.Add($computer)
- logAction "$computer was added to the item list. (Imported from file)"
- }
- }
- # Adds single user input item to item list.
- function addList {
- $computerName = [Microsoft.VisualBasic.Interaction]::InputBox('Enter a Host/User', 'Add Item')
- $itemList.Items.Add($computerName)
- logAction "$computerName was added to the item list. (Added manually)"
- }
- # Removes the currently selected item from the item list.
- function removeList {
- $computerName = $itemList.SelectedItem
- logAction "$computerName was removed from the item list."
- $index = $itemList.SelectedIndex
- $itemList.Items.RemoveAt($index)
- }
- # Clears all items from the list.
- function clearList {
- logAction "All items cleared from list."
- $itemList.Items.Clear()
- }
- function setFilePath {
- $initialDirectory = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop)
- $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
- $OpenFileDialog.initialDirectory = $initialDirectory
- $OpenFileDialog.filter = "All files (*.*)| *.*"
- $OpenFileDialog.ShowDialog() | Out-Null
- $filePath = $OpenFileDialog.filename
- $filePathBox.text = $filePath
- }
- # Activates the main function of the tool, targeting each item on the item list.
- function activateTool {
- logAction "PROJECTNAME started."
- $msgBoxInput = $wshell.Popup("Are you sure you want to proceed?", 0, "Confirm", 0x1)
- switch ($msgBoxInput) {
- '1' {
- logAction "User confirmed process execution."
- # Counters for progress bar calculation
- $ctr = 0
- foreach ($item in $itemList.items) {$ctr++}
- $x = 100 / $ctr
- $y = 0
- # Loop to perform action on each item in the list.
- foreach ($item in $itemList.items) {
- $statusText.text = "Pinging $item"
- try {
- If (test-connection -ComputerName $item -quiet) {
- logAction "$item ONLINE"
- #INSERT SCRIPT TO PERFORM HERE
- }
- Else {logAction "$item OFFLINE"}
- }
- catch {
- logAction "$item | $_.Exception.Message"
- }
- $ProgressBar.Value = $y + $x
- $y = $y + $x
- }
- # Operation Completed finishing actions.
- $statusText.text = "Operation Complete"
- logAction "Operation has completed."
- Invoke-Item $logLocation
- Start-Sleep -Seconds 1
- $wshell.Popup("PROJECTNAME has finished running.", 0, "PROJECTNAME Tool")
- $statusText.text = ""
- $ProgressBar.Value = 0
- }
- '2' {
- logAction "User aborted the operation. (Confirmation declined)"
- }
- }
- }
- ################## GUI Form Elements
- $Form = New-Object system.Windows.Forms.Form
- $Form.ClientSize = '400,300' #Width,Height
- $Form.text = "PROJECTNAME"
- $Form.FormBorderStyle = 'Fixed3D'
- $Form.MaximizeBox = $false
- $Form.TopMost = $false
- # Generates the application icon
- [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
- $bmp = New-Object System.Drawing.Bitmap(16, 16)
- $g = [System.Drawing.Graphics]::FromImage($bmp)
- $g.drawline([System.Drawing.Pens]::Red, 4, 7, 4, 15)
- $g.drawline([System.Drawing.Pens]::Red, 5, 7, 5, 14)
- $g.drawline([System.Drawing.Pens]::Red, 10, 4, 10, 15)
- $g.drawline([System.Drawing.Pens]::Red, 11, 3, 11, 14)
- $g.drawline([System.Drawing.Pens]::Red, 0, 4, 14, 4)
- $g.drawline([System.Drawing.Pens]::Red, 1, 3, 15, 3)
- $ico = [System.Drawing.Icon]::FromHandle($bmp.GetHicon())
- $Form.Icon = $ico
- $itemList = New-Object system.Windows.Forms.ListBox
- $itemList.width = 380
- $itemList.height = 190
- $itemList.location = New-Object System.Drawing.Point(10, 10)
- $Form.Controls.Add($itemList)
- $buttonAdd = New-Object system.Windows.Forms.Button
- $buttonAdd.Text = "Add.."
- $buttonAdd.width = 70
- $buttonAdd.height = 24
- $buttonAdd.location = New-Object System.Drawing.Point(10, 200)
- $buttonAdd.Font = 'Microsoft Sans Serif,10'
- $buttonAdd.Add_Click( { addList })
- $Form.Controls.Add($buttonAdd)
- $buttonRemove = New-Object system.Windows.Forms.Button
- $buttonRemove.Text = "Remove"
- $buttonRemove.width = 90
- $buttonRemove.height = 24
- $buttonRemove.location = New-Object System.Drawing.Point(85, 200)
- $buttonRemove.Font = 'Microsoft Sans Serif,10'
- $buttonRemove.Add_Click( { removeList })
- $Form.Controls.Add($buttonRemove)
- $buttonClear = New-Object system.Windows.Forms.Button
- $buttonClear.Text = "Clear All"
- $buttonClear.width = 100
- $buttonClear.height = 24
- $buttonClear.location = New-Object System.Drawing.Point(180, 200)
- $buttonClear.Font = 'Microsoft Sans Serif,10'
- $buttonClear.Add_Click( { clearList })
- $Form.Controls.Add($buttonClear)
- $buttonImport = New-Object system.Windows.Forms.Button
- $buttonImport.Text = "Import List.."
- $buttonImport.width = 105
- $buttonImport.height = 24
- $buttonImport.location = New-Object System.Drawing.Point(285, 200)
- $buttonImport.Font = 'Microsoft Sans Serif,10'
- $buttonImport.Add_Click( { importList })
- $Form.Controls.Add($buttonImport)
- $ProgressBar = New-Object system.Windows.Forms.ProgressBar
- $ProgressBar.width = 380
- $ProgressBar.height = 17
- $ProgressBar.location = New-Object System.Drawing.Point(10, 230)
- $Form.Controls.Add($ProgressBar)
- $statusText = New-Object system.Windows.Forms.Label
- $statusText.text = ""
- $statusText.AutoSize = $true
- $statusText.width = 25
- $statusText.height = 10
- $statusText.ForeColor = "Red"
- $statusText.location = New-Object System.Drawing.Point(50, 250)
- $statusText.Font = 'Microsoft Sans Serif,10'
- $Form.Controls.Add($statusText)
- $buttonGo = New-Object system.Windows.Forms.Button
- $buttonGo.Text = "Go"
- $buttonGo.width = 60
- $buttonGo.height = 24
- $buttonGo.location = New-Object System.Drawing.Point(170, 270)
- $buttonGo.Font = 'Microsoft Sans Serif,10'
- $buttonGo.Add_Click( { activateTool })
- $Form.Controls.Add($buttonGo)
- # Verifies the application is being run with ADMIN- Account
- #If ($env:UserName -like "ADMIN ACCOUNT PREFIX*") {
- [void]$Form.ShowDialog()
- #}
- #Else {
- # $wshell.Popup("Please launch using ADMIN-Account.", 0, "Security Error")
- # $formMain.Close()
- #}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement