我正在尝试使用一个脚本查询我局域网中的互联网速度,该脚本仅在其低于某个速度时才向我发送警报(请参阅说明)

i am trying to Query internet speeds in my lan with a script that send me an alert only if its lower then something ( see description)

我有一个代码可以正常工作。我希望它发送不同的结果,而不是我已有的结果。

代码:

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 "Fake@gmail.com"

此脚本通过 smtp 将其输出发送到我的邮件。 如果某些字段的下载和上传速度低于 50(向下)和 5(向上),我希望它给我发送邮件。 Speedtest.exe 脚本的输出如下所示:

"

   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/********
 "

我想从 speedtest.exe 输出查询下载和上传速度。

类似于:

if ($DownloadSpeed < 50 && $UploadSpeed <5 )
  {
Send-Email -To "Fake@gmail.com" -Body False
  }

我知道我需要声明 $body 但它与我尝试构建的 IF 无关。

继续我的评论,您需要继续阅读 PowerShell operators,因为在您的代码尝试中您使用的是 C# 风格的运算符。

接下来,您将函数 Send-Email 与未在此处定义的参数 -Body 一起使用。
相反,为测量的下载和上传速度添加参数:

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..)
}

要从结果文件中解析出速度值,您可以这样做:

# 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 "Fake@gmail.com" -DownloadSpeed $downloadSpeed -UploadSpeed $uploadSpeed
    }
}

正则表达式详细信息:

(?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