Home > Software engineering >  i am trying to Query internet speeds in my lan with a script that send me an alert only if its lower
i am trying to Query internet speeds in my lan with a script that send me an alert only if its lower

Time:09-28

i have a code which works fine . i want it to send a different results instead the ones i already have .

Code :

function SpeedTest{}
C:\Users\yigal\Desktop\Tester\speedtest.exe > C:\Users\yigal\Desktop\Results\file.txt
Start-Sleep -s 45 
function Send-Email() {
    param(
        [Parameter(mandatory=$true)][string]$To
        
    )
    $username   = (Get-Content -Path 'C:\credentials.txt')[0]
    $password   = (Get-Content -Path 'C:\credentials.txt')[1]
    $secstr     = New-Object -TypeName System.Security.SecureString
    $password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}

    $hash = @{
        from       = $username
        to         = $To
        smtpserver = "smtp.gmail.com"
        subject = Get-Date
        body = (Get-Content -Path 'C:\Users\yigal\Desktop\Results\file.txt')[7]   (Get-Content -Path 'C:\Users\yigal\Desktop\Results\file.txt')[9] 
        credential = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
        usessl     = $true
        verbose    = $true
    }

    Send-MailMessage @hash
}
Start-Sleep -s 45
Send-Email -To "[email protected]"

This script sends its output to my mail via smtp . I want it to send me a mail if a certain fields the download and upload speeds are for example lower then 50 (down) and 5 (up) . my output from the Speedtest.exe script is shown like this :

"

   Speedtest by Ookla

     Server: ******** - ******** (id = ********)
        ISP: ********
    Latency:    13.18 ms   (2.14 ms jitter)

   Download:    30.47 Mbps (data used: 27.1 MB)                               

     Upload:    10.04 Mbps (data used: 11.7 MB)                               
Packet Loss:     0.0%
 Result URL: https://www.speedtest.net/result/c/********
 "

i want to query the download and upload speeds from the speedtest.exe output.

something like :

if ($DownloadSpeed < 50 && $UploadSpeed <5 )
  {
Send-Email -To "[email protected]" -Body False
  }

I know i need to declare $body but its irrelevant for the IF i am trying to build.

CodePudding user response:

Continuing from my comment, you need to read up on PowerShell operators, because in your code attempt you're using C# style operators.

Next, you use your function Send-Email with a parameter -Body which is not defined there.
Instead, add parameters for the measured Dowload and Upload speed:

function Send-Email {
    param(
        [Parameter(mandatory=$true)][string]$To
        [Parameter(mandatory=$true)][double]$DownloadSpeed
        [Parameter(mandatory=$true)][double]$UploadSpeed
    )
    # rest of the code (I didn't check if that works or not..)
}

To parse out the speed values from the results file, you could do this:

# read the results written by the speedtest.exe as single multi-line string (use -Raw)
$speedResult = Get-Content -Path 'C:\Users\yigal\Desktop\Results\file.txt' -Raw
# test if we can parse out the values for down- and upload speed
if ($speedResult -match '(?s)Download:\s*(?<down>\d (?:\.\d )?) Mbps.*Upload:\s*(?<up>\d (?:\.\d )?) Mbps') {
    $downloadSpeed = [double]$matches['down']
    $uploadSpeed   = [double]$matches['up']
    # if down speed less than 50 AND up speed less than 5
    if ($downloadSpeed -lt 50 -and $uploadSpeed -lt 5) {
        # send your email here.
        Send-Email -To "[email protected]" -DownloadSpeed $downloadSpeed -UploadSpeed $uploadSpeed
    }
}

Regex details:

(?s)             The dot also matches newline characters
Download:        Match the characters “Download:” literally
\s               Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   *             Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?<down>         Match the regular expression below and capture its match into backreference with name “down”
   \d            Match a single digit 0..9
                 Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   (?:           Match the regular expression below
      \.         Match the character “.” literally
      \d         Match a single digit 0..9
                 Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   )?            Between zero and one times, as many times as possible, giving back as needed (greedy)
)                
\ Mbps           Match the characters “ Mbps” literally
.                Match any single character
   *             Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Upload:          Match the characters “Upload:” literally
\s               Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   *             Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?<up>           Match the regular expression below and capture its match into backreference with name “up”
   \d            Match a single digit 0..9
                 Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   (?:           Match the regular expression below
      \.         Match the character “.” literally
      \d         Match a single digit 0..9
                 Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   )?            Between zero and one times, as many times as possible, giving back as needed (greedy)
)                
\ Mbps           Match the characters “ Mbps” literally
  • Related