Advertisement
atheos42

WebSite-Watch

Jul 22nd, 2022 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 10.85 KB | Source Code | 0 0
  1. using namespace System.Collections.Generic
  2.  
  3. ### Constants ###
  4. $nl = [Environment]::NewLine
  5. $desktop = "$($env:USERPROFILE)\Desktop"
  6. $documents = [Environment]::GetFolderPath("MyDocuments")
  7.  
  8. ### Variables ###
  9. $List = [system.collections.generic.list[Object]]::new()
  10. $Sites = [system.collections.generic.list[Object]]::new()
  11. $sites_file = Join-Path $documents 'WebSite-Watch.txt'
  12. $log_file = Join-Path $documents 'WebSite-Watch.log'
  13.  
  14. $defaults = 'woot.com
  15. finviz.com
  16. reddit.com'
  17.  
  18. # no Sites file so create one with default data.
  19. if(!(Test-Path $Script:sites_file)){ $defaults | Set-Content $Script:sites_file }
  20.  
  21.  
  22. #Hide Command Prompt, but not the GUI
  23. Add-Type -Name Window -Namespace Console -MemberDefinition '
  24. [DllImport("Kernel32.dll")]
  25. public static extern IntPtr GetConsoleWindow();
  26. [DllImport("user32.dll")]
  27. public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
  28. '
  29.  
  30. function Show-Console {
  31.     $consolePtr = [Console.Window]::GetConsoleWindow()
  32.     [Console.Window]::ShowWindow($consolePtr, 5)  #5 show
  33. }
  34.  
  35. function Hide-Console {
  36.     $consolePtr = [Console.Window]::GetConsoleWindow()
  37.     [Console.Window]::ShowWindow($consolePtr, 0)  #0 hide
  38. }
  39.  
  40. function Write-Console{
  41.     Param ( [Parameter(Mandatory=1)][string]$message,
  42.             [Parameter(Mandatory=0)][switch]$Warning,
  43.             [Parameter(Mandatory=0)][switch]$Error
  44.     )
  45.     Write-Host '[>]' -ForegroundColor Green -NoNewLine
  46.     if($Warning){ Write-Host $Message -ForegroundColor Yellow ; return }
  47.     if($Error){ Write-Host $Message -ForegroundColor Red ; return }
  48.     Write-Host $Message
  49. }
  50.  
  51. function Get-Response{
  52.     param( [Parameter(Mandatory=1)][string]$Url
  53.     )
  54.     $agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
  55.    
  56.     try{
  57.         $response = Invoke-WebRequest $Url -UserAgent $agent
  58.         return [PSCustomObject] @{ url=$Url; status=$response.StatusDescription; DT=$((get-date).ToLocalTime()).ToString("yyyy-MM-dd HH:mm:ss") }
  59.     }catch{
  60.         $_.Exception.Message | Write-Warning
  61.         return $null
  62.     }
  63. }
  64.  
  65. function Log-Entry{
  66.     param( [Parameter(Mandatory=1)][string]$DT,
  67.            [Parameter(Mandatory=1)][string]$Url,
  68.            [Parameter(Mandatory=1)][string]$Status
  69.     )
  70.     $("[$DT]::$Url : $Status") | Add-Content $Script:log_file
  71. }
  72.  
  73. function Load-Sites{
  74.     if(Test-Path $Script:sites_file){
  75.         if($Script:Sites.Count -gt 0) { $Script:Sites.Clear() }
  76.         Get-Content $Script:sites_file | ForEach-Object { $Script:Sites.Add($_) }
  77.     }
  78. }
  79.  
  80. Add-Type -AssemblyName PresentationCore, PresentationFramework
  81.  
  82. #Create Form Objects
  83. [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
  84. [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
  85.  
  86. $Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
  87.  
  88. $Form                 = New-Object system.Windows.Forms.Form
  89. $Form.ClientSize      = '550,320'
  90. $Form.text            = "WebSite Watch"
  91. $Form.TopMost         = $true
  92. $Form.MaximizeBox     = $false
  93. $Form.SizeGripStyle   = "Hide"
  94. $Form.FormBorderStyle = "FixedDialog"
  95. $Form.Icon            = $Icon
  96. $Form.StartPosition   = 'CenterScreen'
  97. $Form.KeyPreview      = $True
  98.  
  99. # Reference: https://dekac.weebly.com/blog/powershell-gui-script-example
  100. $tooltip1 = New-Object System.Windows.Forms.ToolTip
  101. $ShowHelp={
  102.     #display popup help
  103.     #each value is the name of a control on the form.
  104.     Switch ($this.name) {
  105.         'exitButton'       {$tip = 'Exit The Application.'                                     }
  106.         'startButton'      {$tip = 'Start/Stop Timer to obtain Status of Websites at Interval.'}
  107.         'logButton'        {$tip = 'Open Log.'                                                 }
  108.         'sitesButton'      {$tip = 'Open Sites File.'                                          }
  109.         'intervalComboBox' {$tip = 'Select Timer Interval, (Seconds).'                         }
  110.         'intervalLabel'    {$tip = 'Select Timer Interval, (Seconds).'                         }
  111.         'debugCheckBox'    {$tip = 'Show/Hide Console Window.'                                 }
  112.     }
  113.     $tooltip1.SetToolTip($this,$tip)
  114. }
  115.  
  116. $exitButton             = New-Object system.Windows.Forms.Button
  117. $exitButton.Text        = "Exit"
  118. $exitButton.Width       = 120
  119. $exitButton.Height      = 30
  120. $exitButton.Location    = [System.Drawing.Point]::new(420,10)
  121. $exitButton.Font        = 'Arial,12,style=Bold'
  122. $exitButton.FlatStyle   = [System.Windows.Forms.FlatStyle]::Flat
  123. $exitButton.TabIndex    = 1
  124. $exitButton.Name        = 'exitButton'
  125. $exitButton.Add_MouseHover($ShowHelp)
  126. $Form.Controls.Add($exitButton)
  127. $Form.CancelButton = $exitButton
  128.  
  129. $startButton             = New-Object system.Windows.Forms.Button
  130. $startButton.text        = 'Start Timer'
  131. $startButton.Width       = 120
  132. $startButton.height      = 30
  133. $startButton.location    = [System.Drawing.Point]::new(420,50)
  134. $startButton.Font        = 'Arial,12,style=Bold'
  135. $startButton.FlatStyle   = [System.Windows.Forms.FlatStyle]::Flat
  136. $startButton.TabIndex    = 2
  137. $startButton.Name        = 'startButton'
  138. $startButton.Add_MouseHover($ShowHelp)
  139. $Form.Controls.Add($startButton)
  140. $Form.AcceptButton = $startButton
  141.  
  142. $logButton             = New-Object system.Windows.Forms.Button
  143. $logButton.text        = 'Open Log'
  144. $logButton.Width       = 120
  145. $logButton.height      = 30
  146. $logButton.location    = [System.Drawing.Point]::new(420,90)
  147. $logButton.Font        = 'Arial,12,style=Bold'
  148. $logButton.FlatStyle   = [System.Windows.Forms.FlatStyle]::Flat
  149. $logButton.TabIndex    = 3
  150. $logButton.Name        = 'logButton'
  151. $logButton.Add_MouseHover($ShowHelp)
  152. $Form.Controls.Add($logButton)
  153.  
  154. $sitesButton           = New-Object system.Windows.Forms.Button
  155. $sitesButton.text        = 'Open Sites'
  156. $sitesButton.Width       = 120
  157. $sitesButton.height      = 30
  158. $sitesButton.location    = [System.Drawing.Point]::new(420,130)
  159. $sitesButton.Font        = 'Arial,12,style=Bold'
  160. $sitesButton.FlatStyle   = [System.Windows.Forms.FlatStyle]::Flat
  161. $sitesButton.TabIndex    = 4
  162. $sitesButton.Name        = 'sitesButton'
  163. $sitesButton.Add_MouseHover($ShowHelp)
  164. $Form.Controls.Add($sitesButton)
  165.  
  166. $intervalLabel = New-Object System.Windows.Forms.label
  167. $intervalLabel.Width        = 60
  168. $intervalLabel.Height       = 30
  169. $intervalLabel.Location     = [System.Drawing.Point]::new(420,170)
  170. $intervalLabel.Font         = 'Arial,12,style=Bold'
  171. $intervalLabel.BorderStyle  = 'none'
  172. $intervalLabel.Text         = 'Timer:'
  173. $intervalLabel.Name         = 'intervalLabel'
  174. $intervalLabel.Add_MouseHover($ShowHelp)
  175. $Form.Controls.Add($intervalLabel)
  176.  
  177. $intervalComboBox           = New-Object System.Windows.Forms.ComboBox
  178. $intervalComboBox.Width     = 60
  179. $intervalComboBox.Height    = 30
  180. $intervalComboBox.Location  = [System.Drawing.Point]::new(480,170)
  181. $intervalComboBox.Font      = 'Arial,12,style=Bold'
  182. $intervalComboBox.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
  183. $intervalComboBox.TabIndex  = 5
  184. $intervalComboBox.Name      = 'intervalComboBox'
  185. $intervalComboBox.Add_MouseHover($ShowHelp)
  186. $intervalComboBox.Items.AddRange(@(30,45,60,75,90,120))
  187. $intervalComboBox.SelectedIndex = 0
  188. $Form.Controls.Add($intervalComboBox)
  189.  
  190. $debugCheckBox           = New-Object System.Windows.Forms.CheckBox
  191. $debugCheckBox.Text      = 'Debug Mode'
  192. $debugCheckBox.Width     = 170
  193. $debugCheckBox.Height    = 30
  194. $debugCheckBox.Location  = [System.Drawing.Point]::new(420,210)
  195. $debugCheckBox.Font      = 'Arial,12,style=Bold'
  196. $debugCheckBox.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
  197. $debugCheckBox.TabIndex  = 6
  198. $debugCheckBox.Checked   = $true
  199. $debugCheckBox.Name      = 'debugCheckBox'
  200. $debugCheckBox.Add_MouseHover($ShowHelp)
  201. $Form.Controls.Add($debugCheckBox)
  202.  
  203. $DataGridView                       = New-Object System.Windows.Forms.DataGridView
  204. $DataGridView.Width                 = 400
  205. $DataGridView.Height                = 300
  206. $DataGridView.Location              = [System.Drawing.Point]::new(10,10)
  207. $DataGridView.Font                  = 'Arial,10,style=Bold'
  208. $DataGridView.ReadOnly              = $true
  209. $DataGridView.BorderStyle           = "FixedSingle"
  210. $DataGridView.AllowUserToDeleteRows = $false
  211. $DataGridView.MultiSelect           = $false
  212. $DataGridView.SelectionMode         = 'FullRowSelect'
  213. $DataGridView.AutoSize              = $false
  214. $DataGridView.TabIndex = 7
  215. $Form.Controls.Add($DataGridView)
  216.  
  217. #Sets the timer interval to 60 seconds.
  218. $Timer1 = New-Object System.Windows.Forms.Timer
  219. $Timer1.Interval = 30000
  220.  
  221. #Form Events
  222.  
  223. $Timer1.add_tick({
  224.     $Script:List.Clear()
  225.     Write-Console $('Updating List')
  226.     for($i=0; $i -lt $Script:Sites.Count; $i++){
  227.         $tmp = Get-Response $Script:Sites[$i]
  228.         $Script:List.Add($tmp)
  229.         Write-Console $('Connecting to Site: ' + $Script:Sites[$i])
  230.         Log-Entry $tmp.DT $tmp.url $tmp.status
  231.     }
  232.     $DataGridView.DataSource = $Script:List
  233.     $DataGridView.AutoResizeColumns()
  234. })
  235.  
  236. $Form.Add_FormClosing({
  237.     #Handles a hung GUI
  238.     Stop-Process -Id $PID
  239. })
  240.  
  241. $Form.Add_Load({
  242.     $Timer1.Interval = [int]$intervalComboBox.Text * 1000
  243.     Write-Console $('Set Timer Interval: ' + $Timer1.Interval)
  244.     Load-Sites
  245. })
  246.  
  247. $exitButton.Add_Click({
  248.     # Close Button
  249.     $Timer1.Stop()
  250.     $Timer1.Dispose()
  251.     $Form.close()
  252. })
  253.  
  254. $startButton.Add_Click({
  255.     # Start Button
  256.     if ($Timer1.Enabled -eq $true){
  257.         $Timer1.Stop()
  258.         $startButton.Text  = 'Start Timer'
  259.         $intervalComboBox.Enabled = $true
  260.         $logButton.Enabled = $true
  261.         Write-Console $('Timer Stopped')
  262.     }else{
  263.         $Timer1.Start()
  264.         $startButton.Text = 'Stop Timer'
  265.         $intervalComboBox.Enabled = $false
  266.         $logButton.Enabled = $false
  267.         Write-Console $('Timer Started')
  268.     }
  269. })
  270.  
  271. $logButton.Add_Click({
  272.     # Open Log Button
  273.     if(Test-Path $Script:log_file){
  274.         Start-Process $Script:log_file
  275.         Write-Console $('Opening Log File: ' + $Script:log_file)
  276.     }else{
  277.         Write-Console $('Unable to locate Log File: ' + $Script:log_file) -Warning
  278.     }
  279. })
  280.  
  281. $sitesButton.Add_Click({
  282.     # Sites file Button
  283.     if(Test-Path $Script:sites_file){
  284.         Start-Process $Script:sites_file
  285.         Write-Console $('Opening Sites File: ' + $Script:sites_file)
  286.     }else{
  287.         Write-Console $('Unable to locate Sites File: ' + $Script:sites_file) -Warning
  288.     }
  289. })
  290.  
  291. $intervalComboBox.Add_SelectedIndexChanged({
  292.     $Timer1.Interval = [int]$intervalComboBox.Text * 1000
  293.     Write-Console $('Changed Timer Interval: ' + $Timer1.Interval)
  294. })
  295.  
  296. $debugCheckBox.Add_Click({
  297.     if($debugCheckBox.Checked){
  298.         Show-Console
  299.         Write-Console $('Show Console') -Warning
  300.     }else{
  301.         Hide-Console
  302.         Write-Console $('Hide Console') -Warning
  303.     }
  304. })
  305.  
  306. $Form.Add_KeyDown({
  307.     if ($PSItem.KeyCode -eq 'F1'){
  308.          Write-Console $('User Pressed F1 Key')
  309.     }
  310. })
  311.  
  312. #Show the form
  313. $Form.ShowDialog() | Out-Null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement