2017-10-06 337 views
0

我有某些页面上测量下载时间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 
+1

你会在哪一行得到错误信息?你尝试调试你的代码是什么? – Clijsters

+0

'Write-Host'后的消息是否有引号?即对于CommonName的'Write-Host'请求平均花了$ AverageResponseTime ms“-ForegroundColor Green' – JohnLBevan

+0

在我刚刚添加到原始问题的最后一行中,我错过了那个抱歉。 –

回答

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 
+1

完美答案,谢谢 –

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等格式。

相关问题