使用PowerShell从网站使用DownloadString时出错

时间:2012-09-20 09:11:43

标签: .net powershell

执行下面的代码以获取Web服务器的值时,我收到此错误。

我已经阅读了一些可能的解决方案,涉及更改Web服务器上的设置,但这不是我的选择,因为我没有服务器。

错误

  

使用“1”参数调用“DownloadString”的异常:“服务器   提交了协议违规。 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代码

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

1 个答案:

答案 0 :(得分:0)

$ evalRAMStars的值可能导致不安全的头解析阻止消息。检查其值是否有效。否则,请参阅The server committed a protocol violation. Section=ResponseStatusLine ERROR以获取更多解决方案。