執行下面的代碼以獲取Web服務器的值時出現此錯誤。從使用PowerShell的網站使用DownloadString時出錯
我已經閱讀了一些可能的解決方案,涉及更改Web服務器上的設置它是自我但這不是我的選擇,因爲我沒有擁有服務器。
錯誤
異常調用 「DownloadString」 與 「1」 的參數(一個或多個): 「的服務器 提交協議違例節= ResponseStatusLine。」 在 C:\ get.ps1:28 char:60 + $ ramPage =(New-Object System.Net.WebClient).DownloadString < < < <(「http://」+ $ server +「/ RAM /」+ $ evalRAMStars) + CategoryInfo:NotSpecified: (:) [],MethodInvocationException + FullyQualifiedErrorId:DotNetMeth odException
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.
}
我知道響應中有一個有效的值。 [以編程方式設置:](http://o2platform.wordpress.com/2010/10/20/dealing-with-the-server-committed-a-protocol-violation-sectionresponsestatusline/)似乎會工作,但沒有想法如何在PowerShell中做到這一點,你可能知道嗎? – justinf
@justinf你可以做'(New-Object System.Net.WebClient).DownloadString(「http://10.0.0.188/RAM/」+ $ evalRAMStars)'與$ evalRAMStars的價值,你有問題? $ evalRAMStars是一個數字,還是有一個尾隨或前導空間或其他字符?至於爲powershell使用app.config文件,請參閱http://stackoverflow.com/questions/17960/powershell-app-config。 – akton
$ evalRAMStars包含一個章程,這是一個1,我會嘗試執行(新對象System.Net.WebClient).DownloadString(「http://10.0.0.188/RAM/」+ $ evalRAMStars)一旦我回家稍後感謝您的幫助。 – justinf