2017-10-06 169 views
0

我需要創建一個腳本使在3個不同的環境URL的請求,然後生成與每個環境的平均響應時間的CSV文件,我發送的每個頁面無法轉換參數「地址」,值爲:「System.Object []」,爲「DownloadString」鍵入「System.Uri」:「無法轉換」System.Object []「

但是我得到這個錯誤:

Cannot convert argument "address", with value: "System.Object[]", for "DownloadString" to type "System.Uri": "Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Uri".

這裏是我的代碼:

function ResponseTime($CommonName,$URL, $environment) 
{ 
    $Times = 5 
    $i = 0 
    $TotalResponseTime = 0 

    While ($i -lt $Times) { 
     $Request = New-Object System.Net.WebClient 
     $Request.UseDefaultCredentials = $true 
     $Start = Get-Date 
     $PageRequest = $Request.DownloadString($URL) 
     $TimeTaken = ((Get-Date) - $Start).TotalMilliseconds 
     $Request.Dispose() 
     $i ++ 
     $TotalResponseTime += $TimeTaken 
    } 

    $AverageResponseTime = $TotalResponseTime/$i 
    Write-Host Request to $CommonName took $AverageResponseTime ms in average -ForegroundColor Green 

    $details = @{    
     Date    = get-date    
     AverageResponseTime  = $AverageResponseTime    
     ResponseTime  = $Destination 
     Environment = $environment 
    }       
    $results += New-Object PSObject -Property $details 

} 

ResponseTime 'app homepage' 'https://urlproduction', 'PRODUCTION' 
ResponseTime 'app homepage' 'https://urlQA', 'QA' 
ResponseTime 'app homepage' 'https://urltest', 'TEST' 

$results | export-csv -Path c:\so.csv -NoTypeInformation 
+1

爲什麼你有HTTPS之間的逗號:字符串//部分和「生產'... ? *提示* – t0mm13b

+1

另外,將網址轉換爲'[System.Uri]'。 – t0mm13b

回答

2

您在Powershell中遇到了一個常見問題。認爲函數參數在定義中用逗號分隔,函數調用參數不是。應使用逗號,Powershell將這些項目轉換爲數組。

在這種特定情況

ResponseTime 'app homepage' 'https://urlproduction', 'PRODUCTION' 

被解析爲

Call function ResponseTime with two paremters: 

'app homepage''https://urlproduction', 'PRODUCTION' - 其中後來是由兩個元件的陣列。

在另一方面

ResponseTime 'app homepage' 'https://urlproduction' 'PRODUCTION' 

被解析爲

Call function ResponseTime with three paremters: 

'app homepage''https://urlproduction''PRODUCTION'

相關問題