Solved

Script to See If URL is Accessable

Posted on 2014-10-09
82
122 Views
Last Modified: 2014-10-19
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
0
Comment
Question by:compdigit44
  • 46
  • 29
  • 7
82 Comments
 
LVL 17

Accepted Solution

by:
vb_elmar earned 500 total points
Comment Utility
Set oFS = CreateObject("Scripting.FileSystemObject")
Set outFile = oFS.CreateTextFile("c:\sample.txt")
    outFile.WriteLine "\\ServerA\Share" & ";" & URLExists("http://ServerA.com/Share") & ";"
    outFile.WriteLine "\\ServerB\Share" & ";" & URLExists("http://ServerB.com/Share") & ";"
    outFile.WriteLine "\\ServerC\Share" & ";" & URLExists("http://ServerC.com/Share") & ";"
outFile.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

0
 
LVL 35

Expert Comment

by:Joseph Daly
Comment Utility
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
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
I will give this a try tomorrow and let you know how I make out..

Thanks Again.
0
 
LVL 35

Expert Comment

by:Joseph Daly
Comment Utility
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.
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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
0
 
LVL 35

Expert Comment

by:Joseph Daly
Comment Utility
Can you post the csv?
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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
0
 
LVL 35

Expert Comment

by:Joseph Daly
Comment Utility
You don't have a heading listed.
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
Stupid question... What do you mean by heading... Column heading...?
0
 
LVL 35

Expert Comment

by:Joseph Daly
Comment Utility
Like this.
filename.csv
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
I tried adding a name to column one row one and got the same message
0
 
LVL 35

Expert Comment

by:Joseph Daly
Comment Utility
The name in the column has to match the name in the script.
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
Where do I put the column name in the script... sorry
0
 
LVL 35

Expert Comment

by:Joseph Daly
Comment Utility
The $_. References the name of heading for the column
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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....
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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.
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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.
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
tried typing in test-path \\server\share of a working URL and it came up false.
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
vb_elmar I looked at your script but do not see where the script references as source CSV file
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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...
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
Any way to just dump the results to file and not dialog box since I have a large csv file..
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
Thanks any thought on my my CSV isn't working...
I am sure it has to so with the spaces and qutoes
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
I will try it when I able back at the office tomorrow...

Thanks Again for the help everyone
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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.
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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'.
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
The message box list all share names correctly...
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
Try to replace
fso.FolderExists(L2)
by
(fso.FolderExists(L2))
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
All UNC paths still return as false....
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
Yes all UNC path name so as false in the MSG Box.
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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
ee.gif
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
I will try this tomorrow.. could this script read a CSV file??
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
Thanks but I am getting the following script error

Line: 21
Char: 1
Error: 0x80041002
Code: 80041002
Source: (Null)
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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..
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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...
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
I think this is the problem line

Set oWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & L77)
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
instead ...
Set oWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & L77)
try ...
Set oWMI = GetObject("winmgmts:" & L77)
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
same error...
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
Error line: "On Error GoTo 99"

States syntax error
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
instead :
on error goto 99
try :
on error goto on_error

the
99:
has to be replaced by:
on_error
,also
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
Same syntax error on line 11 again..
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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?
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
>>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
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
OK I do no understand the syntax error
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
Does the MsgBox return a clean spelled 'UNC path' if the error occurs?
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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 ")
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
Error on line 21 again...
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
New error....
Synatax error on :     Set oWMI = GetObject("winmgmts:" & L77)
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
I have read posting on line that other have had problems with this.. I have tried some of the posted suggestions on no luck
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
Can you make a MsgBox when the error occurs to check if the 'UNC path' is clean spelled?
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
How can I do this?
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
Error line 20 expect statement
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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

0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
Error on line 25   Set oWMI = GetObject("winmgmts:" & L77)
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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).
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
Ok  I changed it to "1" and get a message box then the same error on line 25 message
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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 .
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
The MSGBox did not return the name of the UNC but was blank
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
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.
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
I tried created a text file with one know good URL and got the same exact error..
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
You could start a new thread and ask this question again.
0
 
LVL 19

Author Comment

by:compdigit44
Comment Utility
thank you for all your help ... I start opened a new question :-)
0
 
LVL 19

Author Closing Comment

by:compdigit44
Comment Utility
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now