【问题标题】:How To Delay Invoke-WebRequest Until Task is Complete or Make Invoke-WebRequest Actively Update如何延迟 Invoke-WebRequest 直到任务完成或使 Invoke-WebRequest 主动更新
【发布时间】:2023-02-24 22:02:18
【问题描述】:

我正在尝试向 speedtest.net 编写 Web 请求以运行互联网速度测试并返回结果,但由于速度测试需要时间并且我没有在 .content 中获得结果,我怀疑速度测试进程在完成后不会更新请求。也可能是 我不明白如何提取数据。我昨天刚刚了解了 invoke-webrequest,所以我可能还不知道如何使用它。此外,speedtest.exe 通过我们的网络被阻止,否则我会使用它。

我的脚本如下:

$URL = "www.speedtest.net/"
$site = Invoke-WebRequest $URL -UseBasicParsing
$Testpath = ($site.Links | Where-Object onclick -icontains "window.ookla.globals.shouldstartonload = true;").href
$site = Invoke-WebRequest "$url$testpath" -SessionVariable TestSession -UseBasicParsing

至于提取数据,我正在尝试使用它什么都不提取。我使用 Chrome 检查元素从 speedtest.net 获得了类名。我也试过使用 $site.split() 和使用 select-string

$site.content | where-object class -icontains "result-data-large number result-data-value download-speed"

【问题讨论】:

    标签: powershell invoke-webrequest


    【解决方案1】:

    你有没有想过如何做到这一点?

    我能够使用 cli 完成,但我也不想使用 exe。

    ######### Absolute monitoring values ##########
    $maxpacketloss = 2 #how much % packetloss until we alert.
    $MinimumDownloadSpeed = 100 #What is the minimum expected download speed in Mbit/ps
    $MinimumUploadSpeed = 2 #What is the minimum expected upload speed in Mbit/ps
    ######### End absolute monitoring values ######
    
    # Replace the Download URL to where you've uploaded the ZIP file yourself. We will only download this file once.
    # Latest version can be found at: https://www.speedtest.net/apps/cli
    $DownloadURL = "https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-win64.zip"
    $DownloadLocation = "$($Env:ProgramData)SpeedtestCLI"
    try {
    $TestDownloadLocation = Test-Path $DownloadLocation
        if (!$TestDownloadLocation) {
            New-Item $DownloadLocation -ItemType Directory -force
            Invoke-WebRequest -Uri $DownloadURL -OutFile "$($DownloadLocation)speedtest.zip"
            Expand-Archive "$($DownloadLocation)speedtest.zip" -DestinationPath $DownloadLocation -Force
        }
    }
    catch {
        Write-Output -InputObject "The download and extraction of SpeedtestCLI failed. Error: $($_.Exception.Message)"
    exit 1
    }
    $PreviousResults = if (test-path "$($DownloadLocation)LastResults.txt") { get-content "$($DownloadLocation)LastResults.txt" | ConvertFrom-Json }
    $SpeedtestResults = & "$($DownloadLocation)speedtest.exe" --format=json --accept-license --accept-gdpr
    $SpeedtestResults | Out-File "$($DownloadLocation)LastResults.txt" -Force
    $SpeedtestResults = $SpeedtestResults | ConvertFrom-Json
    
    # Creating object
    [PSCustomObject]$SpeedtestObj = @{
        downloadspeed = [math]::Round($SpeedtestResults.download.bandwidth / 125000, 2)
        uploadspeed = [math]::Round($SpeedtestResults.upload.bandwidth / 125000, 2)
        packetloss = [math]::Round($SpeedtestResults.packetLoss)
        isp           = $SpeedtestResults.isp
        ExternalIP    = $SpeedtestResults.interface.externalIp
        InternalIP    = $SpeedtestResults.interface.internalIp
        UsedServer    = $SpeedtestResults.server.host
        ResultsURL    = $SpeedtestResults.result.url
        Jitter        = [math]::Round($SpeedtestResults.ping.jitter)
        Latency = [math]::Round($SpeedtestResults.ping.latency)
    }
    $SpeedtestHealth = @()
    # Comparing against previous result. Alerting is download or upload differs more than 20%.
    if ($PreviousResults) {
        if ($PreviousResults.download.bandwidth / $SpeedtestResults.download.bandwidth * 100 -le 80) { $SpeedtestHealth += "Download speed difference is more than 20%" }
        if ($PreviousResults.upload.bandwidth / $SpeedtestResults.upload.bandwidth * 100 -le 80) { $SpeedtestHealth += "Upload speed difference is more than 20%" }
    }
    
    # Comparing against preset variables.
    if ($SpeedtestObj.downloadspeed -lt $MinimumDownloadSpeed) { $SpeedtestHealth += "Download speed is lower than $MinimumDownloadSpeed Mbit/ps" }
    if ($SpeedtestObj.uploadspeed -lt $MinimumUploadSpeed) { $SpeedtestHealth += "Upload speed is lower than $MinimumUploadSpeed Mbit/ps" }
    if ($SpeedtestObj.packetloss -gt $MaxPacketLoss) { $SpeedtestHealth += "Packetloss is higher than $maxpacketloss%" }
    
    if (!$SpeedtestHealth) {
    $SpeedtestHealth = "Healthy - ISP: $($speedtestObj.isp) Download Speed: $($SpeedtestObj.downloadspeed) Upload Speed: $($speedtestObj.uploadspeed) Jitter: $($speedtestObj.jitter) Lattency:Lattency: $($speedtestObj.Latency) PacketLoss: $($speedtestObj.packetLoss)"
    }
    
    Write-Output -InputObject $SpeedtestObj

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 2020-05-17
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多