无法将参数绑定到参数' InputObject'因为它是null

时间:2017-10-06 11:51:57

标签: powershell powershell-v2.0 powershell-v3.0

我有一个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

2 个答案:

答案 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