我有一个PowerShell脚本可以测量某些页面的下载时间,但是我得到上面的错误,我不确定我做错了什么
错误是
无法将参数绑定到参数' InputObject'因为它是空的。
function ResponseTime($CommonName,$URL, $environment)
{
$Times = 5
$i = 0
$TotalResponseTime = 0
Write-HOst $URL
While ($i -lt $Times) {
$Request = New-Object System.Net.WebClient
$Request.UseDefaultCredentials = $true
$Start = Get-Date
Write-HOst $URL
$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
$random = Get-Random -minimum 1 -maximum 30
Start-Sleep -s $random
}
#PRODUCTION
ResponseTime -commonname 'app homepage' -URL 'https://url1' -environment 'PRODUCTION'
ResponseTime -commonname 'department homepage' -URL 'https://url2' -environment 'PRODUCTION'
$results | export-csv -Path c:\so.csv -NoTypeInformation
答案 0 :(得分:1)
审核上次修改时,$results
似乎只返回$null
(正如您的错误所示)
唯一的行设置$results
是$results += New-Object PSObject -Property $details
这不在你Export-CSV
电话的范围内 - 即使这样,如果没有调用此行,$结果可能为空。
你应该恕我直言将其设置为例如一个ArrayList如下:
$results = New-Object -TypeName System.Collections.ArrayList
并通过
添加项目$times = ResponseTime -commonname '' #etc
$results.Add($times) | Out-Null
这为您提供了一个ArrayList - 即使其中没有项目 - 可以很容易地转换为CSV和其他格式。
答案 1 :(得分:1)
@Clijsters给出了正确的答案;即问题是$results
变量的范围。
这个答案只提供了一点code review来帮助你推进其他比赛...
function Get-ResponseTime {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$CommonName
,
[Parameter(Mandatory = $true)]
[string]$URL
,
[Parameter(Mandatory = $true)]
[string]$Environment
,
[Parameter(Mandatory = $false)]
[int]$Times = 5
)
[System.Int64]$TotalResponseTime = 0
[System.Diagnostics.Stopwatch]$stopwatch = New-Object 'System.Diagnostics.Stopwatch'
Write-Verbose "Processing URL: $URL"
1..$times | foreach-object {
[System.Net.WebClient]$Request = New-Object 'System.Net.WebClient'
$Request.UseDefaultCredentials = $true
Write-Verboset "Call $_ to URL: $URL"
$stopwatch.Restart()
$PageRequest = $Request.DownloadString($URL)
$stopwatch.Stop()
$TimeTaken = $stopwatch.Elapsed.TotalMilliseconds
$Request.Dispose()
$TotalResponseTime += $TimeTaken
}
$AverageResponseTime = $TotalResponseTime / $Times
Write-Verbose "Request to $CommonName took $AverageResponseTime ms on average"
$details = @{
Date = get-date
AverageResponseTime = $AverageResponseTime
#ResponseTime = $Destination #this is not declared anywhere / don't know what this field's for
Environment = $environment
}
Write-Output (New-Object 'PSObject' -Property $details)
#do you really want a delay here? Doesn't make much sense... may make sense to include a delay in the above loop; i.e. to stagger your tests?
#$random = Get-Random -minimum 1 -maximum 30
#Start-Sleep -s $random
}
#PRODUCTION
[PSObject[]]$results = @(
(Get-ResponseTime -commonname 'app homepage' -URL 'https://url1' -environment 'PRODUCTION' -Verbose)
,(Get-ResponseTime -commonname 'department homepage' -URL 'https://url2' -environment 'PRODUCTION' -Verbose)
)
$results | Export-Csv -LiteralPath 'c:\so.csv' -NoTypeInformation
Get-Item
)。 What is the naming convention for Powershell functions with regard to upper/lower case usage? Cmdlets
”(高级功能)代替(基本)Functions
;它们基本上是一样的,只用[Cmdletbinding()]
标记。这样做的原因是您获得了详细输出等功能的支持。 http://www.lazywinadmin.com/2015/03/standard-and-advanced-powershell.html measure-command
;但measure-command
函数会抑制/消耗任何输出。 Timing a command's execution in PowerShell Write-Output
将其值输出到管道(或者您可以不使用函数名称;通过放置一个没有任何处理它的变量而导致的任何输出都将被提供给管道;即{{ 1}}与仅由write-object $a
)组成的行相同。