【问题标题】:Cannot bind argument to parameter 'InputObject' because it is null无法将参数绑定到参数“InputObject”,因为它为空
【发布时间】:2018-03-18 05:19:06
【问题描述】:

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

【问题讨论】:

  • 您在哪一行收到错误消息?你尝试调试你的代码是什么?
  • Write-Host 之后的消息是否有引号?即Write-Host "Request to $CommonName took $AverageResponseTime ms in average" -ForegroundColor Green
  • 在我刚刚添加到原始问题的最后一行中,我错过了抱歉。
  • ps。仅供参考,通常建议您在大多数情况下避免使用Write-Host;有关更多信息,请参阅此帖子:stackoverflow.com/questions/38523369/…
  • 是的,所以$results 是空的。它应该是一个类似列表的对象,您可以在其中添加项目。

标签: powershell powershell-2.0 powershell-3.0


【解决方案1】:

查看您上次的编辑,$results 似乎只是返回 $null(正如您的错误所说)

唯一的行设置$results$results += New-Object PSObject -Property $details

它不在您的 Export-CSV 调用范围内,并且 - 即使它会,如果未调用此行,$results 也可能为空。

恕我直言,您应该将其设置为例如ArrayList 如下:

$results = New-Object -TypeName System.Collections.ArrayList

并通过

向其中添加项目
$times = ResponseTime -commonname '' #etc
$results.Add($times) | Out-Null

这为您提供了一个 ArrayList - 即使其中没​​有项目 - 也可以轻松转换为 CSV 和其他格式。

【讨论】:

    【解决方案2】:

    @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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 2017-03-09
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多