0

I have modified a large file to give me just the urls that I want to run in powershell to check the status code and return them. I am new to powershell so I am having a few issues with my code. The file could have duplicate urls also.

I have this working when I just insert a single url. I have tried a few different things foreach Object, but I feel like I am close.

$urlArray = Import-Csv -Path "Scripts\test.csv" | Select -ExpandProperty urls


Function Get-WebStatus($url){

foreach ($url in $urlArray) {
    # First we create the request.
    $HTTP_Request = [System.Net.WebRequest]::Create($url)

    # We then get the HTTP code as an integer.
    $HTTP_Status = [int]$HTTP_Response.StatusCode

    If ($HTTP_Status -eq 200) {
        Write-Host "Site is Ok!"
    } Else {
        Write-Host $url + $HTTP_Request
    }
}
}

I just need a list of sites that don't work. Best case it is saved to its own txt file.

Not working example: google.com 404

Amanda
  • 31
  • 5
  • are you looking for a result similar to this one? https://stackoverflow.com/questions/20259251/powershell-script-to-check-the-status-of-a-url – Anthony Stringer Mar 28 '19 at 20:52
  • Something close to that. I found that thread, but was having issues pulling in my csv file. I kept getting errors. – Amanda Mar 28 '19 at 20:56
  • Related: [How to get 200 status code in PS after checking a redirected URL](https://stackoverflow.com/questions/54693287/) – JosefZ Mar 28 '19 at 22:28
  • all 200+ codes are ok, as well as 300+ redirecrs... it's better to check 400+ codes or 500+ – alexsuslin Mar 28 '19 at 23:42

2 Answers2

1

This should work:

Add-Type -AssemblyName System.Web
Add-Type -AssemblyName System.Web.Extensions

$urlArray = Import-Csv -Path "Scripts\test.csv" | Select -ExpandProperty urls

function Get-WebStatus {

    param( 
        $urlArray 
    )

    foreach ($url in $urlArray) {

        try {
            $request     = Invoke-WebRequest -Uri $url -Method GET
            $statusCode  = $request.StatusCode
        }
        catch {
            $statusCode  = $_.Exception.Response.StatusCode.value__
        }

        "$statusCode returned from $url"

    }
}

Get-WebStatus -urlArray $urlArray
f6a4
  • 1,684
  • 1
  • 10
  • 13
1

this is the function i use

function Get-UrlResponse ([string] $Url)
{
    try
    {
        $TotalMilliseconds = $(Measure-Command {
            $reply = Invoke-WebRequest -Uri $Url -UseBasicParsing -DisableKeepAlive -Method Head
            $statuscode = $reply.StatusCode
        }).TotalMilliseconds
    }
    catch [Net.WebException]
    {
        $statuscode = [int]$_.Exception.Response.StatusCode
    }

    [pscustomobject]@{
        URL = $url
        StatusCode = $statuscode
        TotalMilliseconds = $TotalMilliseconds
        Date = Get-Date -f yyyyMMdd-HHmmss
    }
}

$urlArray = Import-Csv -Path "Scripts\test.csv" | Select -ExpandProperty urls

foreach ($url in $urlArray) {
    Get-UrlResponse $url
}
Anthony Stringer
  • 1,981
  • 1
  • 10
  • 15