无法绑定参数参数“InputObject”,因为它是空
问题描述:
我有某些页面上测量下载时间PowerShell脚本,但是我得到上述错误,我不能确定我在做什么错无法绑定参数参数“InputObject”,因为它是空
错误
无法将参数绑定到参数'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
答
@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 - 让您的cmdlet通过
Write-Output
将其值输出到管道中(或者您可以不使用函数名称;因为放置一个没有任何东西来处理它的变量而导致的任何输出将被馈送到管道;即write-object $a
与一行仅由$a
组成)。 - 将输出捕获到函数外的$ results变量中,并在那里处理结果。
+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等格式。
你会在哪一行得到错误信息?你尝试调试你的代码是什么? – Clijsters
'Write-Host'后的消息是否有引号?即对于CommonName的'Write-Host'请求平均花了$ AverageResponseTime ms“-ForegroundColor Green' – JohnLBevan
在我刚刚添加到原始问题的最后一行中,我错过了那个抱歉。 –