Test-Uri

Test-Uri

Got a bunch or webservers you’d like to test the connection to? Maybe a file share that you want to test connection to a certain file. We can use powershell’s Invoke-WebRequest CmdLet with the -Method head. Head is better here than GET, because head will answer a web request with most of the information except the actual body. This makes it faster than GET and better suited to our purposes.

 

The function allows us to custom build our queries, so we can, for instance, do the following simple list out of the details. Here I have a txt file of URLs -one per line-called urls.txt located at C:\scripts\urls.txt. We’ll use Get-content (alias GC) to get the URLS one at time , then pipe them into the function test-uri and add the -Detail switch;

I put some good and some bad urls in the text file;

http://mattsalamon.com

http://google.com

http://www.msalamon.com/blog

http://drabble

 

Here is the output:

Or maybe we just want to get the bad ones:

Output:

Here we find that these 2 URLs are not good- Status code is 404–better check these out!

One quick note-when looking for the validity of say a file for download, the server may respond with a 200 OK Satus, but the file may not be valid. In this case we can look closely at the ContentType. If it is text instead of application, there may be a problem with the file.¬† so we can filter for contentType that is not equal to ‘application’ to fix this problem;

 

Finally, we pipe the results into where and only grab the ones that have a status code other than 200, since these are the problematic URLs that we want to know about. then we can take the results and send them to a file:

Here is the test-uri function, which I got from  petri:

 

 

 

 

 

 

You Might Also Like

2 Comments

Leave a Reply

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