Public/Invoke-Monitor.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function Invoke-Monitor {

    <#
    .DESCRIPTION
    Monitor a down service until it is reachable. Provides time elapsed for the monitor and an audible indication when the services is back online.

    .Parameter Seconds
    Specify Seconds between tests

    .Parameter Service
    Specify the service to monitor

    .Parameter Port
    Specify the port to monitor

    .Example
    Invoke-Monitor -Service Microsoft.com -port 443

    .Link
    Get-Emote
    Test-Netconnection

    .Notes
    Requires the Admintoolbox.Fun module
    #>


    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]$Service,
        [Parameter(Mandatory = $true)]$Port,
        [Parameter(Mandatory = $true)]$Seconds
    )

    #Defined variables for writing to host later
    $Flip = Get-Emote -Name DoubleFlip
    $sunglasses = Get-Emote -Name sunglasses

    #Start Stopwatch
    $stopwatch = [system.diagnostics.stopwatch]::StartNew()

    do {
        #Write fail status and time elapsed
        Write-Host "Elapsed Time " -ForegroundColor Green -NoNewline
        Write-Host $stopwatch.Elapsed.Hours -ForegroundColor Yellow -NoNewline
        Write-Host ':' -ForegroundColor Yellow -NoNewline
        Write-Host $stopwatch.Elapsed.Minutes -ForegroundColor Yellow -NoNewline
        Write-Host ':' -ForegroundColor Yellow -NoNewline
        Write-Host $Stopwatch.Elapsed.Seconds -ForegroundColor Yellow -NoNewline
        Write-Host " (Hours:Minutes:Seconds)"
        Write-Host "It is Still Failing AHHHHHHHHHH! $Flip" -BackgroundColor black -ForegroundColor Red
        Write-Host " "

        #Test connection and Sleep
        $condition = Test-NetConnection -ComputerName $Service -Port $Port -InformationLevel Quiet -WarningAction 'SilentlyContinue'
        Start-Sleep -Seconds $seconds
    } until ($condition -like "True")

    #Reset the stopwatch
    $stopwatch.Stop()
    $stopwatch.Reset()

    #Write about success and play music
    Write-Host "Success" -ForegroundColor Green;
    Write-Host "$Sunglasses" -ForegroundColor Cyan
    Get-ImperialMarch
}