无法将参数 "address" 转换为值:"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[]"
我需要创建一个脚本来对 3 个不同的环境 url 发出请求,然后为我发送的每个页面生成一个 CSV 文件,其中包含每个环境的平均响应时间。
但是我得到这个错误:
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
您在 Powershell 中遇到了一个常见的陷阱。认为函数参数在定义中用逗号分隔,函数调用参数不是。如果使用逗号,Powershell 会将此类项目转换为数组。
在此特定情况下
ResponseTime 'app homepage' 'https://urlproduction', 'PRODUCTION'
被解析为
Call function ResponseTime with two parameters:
'app homepage'
和'https://urlproduction', 'PRODUCTION'
——其中后者是由两个元素组成的数组。
另一方面
ResponseTime 'app homepage' 'https://urlproduction' 'PRODUCTION'
被解析为
Call function ResponseTime with three parameters:
'app homepage'
、'https://urlproduction'
和 'PRODUCTION'
我需要创建一个脚本来对 3 个不同的环境 url 发出请求,然后为我发送的每个页面生成一个 CSV 文件,其中包含每个环境的平均响应时间。
但是我得到这个错误:
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
您在 Powershell 中遇到了一个常见的陷阱。认为函数参数在定义中用逗号分隔,函数调用参数不是。如果使用逗号,Powershell 会将此类项目转换为数组。
在此特定情况下
ResponseTime 'app homepage' 'https://urlproduction', 'PRODUCTION'
被解析为
Call function ResponseTime with two parameters:
'app homepage'
和'https://urlproduction', 'PRODUCTION'
——其中后者是由两个元素组成的数组。
另一方面
ResponseTime 'app homepage' 'https://urlproduction' 'PRODUCTION'
被解析为
Call function ResponseTime with three parameters:
'app homepage'
、'https://urlproduction'
和 'PRODUCTION'