首页 > 解决方案 > 使用 Measure-Command 可以知道 http 请求时间,但是如何获得平均值、最小值和最大值呢?

问题描述

我有一个在 Sharpeoint 中查询某些内容的脚本,然后使用每个文档 ID,它执行一个 Web 服务,我熟悉 measure-command cmdlet 并且工作得很好。

但是我想修改下面的脚本,以获得请求所需的最小、最大和平均时间

代码正确地显示了整个脚本的总执行时间,但我想知道 TRY 块的总、平均、最小和最大执行时间,也

我试图将 measure-command 放入其中,但它会提示输入一个表达式,因此显然 2 个测量命令一个接一个嵌套是不可能的

 function Failure {
    $global:helpme = $body
    $global:helpmoref = $moref
    $global:result = $_.Exception.Response.GetResponseStream()
    $global:reader = New-Object System.IO.StreamReader($global:result)
    $global:responseBody = $global:reader.ReadToEnd();
    Write-Host -BackgroundColor:Black -ForegroundColor:Red "Status: A system exception was caught."
    Write-Host -BackgroundColor:Black -ForegroundColor:Red $global:responsebody
    Write-Host -BackgroundColor:Black -ForegroundColor:Red "The request body has been saved to `$global:helpme"
    break
}

 Measure-command {  
    If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )  
    { Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell } 

    $StartDate=(GET-DATE)
    $CutOffDate=(Get-date).AddDays(-540)
    $SPAssignment = Start-SPAssignment
    $SPWeb = Get-SPWeb https://oursite/sites/billing -AssignmentCollection $spAssignment
    # Next step is to get the list:
    $SPList = $SPWeb.Lists["Bill Cycles"]
     $spqQuery = New-Object Microsoft.SharePoint.SPQuery 
            $spqQuery.Query =  
                "   <Where> 
                    <And>
                        <Eq> 
                            <FieldRef Name='Bill_x0020_Preparation_x0020_Status' /> 
                            <Value Type='Text'>COMPLETED</Value> 
                        </Eq> 
                        <Leq>
                            <FieldRef Name='Bill_x0020_to_x0020_date' />
                            <Value Type='DateTime'>
                                <Today OffsetDays='540' />
                            </Value>
                        </Leq>
                    </And>
                </Where>" 
    $spqQuery.ViewFields = "<FieldRef Name='Bill_x0020_Preparation_x0020_Status' /><FieldRef Name='Title' /><FieldRef Name='Bill_x0020_to_x0020_date' /><FieldRef Name='ContentType'/><FieldRef Name='ID'/>" 
    $spqQuery.ViewFieldsOnly = $true 
    $splListItems = $SPList.GetItems($spqQuery) 
    $iNumber=1 
    $iNumberOfContentTypes=1 
    $iNumberOfBillCyclesArchived=0 
    $iNumberOfBillCyclesArchivedFailed=0 
    $cred = Get-Credential 
    foreach  ($splListItem in ($splListItems | Select-Object -First 10) )
    {
        $iNumber+=1 
        if( $splListItem["ContentType"] -eq "Bill Cycle"){           

           try {                
                    #Start-Sleep -s 65
                    $Url = "https://oursite/sites/billing/_vti_bin/DMS/DMSWebService.svc/Billing/ArchiveBillCycle/"+$splListItem["ID"]
                    #Write-Host $Url 
                    #https://oursite/sites/billing/_vti_bin/DMS/DMSWebService.svc/Billing/ArchiveBillCycle/145771
                    $r = Invoke-WebRequest -Credential $cred -Uri $Url   -TimeoutSec 180 -ErrorAction:Stop
                    #Write-Host $r.Content.ToString()
                    if($r.Content -like '*<RequestSucceeded>false</RequestSucceeded>*')
                    {
                        write-host "Archiving Failed, File # $iNumber - Bill Cycle Id:" $splListItem["ID"], "Title:" $splListItem["Title"] , "ContentType:" $splListItem["ContentType"] ,  "Status:" $splListItem["Bill_x0020_Preparation_x0020_Status"] , "Billtodate:" $splListItem["Bill_x0020_to_x0020_date"] -ForegroundColor Red 
                        $iNumberOfBillCyclesArchivedFailed+=1
                    }
                    elseif($r.Content -like '*<RequestSucceeded>true</RequestSucceeded>*')
                    {
                        write-host "Archived, File # $iNumber - Bill Cycle Id:" $splListItem["ID"], "Title:" $splListItem["Title"] , "ContentType:" $splListItem["ContentType"] ,  "Status:" $splListItem["Bill_x0020_Preparation_x0020_Status"] , "Billtodate:" $splListItem["Bill_x0020_to_x0020_date"] -ForegroundColor Green 
                        $iNumberOfBillCyclesArchived+=1
                    }
                    else{
                        write-host "Archived, File # $iNumber - Bill Cycle Id:" $splListItem["ID"], "Title:" $splListItem["Title"] , "ContentType:" $splListItem["ContentType"] ,  "Status:" $splListItem["Bill_x0020_Preparation_x0020_Status"] , "Billtodate:" $splListItem["Bill_x0020_to_x0020_date"] -ForegroundColor Green 
                        $iNumberOfBillCyclesArchived+=1
                    }   
           } 
           catch {
                $iNumberOfBillCyclesArchivedFailed+=1
                write-host "Archiving Failed, File # $iNumber - Bill Cycle Id:" $splListItem["ID"], "Title:" $splListItem["Title"] , "ContentType:" $splListItem["ContentType"] ,  "Status:" $splListItem["Bill_x0020_Preparation_x0020_Status"] , "Billtodate:" $splListItem["Bill_x0020_to_x0020_date"] -ForegroundColor Red 
                Failure
           }

           $iNumberOfContentTypes+=1
        }              
    } 

    Write-Host "Number of items in the query: "  $iNumber
    Write-Host "Number of content types in the query: "  $iNumberOfContentTypes
    Write-Host "Number of bill cycles archived: "  $iNumberOfBillCyclesArchived
    Write-Host "Number of bill cycles archived failed: "  $iNumberOfBillCyclesArchivedFailed


}

标签: powershell

解决方案


推荐阅读