Link to home
Start Free TrialLog in
Avatar of compdigit44
compdigit44

asked on

Script to See If URL is Accessable

I have a windows 7 workstation and looking for a way to check the status of a URL's on our network to see if they are still valid.  I am trying to do a lot of cleanup work.

 I would like to reference a CSV file and have he script check/ping each URL and write the status to a Text file.

Example

URL \\ServerA\Share  -> No Reachable
URL \\ServerB\Share -> Reachable
ASKER CERTIFIED SOLUTION
Avatar of vb_elmar
vb_elmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Joseph Daly
Powershell is hugely easier for this task.

test-path \\server\share

If its reachable it returns true if not it returns false.

import-csv c:\filename.csv | foreach {
$exists = test-path $_.pathname
$path = $_.pathname
"$path, $exists" >> C:\file.txt
}

This will outut something like

\\server\share, true
\\server\share, false
Avatar of compdigit44
compdigit44

ASKER

I will give this a try tomorrow and let you know how I make out..

Thanks Again.
One thing to note with that powershell. The $_. denotes whatever your column heading in the csv file is.  The file must have a heading and then the values in a column underneath that.
I setup my CSV so it used one column with Cell one with a heading of Printers and all URL underneath it..

I get the error "Cannot bind argument to parameter 'path' is NULL
Can you post the csv?
I cannot post the CSV since it contains sensitive information

But it looks like the following in CSV format all using one column. Starting are column and row one .

\\ServerA\Share1
\\ShareA\Share2

Here is the exact error I am seeing....

Test-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\PrinterURLChecker.ps1:2 char:21
+ $exists = test-path $_.pathname
You don't have a heading listed.
Stupid question... What do you mean by heading... Column heading...?
I tried adding a name to column one row one and got the same message
The name in the column has to match the name in the script.
Where do I put the column name in the script... sorry
The $_. References the name of heading for the column
Thanks I was able to get it to run and learned something about test-path... One problem though.. the out-put file list are shares are false which i know is not true since they exist....
I think I found the problem... The URL I need to check have spaces. In CSV which is large how can I added quotes to all items easily.. I found how to add quotes to a columns using a custom format ..  I will keep you posted.
Still not working and showing everything as False...

Here is the custom format I used to place quote around everything...

https://answers.yahoo.com/question/index?qid=20100210103758AAsTyfK

Custom format - "''"@"''"
that is quote,2 apostrophes, quote,@, quote, 2 apostrophes, quote
Apply to column.
tried typing in test-path \\server\share of a working URL and it came up false.
vb_elmar I looked at your script but do not see where the script references as source CSV file
The following .vbs code reads the "filename.csf" starting from the second line and tells you each server status with a messagebox.
Set fso = CreateObject("Scripting.FileSystemObject")
Set inputFile = fso.OpenTextFile("filename.csv", 1)
Do While Not inputFile.AtEndOfStream
    L2 = inputFile.ReadLine
    If L2 <> "Pathname" Then
        WScript.Echo URLExists(L2)
    End If
Loop
inputFile.Close

Function URLExists(strURL)
    Dim o, Retval, isNumber404
    On Error Resume Next
    Set o = CreateObject("Microsoft.XMLHTTP")
    o.open "GET", strURL, False
    o.send
    If Err.Number = 0 Then
        Retval = o.responseText
        isNumber404 = Left(Retval, 1)
        If Asc(isNumber404) > 47 And Asc(isNumber404) < 58 Then 'chars 0-9
            URLExists = "No Reachable" ' HTTP Error code returned
        Else
            URLExists = "Reachable"
        End If
    Else
        URLExists = "No network or URL misspelled."
        Err.Clear
    End If
    Set o = Nothing
End Function

Open in new window

I get the error No error URL misspelled... My URL have spaces and I made sure to place quotes around them I am not sure if this causing the problem...
Any way to just dump the results to file and not dialog box since I have a large csv file..
This is how to write results to file:
Set outFile = fso.CreateTextFile("c:\sample.txt")
    outFile.WriteLine URLExists(L2)
outFile.Close

Open in new window

Thanks any thought on my my CSV isn't working...
I am sure it has to so with the spaces and qutoes
A working URL never contains quotes. To remove quotes from your CSV file replace the term ...
L2 = inputFile.ReadLine
If L2 <> "Pathname" Then

Open in new window

by the term ...
L2 = inputFile.ReadLine
L2 = Replace(L2, Chr(34), "") 'replacing quotes
If L2 <> "Pathname" Then

Open in new window

OK..

This may be the problem while the powershell script is not returning the proper values as well..

So my CSV should not contain quotes for example.. some of my urls look like this:

\\ServerA\Some Share Name

I am not a scripting person so I am trying to understand what you need me to do here..

Sorry if this is an obvious question/answer
To check an 'UNC path' like ...
\\ServerA\Some Share Name
... try this:
Set fso = CreateObject("Scripting.FileSystemObject")
Set inputFile = fso.OpenTextFile("filename.csv", 1)
Set outFile = fso.CreateTextFile("output.csv")
Do While Not inputFile.AtEndOfStream
    L2 = inputFile.ReadLine
    L2 = Replace(L2, Chr(34), "") 'remove all quotes "
    L2 = Replace(L2, ";", "")     'remove semicolons ;
    If L2 <> "Pathname" Then
    outFile.WriteLine L2 & ";" & fso.FolderExists(L2)
    End If
Loop
inputFile.Close
outFile.Close

Open in new window

I will try it when I able back at the office tomorrow...

Thanks Again for the help everyone
The script runs perfect BUT everything but all URL's come up as false. An I know the one'e listed are LIVE URL's..

I really think it has something to do with the fact there are spaces in the same.
The script from 2014-10-12 at 02:33:37 is designed for an 'UNC path' like ...
\\ServerA\Some Share Name
and it allows to process spaces.

-But it is not designed to process URL's containing colons like
e.g. http://

Please note that opening an URL requires another
method than opening an 'UNC path'.
Yes this is the script i am using and I am using UNC path's not URL's sorry ..

All UNC path return as FALSE which is not true because they are known good working URL's
I modifierd the code. It shows the  'UNC path' in a MsgBox before processing it. Is the 'UNC path' in the MsgBox correct?
Set fso = CreateObject("Scripting.FileSystemObject")
Set inputFile = fso.OpenTextFile("filename.csv", 1)
Set outFile = fso.CreateTextFile("output.csv")
Do While Not inputFile.AtEndOfStream
    L2 = inputFile.ReadLine
    L2 = Replace(L2, Chr(34), "") 'remove all quotes "
    L2 = Replace(L2, ";", "")     'remove semicolons ;
    If L2 <> "Pathname" Then
    WScript.Echo "->" & L2 & "<-"
    outFile.WriteLine L2 & ";" & fso.FolderExists(L2)
    End If
Loop
inputFile.Close
outFile.Close

Open in new window

The message box list all share names correctly...
Try to replace
fso.FolderExists(L2)
by
(fso.FolderExists(L2))
All UNC paths still return as false....
I modified the MsgBox again. Does the MsgBox show all 'UNC paths' false?
Dim L22, isOnline
Set fso = CreateObject("Scripting.FileSystemObject")
Set inputFile = fso.OpenTextFile("filename.csv", 1)
Set outFile = fso.CreateTextFile("output.csv")
Do While Not inputFile.AtEndOfStream
    L22 = inputFile.ReadLine
    L22 = Replace(L22, Chr(34), "") 'remove all quotes "
    L22 = Split(L22, ";")(0)
    If L22 <> "Pathname" Then
      isOnline = (fso.FolderExists(L22))
      WScript.Echo "->" & L22 & "<-" & vbCrLf & isOnline
      outFile.WriteLine L22 & ";" & isOnline
    End If
Loop
inputFile.Close
outFile.Close

Open in new window

Yes all UNC path name so as false in the MSG Box.
When I run the code from 2014-10-13 at 17:21:30 it shows a MsgBox:
In the 1st line I see the 'UNC path' and
in the 2nd line the MsgBox shows True
User generated image
Thank you for sticking with me on this. WIth 500 UNC path's to check I am hoping to get this. script to work.

I am still not having any luck. I copied and pasted all UNC paths from my Excel 2010 spreadsheet into a new clean CSV file and got the same result..

Yet when I take the same UNC path in go to start -> run the path bring me to the correct network location.

I even tried switching to lower case and it did not work
I do not understand why this will not work with powershell or VBS... I have upload a sample of what my UNC path's look like..
Sample-UNC.csv
I think line 1 should better be written as it is shown in line 2
because (as far I know) leading spaces are not allowed.
\\Server-1A1\ HP LaserJet 4050 H4
\\Server-1A1\HP LaserJet 4050 H4

Open in new window

I modified the code to prevent leading spaces.
Dim L22, isOnline
Set fso = CreateObject("Scripting.FileSystemObject")
Set inputFile = fso.OpenTextFile("filename.csv", 1)
Set outFile = fso.CreateTextFile("output.csv")
Do While Not inputFile.AtEndOfStream
    L22 = inputFile.ReadLine
    L22 = Replace(L22, Chr(34), "") 'remove all quotes "
    L22 = Replace(L22, "\ ", "\")   'prevent leading spaces
    L22 = Split(L22, ";")(0)
    If L22 <> "Pathname" Then
      isOnline = (fso.FolderExists(L22))
      WScript.Echo "->" & L22 & "<-" & vbCrLf & isOnline
      outFile.WriteLine L22 & ";" & isOnline
    End If
Loop
inputFile.Close
outFile.Close

Open in new window

Here is a function to check if a printer is online:
WScript.Echo isPrinterOnline("\\Server-1A1\HP LaserJet 4050 H4")

Function isPrinterOnline(L77)
Dim o
Dim oWMI
Dim allPrinters
Set oWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & L77)
Set allPrinters = oWMI.ExecQuery("Select * from Win32_Printer where Default = 'True'")
For Each o In allPrinters
    Select Case o.PrinterStatus
    Case 3
        isPrinterOnline = "Printer Idle"
    Case 4
        isPrinterOnline = "Printer Printing"
    Case 5
        isPrinterOnline = "Printer WarmingUp"
    Case Else
        isPrinterOnline = "Printer OffLine"
    End Select
Next
End Function

Open in new window

I will try this tomorrow.. could this script read a CSV file??
The following sample reads the first column of a .csv file with the printer 'UNC path' information and checks if printer is online:
Dim L22, isDeviceOnline
Set fso = CreateObject("Scripting.FileSystemObject")
Set inputFile = fso.OpenTextFile("filename.csv", 1)
Set outFile = fso.CreateTextFile("output.csv")
Do While Not inputFile.AtEndOfStream
    L22 = inputFile.ReadLine
    L22 = Replace(L22, Chr(34), "") 'remove all quotes "
    L22 = Replace(L22, "\ ", "\")   'prevent leading spaces
    L22 = Split(L22, ";")(0) 'read first col of a .csv file
    If L22 <> "Pathname" Then
      isDeviceOnline = isPrinterOnline(L22)
      WScript.Echo "->" & L22 & "<-" & vbCrLf & isDeviceOnline      
      outFile.WriteLine L22 & ";" & isDeviceOnline
    End If
Loop
inputFile.Close
outFile.Close

Function isPrinterOnline(L77)
Dim o, oWMI, allPrinters
Set oWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & L77)
Set allPrinters = oWMI.ExecQuery("Select * from Win32_Printer where Default = 'True'")
For Each o In allPrinters
    Select Case o.PrinterStatus
    Case 3
        isPrinterOnline = "PrinterIdle"
    Case 4
        isPrinterOnline = "Printing"
    Case 5
        isPrinterOnline = "WarmingUp"
    Case Else
        isPrinterOnline = "PrinterOffLine"
    End Select
Next
End Function

Open in new window

Thanks but I am getting the following script error

Line: 21
Char: 1
Error: 0x80041002
Code: 80041002
Source: (Null)
OK since my new CSV does not have Quotes I removed the section of code that removes the quotes. The scripts run but the output file is blank..
Here is a script to obtain the IP address of a network printer. If the function returns '0' the printer isn't online:
Sub GetIP()
SRV = "servername"
Set oWMI = GetObject("winmgmts:\\" & SRV & "\root\cimv2")
Set allPrinters = oWMI.ExecQuery("Select * from Win32_Printer ", , 48)
For Each o In allPrinters
    Length = Len(o.PortName) - 3
    IP = Right(o.PortName, Length)
    WScript.Echo o.ShareName & vbCrLf & IP
Next
End Sub

Open in new window

OK the works but I need to check the URL because sometime... printer get renamed... so know the port will not help me as much sorry...
I think this is the problem line

Set oWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & L77)
instead ...
Set oWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & L77)
try ...
Set oWMI = GetObject("winmgmts:" & L77)
same error...
Did the Msgbox return an 'UNC path' that isn't  misspelled?
To check this I supplied the isPrinterOnline() function with
'on error goto' :
Dim L22, isDeviceOnline
Set fso = CreateObject("Scripting.FileSystemObject")
Set inputFile = fso.OpenTextFile("filename.csv", 1)
Set outFile = fso.CreateTextFile("output.csv")
Do While Not inputFile.AtEndOfStream
    L22 = inputFile.ReadLine
    L22 = Replace(L22, Chr(34), "") 'remove all quotes "
    L22 = Replace(L22, "\ ", "\")   'prevent leading spaces
    L22 = Split(L22, ";")(0) 'read first col of a .csv file
    If L22 <> "Pathname" Then
      isDeviceOnline = isPrinterOnline(L22)
      WScript.Echo "->" & L22 & "<-" & vbCrLf & isDeviceOnline
      outFile.WriteLine L22 & ";" & isDeviceOnline
    End If
Loop
inputFile.Close
outFile.Close

Function isPrinterOnline(L77)
Dim o, oWMI, allPrinters
On Error GoTo 99
Set oWMI = GetObject("winmgmts:" & L77)
Set allPrinters = oWMI.ExecQuery("Select * from Win32_Printer where Default = 'True'")
For Each o In allPrinters
    Select Case o.PrinterStatus
    Case 3
        isPrinterOnline = "PrinterIdle"
    Case 4
        isPrinterOnline = "Printing"
    Case 5
        isPrinterOnline = "WarmingUp"
    Case Else
        isPrinterOnline = "PrinterOffLine"
    End Select
Next
Exit Function
99:
WScript.Echo "Error" & vbCrLf & "->" & L77 & "<-"
End Function

Open in new window

Error line: "On Error GoTo 99"

States syntax error
instead :
on error goto 99
try :
on error goto on_error

the
99:
has to be replaced by:
on_error
,also
Same syntax error on line 11 again..
if am having problems with the powershell script and your reading the CSV file I am wondering if it is my CSV file. I have recreated multiple time though and it does not have quotes but my UNC paths do have spaces..

It it case sensative?
>>Is it case sensitive?
no, It is not case sensitive

>>it does not have quotes but my UNC paths do have spaces
it is allowed in 'UNC paths' to have spaces
OK I do no understand the syntax error
Does the MsgBox return a clean spelled 'UNC path' if the error occurs?
if the 'UNC path' is clean, try to replace ...
Set allPrinters = oWMI.ExecQuery("Select * from Win32_Printer where Default = 'True'")
by ...
Set allPrinters = oWMI.ExecQuery("Select * from Win32_Printer ")
Here is the script that I am running... It now runs but the output.csv file is blank......


Dim L22, isDeviceOnline
Set fso = CreateObject("Scripting.FileSystemObject")
Set inputFile = fso.OpenTextFile("C:\UNCCheck.csv", 1)
Set outFile = fso.CreateTextFile("C:\output.csv")

inputFile.Close
outFile.Close

Function isPrinterOnline(L77)
Dim o, oWMI, allPrinters
Set oWMI = GetObject("winmgmts:" & L77)
Set allPrinters = oWMI.ExecQuery("Select * from Win32_Printer ")
For Each o In allPrinters
    Select Case o.PrinterStatus
    Case 3
        isPrinterOnline = "PrinterIdle"
    Case 4
        isPrinterOnline = "Printing"
    Case 5
        isPrinterOnline = "WarmingUp"
    Case Else
        isPrinterOnline = "PrinterOffLine"
    End Select
Next
Exit Function
WScript.Echo "Error" & vbCrLf & "->" & L77 & "<-"
End Function
Dim L22, isDeviceOnline
Set fso = CreateObject("Scripting.FileSystemObject")
Set inputFile = fso.OpenTextFile("C:\UNCCheck.csv", 1)
Set outFile = fso.CreateTextFile("C:\output.csv")
Do While Not inputFile.AtEndOfStream
    L22 = inputFile.ReadLine
    L22 = Replace(L22, Chr(34), "") 'remove all quotes "
    L22 = Replace(L22, "\ ", "\")   'prevent leading spaces
    L22 = Split(L22, ";")(0) 'read first col of a .csv file
    If L22 <> "Pathname" Then
      isDeviceOnline = isPrinterOnline(L22)
      WScript.Echo "->" & L22 & "<-" & vbCrLf & isDeviceOnline
      outFile.WriteLine L22 & ";" & isDeviceOnline
    End If
Loop
inputFile.Close
outFile.Close

Function isPrinterOnline(L77)
Dim o, oWMI, allPrinters
On Error GoTo on_error
Set oWMI = GetObject("winmgmts:" & L77)
Set allPrinters = oWMI.ExecQuery("Select * from Win32_Printer ")
For Each o In allPrinters
    Select Case o.PrinterStatus
    Case 3
        isPrinterOnline = "PrinterIdle"
    Case 4
        isPrinterOnline = "Printing"
    Case 5
        isPrinterOnline = "WarmingUp"
    Case Else
        isPrinterOnline = "PrinterOffLine"
    End Select
Next
Exit Function
on_error:
WScript.Echo "Error" & vbCrLf & "->" & L77 & "<-"
End Function

Open in new window

Error on line 21 again...
I read an microsoft article. -They write : "VBScript does not support On Error GoTo. -Instead,
use On Error Resume Next and then check the Err.Number"

So, please delete the ...
'on error goto on_error'
statement in the vbs file
New error....
Synatax error on :     Set oWMI = GetObject("winmgmts:" & L77)
I have read posting on line that other have had problems with this.. I have tried some of the posted suggestions on no luck
Can you make a MsgBox when the error occurs to check if the 'UNC path' is clean spelled?
How can I do this?
I added a counter for the MsgBox. In the '10th' query a MsgBox appears
showing the 'UNC path'. This is the line:
If cnt = 10 Then ..
-You can change the value of '10' to the value you want.
Dim L22, isDeviceOnline
Set fso = CreateObject("Scripting.FileSystemObject")
Set inputFile = fso.OpenTextFile("C:\UNCCheck.csv", 1)
Set outFile = fso.CreateTextFile("C:\output.csv")
Do While Not inputFile.AtEndOfStream
    L22 = inputFile.ReadLine
    L22 = Replace(L22, Chr(34), "") 'remove all quotes "
    L22 = Replace(L22, "\ ", "\")   'prevent leading spaces
    L22 = Split(L22, ";")(0) 'read first col of a .csv file
    If L22 <> "Pathname" Then
      isDeviceOnline = isPrinterOnline(L22)
      outFile.WriteLine L22 & ";" & isDeviceOnline
    End If
Loop
inputFile.Close
outFile.Close

Function isPrinterOnline(L77)
Static cnt: cnt = cnt + 1
If cnt = 10 Then
   WScript.Echo "cnt = " & cnt & vbCrLf & "->" & L77 & "<-"
End If
Dim o, oWMI, allPrinters
Set oWMI = GetObject("winmgmts:" & L77)
Set allPrinters = oWMI.ExecQuery("Select * from Win32_Printer ")
For Each o In allPrinters
    Select Case o.PrinterStatus
    Case 3
        isPrinterOnline = "PrinterIdle"
    Case 4
        isPrinterOnline = "Printing"
    Case 5
        isPrinterOnline = "WarmingUp"
    Case Else
        isPrinterOnline = "PrinterOffLine"
    End Select
Next
End Function

Open in new window

Error line 20 expect statement
Dim L22, isDeviceOnline, cnt
Set fso = CreateObject("Scripting.FileSystemObject")
Set inputFile = fso.OpenTextFile("C:\UNCCheck.csv", 1)
Set outFile = fso.CreateTextFile("C:\output.csv")
Do While Not inputFile.AtEndOfStream
    L22 = inputFile.ReadLine
    L22 = Replace(L22, Chr(34), "") 'remove all quotes "
    L22 = Replace(L22, "\ ", "\")   'prevent leading spaces
    L22 = Split(L22, ";")(0) 'read first col of a .csv file
    If L22 <> "Pathname" Then
    cnt = cnt + 1
If cnt = 10 Then
   WScript.Echo "cnt = " & cnt & vbCrLf & "->" & L77 & "<-"
End If
      isDeviceOnline = isPrinterOnline(L22)
      WScript.Echo "->" & L22 & "<-" & vbCrLf & isDeviceOnline
      outFile.WriteLine L22 & ";" & isDeviceOnline
    End If
Loop
inputFile.Close
outFile.Close

Function isPrinterOnline(L77)
Dim o, oWMI, allPrinters
Set oWMI = GetObject("winmgmts:" & L77)
Set allPrinters = oWMI.ExecQuery("Select * from Win32_Printer ")
For Each o In allPrinters
    Select Case o.PrinterStatus
    Case 3
        isPrinterOnline = "PrinterIdle"
    Case 4
        isPrinterOnline = "Printing"
    Case 5
        isPrinterOnline = "WarmingUp"
    Case Else
        isPrinterOnline = "PrinterOffLine"
    End Select
Next
End Function

Open in new window

Error on line 25   Set oWMI = GetObject("winmgmts:" & L77)
I know the error.

Prior to the error appears you have to make sure the MsgBox
appears and showing you the 'UNC path' which causes the error.

Please set the value in the line
If cnt = 10 Then ...
to the value where the error appears.

This may be ...
If cnt = 1 Then ..
or
If cnt = 31 Then ..
or
If cnt = 860 Then ..

-You have to find it of yourself (by trying).
Ok  I changed it to "1" and get a message box then the same error on line 25 message
Did the Msgbox return an 'UNC path' that is spelled properly ?

Annotation:
instead of ...
If cnt = 10 Then ..
you can write as well ...
If cnt > 10 And cnt < 15 Then..

-This modification prevents you to click hundreds of MsgBoxes. Then you have to click only
MsgBox 11 , MsgBox 12 , MsgBox 13 and MsgBox 14 .
The MSGBox did not return the name of the UNC but was blank
Since he message box pops up when I type in a count of 1 and returns the nothing for the UNC path I can only assume the script is not able to read the CSV file for some reason....  

I will try changing this to a text file tomorrow but do not think it will make a difference.
I tried created a text file with one know good URL and got the same exact error..
You could start a new thread and ask this question again.
thank you for all your help ... I start opened a new question :-)
I wanted to give you created for this because your script is correct and does work for file shares. My problem is that I need it to work with printer shares but did not communicate this correctly in my original posting.