0

Here is a script I used to check status of my URL whether it is working or not. I need to write a script in such a way that it accepts all the URLs present in a text file which is placed in local drive and displays their information.

[string] $url = 'http://mywebsite.net'
function CheckForStatus($url) {
try {
    [net.httpWebRequest] $req = [net.webRequest]::create($url)
    $req.Method = "HEAD"
    [net.httpWebResponse] $res = $req.getResponse()
    if ($res.StatusCode -eq "200") {
        write-host "`nSite $url is up (Return code: $($res.StatusCode) - 
$([int] $res.StatusCode))`n" -ForegroundColor green 
    }
    else {
       write-host "`nSite $url is not available (Return code: 
$($res.StatusCode) - $([int] $res.StatusCode))`n" -ForegroundColor red
    }
} catch {
    write-host "`nSite $url is having some DNS issues`n" -ForegroundColor 
red
}
}
CheckForStatus $url
0

1 Answer 1

3

Use the Get-Content cmdlet and pipe it for processing each line.

Get-Content "some_file" | % { CheckForStatus($_) }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.