Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace System.Collections.Generic
- ### Constants ###
- $nl = [Environment]::NewLine
- $desktop = "$($env:USERPROFILE)\Desktop"
- $documents = [Environment]::GetFolderPath("MyDocuments")
- ### Variables ###
- $List = [system.collections.generic.list[Object]]::new()
- $Sites = [system.collections.generic.list[Object]]::new()
- $sites_file = Join-Path $documents 'WebSite-Watch.txt'
- $log_file = Join-Path $documents 'WebSite-Watch.log'
- $defaults = 'woot.com
- finviz.com
- reddit.com'
- # no Sites file so create one with default data.
- if(!(Test-Path $Script:sites_file)){ $defaults | Set-Content $Script:sites_file }
- #Hide Command Prompt, but not the GUI
- Add-Type -Name Window -Namespace Console -MemberDefinition '
- [DllImport("Kernel32.dll")]
- public static extern IntPtr GetConsoleWindow();
- [DllImport("user32.dll")]
- public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
- '
- function Show-Console {
- $consolePtr = [Console.Window]::GetConsoleWindow()
- [Console.Window]::ShowWindow($consolePtr, 5) #5 show
- }
- function Hide-Console {
- $consolePtr = [Console.Window]::GetConsoleWindow()
- [Console.Window]::ShowWindow($consolePtr, 0) #0 hide
- }
- function Write-Console{
- Param ( [Parameter(Mandatory=1)][string]$message,
- [Parameter(Mandatory=0)][switch]$Warning,
- [Parameter(Mandatory=0)][switch]$Error
- )
- Write-Host '[>]' -ForegroundColor Green -NoNewLine
- if($Warning){ Write-Host $Message -ForegroundColor Yellow ; return }
- if($Error){ Write-Host $Message -ForegroundColor Red ; return }
- Write-Host $Message
- }
- function Get-Response{
- param( [Parameter(Mandatory=1)][string]$Url
- )
- $agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
- try{
- $response = Invoke-WebRequest $Url -UserAgent $agent
- return [PSCustomObject] @{ url=$Url; status=$response.StatusDescription; DT=$((get-date).ToLocalTime()).ToString("yyyy-MM-dd HH:mm:ss") }
- }catch{
- $_.Exception.Message | Write-Warning
- return $null
- }
- }
- function Log-Entry{
- param( [Parameter(Mandatory=1)][string]$DT,
- [Parameter(Mandatory=1)][string]$Url,
- [Parameter(Mandatory=1)][string]$Status
- )
- $("[$DT]::$Url : $Status") | Add-Content $Script:log_file
- }
- function Load-Sites{
- if(Test-Path $Script:sites_file){
- if($Script:Sites.Count -gt 0) { $Script:Sites.Clear() }
- Get-Content $Script:sites_file | ForEach-Object { $Script:Sites.Add($_) }
- }
- }
- Add-Type -AssemblyName PresentationCore, PresentationFramework
- #Create Form Objects
- [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
- [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
- $Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
- $Form = New-Object system.Windows.Forms.Form
- $Form.ClientSize = '550,320'
- $Form.text = "WebSite Watch"
- $Form.TopMost = $true
- $Form.MaximizeBox = $false
- $Form.SizeGripStyle = "Hide"
- $Form.FormBorderStyle = "FixedDialog"
- $Form.Icon = $Icon
- $Form.StartPosition = 'CenterScreen'
- $Form.KeyPreview = $True
- # Reference: https://dekac.weebly.com/blog/powershell-gui-script-example
- $tooltip1 = New-Object System.Windows.Forms.ToolTip
- $ShowHelp={
- #display popup help
- #each value is the name of a control on the form.
- Switch ($this.name) {
- 'exitButton' {$tip = 'Exit The Application.' }
- 'startButton' {$tip = 'Start/Stop Timer to obtain Status of Websites at Interval.'}
- 'logButton' {$tip = 'Open Log.' }
- 'sitesButton' {$tip = 'Open Sites File.' }
- 'intervalComboBox' {$tip = 'Select Timer Interval, (Seconds).' }
- 'intervalLabel' {$tip = 'Select Timer Interval, (Seconds).' }
- 'debugCheckBox' {$tip = 'Show/Hide Console Window.' }
- }
- $tooltip1.SetToolTip($this,$tip)
- }
- $exitButton = New-Object system.Windows.Forms.Button
- $exitButton.Text = "Exit"
- $exitButton.Width = 120
- $exitButton.Height = 30
- $exitButton.Location = [System.Drawing.Point]::new(420,10)
- $exitButton.Font = 'Arial,12,style=Bold'
- $exitButton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
- $exitButton.TabIndex = 1
- $exitButton.Name = 'exitButton'
- $exitButton.Add_MouseHover($ShowHelp)
- $Form.Controls.Add($exitButton)
- $Form.CancelButton = $exitButton
- $startButton = New-Object system.Windows.Forms.Button
- $startButton.text = 'Start Timer'
- $startButton.Width = 120
- $startButton.height = 30
- $startButton.location = [System.Drawing.Point]::new(420,50)
- $startButton.Font = 'Arial,12,style=Bold'
- $startButton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
- $startButton.TabIndex = 2
- $startButton.Name = 'startButton'
- $startButton.Add_MouseHover($ShowHelp)
- $Form.Controls.Add($startButton)
- $Form.AcceptButton = $startButton
- $logButton = New-Object system.Windows.Forms.Button
- $logButton.text = 'Open Log'
- $logButton.Width = 120
- $logButton.height = 30
- $logButton.location = [System.Drawing.Point]::new(420,90)
- $logButton.Font = 'Arial,12,style=Bold'
- $logButton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
- $logButton.TabIndex = 3
- $logButton.Name = 'logButton'
- $logButton.Add_MouseHover($ShowHelp)
- $Form.Controls.Add($logButton)
- $sitesButton = New-Object system.Windows.Forms.Button
- $sitesButton.text = 'Open Sites'
- $sitesButton.Width = 120
- $sitesButton.height = 30
- $sitesButton.location = [System.Drawing.Point]::new(420,130)
- $sitesButton.Font = 'Arial,12,style=Bold'
- $sitesButton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
- $sitesButton.TabIndex = 4
- $sitesButton.Name = 'sitesButton'
- $sitesButton.Add_MouseHover($ShowHelp)
- $Form.Controls.Add($sitesButton)
- $intervalLabel = New-Object System.Windows.Forms.label
- $intervalLabel.Width = 60
- $intervalLabel.Height = 30
- $intervalLabel.Location = [System.Drawing.Point]::new(420,170)
- $intervalLabel.Font = 'Arial,12,style=Bold'
- $intervalLabel.BorderStyle = 'none'
- $intervalLabel.Text = 'Timer:'
- $intervalLabel.Name = 'intervalLabel'
- $intervalLabel.Add_MouseHover($ShowHelp)
- $Form.Controls.Add($intervalLabel)
- $intervalComboBox = New-Object System.Windows.Forms.ComboBox
- $intervalComboBox.Width = 60
- $intervalComboBox.Height = 30
- $intervalComboBox.Location = [System.Drawing.Point]::new(480,170)
- $intervalComboBox.Font = 'Arial,12,style=Bold'
- $intervalComboBox.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
- $intervalComboBox.TabIndex = 5
- $intervalComboBox.Name = 'intervalComboBox'
- $intervalComboBox.Add_MouseHover($ShowHelp)
- $intervalComboBox.Items.AddRange(@(30,45,60,75,90,120))
- $intervalComboBox.SelectedIndex = 0
- $Form.Controls.Add($intervalComboBox)
- $debugCheckBox = New-Object System.Windows.Forms.CheckBox
- $debugCheckBox.Text = 'Debug Mode'
- $debugCheckBox.Width = 170
- $debugCheckBox.Height = 30
- $debugCheckBox.Location = [System.Drawing.Point]::new(420,210)
- $debugCheckBox.Font = 'Arial,12,style=Bold'
- $debugCheckBox.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
- $debugCheckBox.TabIndex = 6
- $debugCheckBox.Checked = $true
- $debugCheckBox.Name = 'debugCheckBox'
- $debugCheckBox.Add_MouseHover($ShowHelp)
- $Form.Controls.Add($debugCheckBox)
- $DataGridView = New-Object System.Windows.Forms.DataGridView
- $DataGridView.Width = 400
- $DataGridView.Height = 300
- $DataGridView.Location = [System.Drawing.Point]::new(10,10)
- $DataGridView.Font = 'Arial,10,style=Bold'
- $DataGridView.ReadOnly = $true
- $DataGridView.BorderStyle = "FixedSingle"
- $DataGridView.AllowUserToDeleteRows = $false
- $DataGridView.MultiSelect = $false
- $DataGridView.SelectionMode = 'FullRowSelect'
- $DataGridView.AutoSize = $false
- $DataGridView.TabIndex = 7
- $Form.Controls.Add($DataGridView)
- #Sets the timer interval to 60 seconds.
- $Timer1 = New-Object System.Windows.Forms.Timer
- $Timer1.Interval = 30000
- #Form Events
- $Timer1.add_tick({
- $Script:List.Clear()
- Write-Console $('Updating List')
- for($i=0; $i -lt $Script:Sites.Count; $i++){
- $tmp = Get-Response $Script:Sites[$i]
- $Script:List.Add($tmp)
- Write-Console $('Connecting to Site: ' + $Script:Sites[$i])
- Log-Entry $tmp.DT $tmp.url $tmp.status
- }
- $DataGridView.DataSource = $Script:List
- $DataGridView.AutoResizeColumns()
- })
- $Form.Add_FormClosing({
- #Handles a hung GUI
- Stop-Process -Id $PID
- })
- $Form.Add_Load({
- $Timer1.Interval = [int]$intervalComboBox.Text * 1000
- Write-Console $('Set Timer Interval: ' + $Timer1.Interval)
- Load-Sites
- })
- $exitButton.Add_Click({
- # Close Button
- $Timer1.Stop()
- $Timer1.Dispose()
- $Form.close()
- })
- $startButton.Add_Click({
- # Start Button
- if ($Timer1.Enabled -eq $true){
- $Timer1.Stop()
- $startButton.Text = 'Start Timer'
- $intervalComboBox.Enabled = $true
- $logButton.Enabled = $true
- Write-Console $('Timer Stopped')
- }else{
- $Timer1.Start()
- $startButton.Text = 'Stop Timer'
- $intervalComboBox.Enabled = $false
- $logButton.Enabled = $false
- Write-Console $('Timer Started')
- }
- })
- $logButton.Add_Click({
- # Open Log Button
- if(Test-Path $Script:log_file){
- Start-Process $Script:log_file
- Write-Console $('Opening Log File: ' + $Script:log_file)
- }else{
- Write-Console $('Unable to locate Log File: ' + $Script:log_file) -Warning
- }
- })
- $sitesButton.Add_Click({
- # Sites file Button
- if(Test-Path $Script:sites_file){
- Start-Process $Script:sites_file
- Write-Console $('Opening Sites File: ' + $Script:sites_file)
- }else{
- Write-Console $('Unable to locate Sites File: ' + $Script:sites_file) -Warning
- }
- })
- $intervalComboBox.Add_SelectedIndexChanged({
- $Timer1.Interval = [int]$intervalComboBox.Text * 1000
- Write-Console $('Changed Timer Interval: ' + $Timer1.Interval)
- })
- $debugCheckBox.Add_Click({
- if($debugCheckBox.Checked){
- Show-Console
- Write-Console $('Show Console') -Warning
- }else{
- Hide-Console
- Write-Console $('Hide Console') -Warning
- }
- })
- $Form.Add_KeyDown({
- if ($PSItem.KeyCode -eq 'F1'){
- Write-Console $('User Pressed F1 Key')
- }
- })
- #Show the form
- $Form.ShowDialog() | Out-Null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement