mfgwicked

OG Polling module

Dec 11th, 2024
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 1.37 KB | Source Code | 0 0
  1. class Custom_Polling {
  2.     [string]$Device
  3.     [string]$Status
  4.  
  5.     Custom_Polling([string]$Device) {
  6.         $this.Device = $Device
  7.         $this.Status = $this.Pulse($Device)
  8.     }
  9.  
  10.     [string] Pulse($Device) {
  11.         try {
  12.             $job = Start-Job -ScriptBlock {
  13.                 param($Device)
  14.                 Test-NetConnection -ComputerName $Device -Port 22
  15.             } -ArgumentList $Device | Wait-Job -Timeout 5
  16.             #
  17.             if ((Get-Job -Id $job.Id).State -eq 'Completed') {
  18.                 $result = 'UP'
  19.             }
  20.             else {
  21.                 $result = 'DOWN'
  22.             }
  23.         }
  24.         catch {
  25.             $job = Get-Job -Command "Test-NetConnection -Computername $Device -Port 22"
  26.             $result = 'ERROR'
  27.         }
  28.         finally {
  29.             if ($null -ne $job) {
  30.                 Remove-Job -Id $job.id
  31.             }
  32.         }
  33.         return $result
  34.     }
  35. }
  36. function Invoke-Polling {
  37.     <#
  38.     Query devices for SSH open and available and store as Polling active or down
  39.     #>
  40.     param ([Parameter(Mandatory=$True)][string]$Path)
  41.     if (Test-path -path $path) {
  42.         $Devices = Get-Content -Path $Path
  43.     }
  44.     $obj = New-Object System.Collections.ArrayList
  45.     foreach ($Dev in $Devices) {
  46.         $poll = [Custom_Polling]::new($Dev)
  47.         $obj.Add($poll)
  48.     }
  49.     Write-Output $obj
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment