如何在 SharePoint2013 预热脚本中捕获浏览器错误(如 504)?

How can I catch Browsererrors like 504 in a SharePoint2013 Warm-Up Skript?

我是 PowerShell 的新生,我尝试实施 SharePoint 预热脚本。 这个脚本不是我的,我只编辑了一个现有的。现在 运行 很好。但是如果发生像 DNS 查找失败这样的错误,我希望在我的日志文件中有一个输出,例如:"A DNS look up fail occured at URL..." 我想捕获浏览器错误 400、404 和 504。我该如何捕获它们?

这里是我的代码:

Function WarmUp() {

    # Get URL list
    Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    $was = Get-SPWebApplication -IncludeCentralAdministration
    $was |? {$_.IsAdministrationWebApplication -eq $true} |% {$caTitle = Get-SPWeb $_.Url | Select Title}

    # Warm up SharePoint web applications
    Write-Host "Opening Web Applications..."
    $global:ie = New-Object -Com "InternetExplorer.Application"
    $global:ie.Navigate("about:blank")
    $global:ie.Visible = $true
    $global:ieproc = (Get-Process -Name iexplore)|? {$_.MainWindowHandle -eq $global:ie.HWND}

    #Navigate here to all Applications, Collections and sites


    # Close IE window
    if ($global:ie) {
        Write-Host "Closing IE"    
        $global:ie | Stop-Process -Force -ErrorAction SilentlyContinue
    }
    $global:ieproc | Stop-Process -Force -ErrorAction SilentlyContinue

    # Clean Temporary Files
    Remove-item "$env:systemroot\system32\config\systemprofile\appdata\local\microsoft\Windows\temporary internet files\content.ie5\*.*" -Recurse -ErrorAction SilentlyContinue
    Remove-item "$env:systemroot\syswow64\config\systemprofile\appdata\local\microsoft\Windows\temporary internet files\content.ie5\*.*" -Recurse -ErrorAction SilentlyContinue
}

Function IENavigateTo([string] $url, [int] $delayTime = 500) {
    # Navigate to a given URL
    if ($url) {
        if ($url.ToUpper().StartsWith("HTTP")) {
            Write-Host "  Navigating to $url"
            try {
                $global:ie.Navigate($url)

                #If the certificate is invalid, bypass the error to show the context.
                if ($global:ie.document.url -Match "invalidcert")
                {
                    "Bypassing SSL Certificate Error Page";
                    $sslbypass=$global:ie.Document.getElementsByTagName("a") | where-object {$_.id -eq "overridelink"};
                    $sslbypass.click();
                    "Sleep for 5 seconds while final page loads";
                    start-sleep -s 5;
                }                  

            } catch {
                try {
                    $pid = $global:ieproc.id
                } catch {}
                Write-Host "  IE not responding.  Closing process ID $pid"
                $global:ie | Stop-Process -Force -ErrorAction SilentlyContinue
                $global:ieproc | Stop-Process -Force -ErrorAction SilentlyContinue
                $global:ie = New-Object -Com "InternetExplorer.Application"
                $global:ie.Navigate("about:blank")
                $global:ie.Visible = $true
                $global:ieproc = (Get-Process -Name iexplore)|? {$_.MainWindowHandle -eq $global:ie.HWND}
            }
            IEWaitForPage $delayTime
        }
    }
}

Function IEWaitForPage([int] $delayTime = 500) {
    # Wait for current page to finish loading
    $loaded = $false
    $loop = 0
    $maxLoop = 20
    while ($loaded -eq $false) {
        $loop++

        # Wait until the page is loaded.
        While ($global:ie.ReadyState -ne 4) {
        Start-Sleep -Seconds 2
        Write-Host "Busy!"
        }
        if ($loop -gt $maxLoop) {
            $loaded = $true
        }
        [System.Threading.Thread]::Sleep($delayTime) 
        # If the browser is not busy, the page is loaded
        if (-not $global:ie.Busy)
        {
            $loaded = $true
        }
    }
}

#Main
if(-not(Test-Path "C:\Logs\SharePoint\WarmUpLogTest")){New-Item -ItemType Directory -Path "C:\Logs\SharePoint\WarmUpLogTest"}
Start-Transcript -Path ("C:\Logs\SharePoint\WarmUpLogTest\SPWarmUp{0:yyyy-MM-dd}.txt"  -f $(get-date)) -Append

WarmUp

Stop-Transcript

如果您 运行 powershell V3 或最新版本,您可以将其添加到 IENavigateTo

之上
try{invoke-webrequest $url}
catch{$_.errordetails|select -expand message}  

如果没有 V3,您可以尝试

$request = [System.Net.WebRequest]::Create($url)   
try{$response = $request.GetResponse() }catch{$_.exception.message}