简体   繁体   中英

Error when using DownloadString from a website using powershell

When executing the code below to get a value for a web server I get this error.

I have read some possible solutions that involve changing setting on the web server it's self but this is not an option for me because I don't own the server.

Error

Exception calling "DownloadString" with "1" argument(s): "The server committed a protocol violation. Section=ResponseStatusLine" At C:\\get.ps1:28 char:60 + $ramPage = (New-Object System.Net.WebClient).DownloadString <<<< ("http://" + $server + "/RAM/" + $evalRAMStars) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

Powershell Code

Function Get-Performance(){
$CPU = Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average

$SystemInfo = Get-WmiObject -Class Win32_OperatingSystem | Select-Object Name, TotalVisibleMemorySize, FreePhysicalMemory
$perRam = (($SystemInfo.FreePhysicalMemory/1MB) / ($systeminfo.totalvisiblememorysize/1MB)) * 100
$perRam = 100 - $perRam

Write-host ("CPU:  " + $CPU.Average + "%")
Write-host ("Ram:  " + $perRam + "%")

$CPUStars = $CPU.Average / 10
$RAMStars = $perRam /10

if($CPUStars -lt 1){$CPUStars = 1}

Write-host ("{0:N0}" -f $CPUStars)
Write-host ("{0:N0}" -f $RAMStars)

$evalCPUStars = "{0:N0}" -f $CPUStars
$evalRAMStars = "{0:N0}" -f $RAMStars

$server = "10.0.0.188"

#this will get our page for later
$cpuPage = (New-Object System.Net.WebClient).DownloadString("http://" + $server + "/CPU/" + $evalCPUStars)
$ramPage = (New-Object System.Net.WebClient).DownloadString("http://" + $server + "/RAM/" + $evalRAMStars)
Write-host("http://" + $server + "/CPU/" + $evalCPUStars)
Write-host("http://" + $server + "/RAM/" + $evalRAMStars)
}

While($x -ne 0)
{
    Get-Performance
    Start-Sleep -s 10   #this will run the powershell script every 10 seconds.
}

The value of $evalRAMStars is probably causing the unsafe header parsing to block the message. Check that its value is valid. Otherwise, see The server committed a protocol violation. Section=ResponseStatusLine ERROR for more solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM