Test URL on Server Core machines using PowerShell

Recently I had to test URL on server core machines. I created simple scripts to generate report with just basic information like status code and content. Below you can find short description how to check http and https URLs.

Test URL

For checking http URL I used Invoke-WebRequest command. Unfortunately when I run it localy on server core machine it returned me an error:

Invoke-WebRequest "http://www.bing.com/"  | Select-Object statuscode,statusdescription,content

Error message:
The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer’s first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again.

To fix this we just have to use one of the parameters -UseBasicParsing:

Invoke-WebRequest "http://www.bing.com/" -UseBasicParsing | Select-Object statuscode,statusdescription,content

You can run this command remotely by adding this in Scriptblock:

$Server = "Server01"
$Url = "http://www.bing.com/"
$HttpQuery = Invoke-command $Server -Scriptblock{param($url)Invoke-WebRequest $url -UseBasicParsing | Select-Object statuscode,statusdescription,content } -ArgumentList $Url

Below you can find full script that can be run for multiple machines. Resuls will be added to an array and displayed in new pop-up window.
For more information about this command please visit docs Microsoft page.

Final script (http):

#=======================================================================
$Report = @()
$Url = "http://www.bing.com/"
$Servers = Get-Content "c:\users\$env:username\desktop\input.txt"
#=======================================================================
ForEach ($Server in $Servers) {
    $HttpQuery = $Object = $null
    $Server = $Server.trim()
    
    Write-Host "Processing $Server"
    Try {
        $HttpQuery = Invoke-command $Server -Scriptblock{param($Url)Invoke-WebRequest $Url -UseBasicParsing | Select-Object statuscode,statusdescription,content } -ArgumentList $Url
        If ($HttpQuery) {
            $Object = New-Object PSObject -Property ([ordered]@{ 
 
                ServerName              = $Server
                URL                     = $url 
                Status                  = $HttpQuery.statusdescription
                StatusCode              = $HttpQuery.statuscode
                Content                 = $HttpQuery.content
                
            })
            $Report += $Object
        }
    }
    Catch {
        $_.Exception.Message
        Continue
    }
}

$Report | Out-GridView -Title "Final results"

What if we need to test https URLs?
For example you have simple https test application based on server IP addresses "https://192.120.10.10/TestURL/" which returns success or failed value. To test this we can just use .Net class New-Object System.Net.WebClient

Final script (https):

    $Results = @()
    $Servers = Get-Content "D:\scripts\input.txt"
    $WebClient = New-Object System.Net.WebClient

    Foreach($Computername in $Servers) {
        $Computername = $Computername.Trim()
        Write-Host "Processing $Computername"
        $Object = $FQDN = $Ip = $URLTest = $Content = $ip = $TestUrl = $null
        $FQDN = ([System.Net.Dns]::GetHostByName(("$Computername")))
 
        If(!$FQDN) {
            Write-Warning "$Computername does not exist"
        }
        Else {

            Try {
                $Ip = $FQDN.AddressList[0].IPAddressToString
                $TestUrl = "https://$ip/TestURL/"

                [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
                $URLTest = $WebClient.DownloadString($TestUrl)

                If($URLTest) {
                    $Content = (($URLTest -split '\n') | Where-Object {$_ -match "success|failed"}).Trim()
                    $Object = New-Object PSObject -Property ([ordered]@{ 
    
                        Server                  = $Computername
                        IPAddress               = $Ip
                        TestURL                 = $TestUrl
                        Content                 = $Content
                    
                    })
                    $Results += $Object
                }
            }
            Catch {
                $_.Exception.Message
                Continue
            }
        }
    }

    $Results | Out-GridView -Title "Test results"
    $Results  | Export-Csv -Path C:\users\$env:username\desktop\TestResults.csv -NoTypeInformation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.