Solved

Script to See If URL is Accessable

Posted on 2014-10-09
82
124 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
ID: 40371362
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
ID: 40371560
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
ID: 40371604
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
ID: 40371667
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
ID: 40372928
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
ID: 40372944
Can you post the csv?
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40373229
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
ID: 40373323
You don't have a heading listed.
0
 
LVL 19

Author Comment

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

Expert Comment

by:Joseph Daly
ID: 40373334
Like this.
filename.csv
0
 
LVL 19

Author Comment

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

Expert Comment

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

Author Comment

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

Expert Comment

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

Author Comment

by:compdigit44
ID: 40373560
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
ID: 40373592
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
ID: 40373649
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
ID: 40373655
tried typing in test-path \\server\share of a working URL and it came up false.
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40373693
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
ID: 40373697
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
ID: 40373735
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
ID: 40373737
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
ID: 40373904
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
ID: 40373917
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
ID: 40373958
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
ID: 40375058
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
ID: 40375235
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
ID: 40375755
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
ID: 40376868
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
ID: 40376987
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
ID: 40377012
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
ID: 40377044
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
ID: 40377095
The message box list all share names correctly...
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40377115
Try to replace
fso.FolderExists(L2)
by
(fso.FolderExists(L2))
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40377221
All UNC paths still return as false....
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40377309
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
ID: 40377340
Yes all UNC path name so as false in the MSG Box.
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40377359
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
ID: 40377543
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
ID: 40377703
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
ID: 40377928
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 17

Expert Comment

by:vb_elmar
ID: 40377975
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
ID: 40378127
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
ID: 40378811
I will try this tomorrow.. could this script read a CSV file??
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40379111
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
ID: 40379663
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
ID: 40379872
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
ID: 40379953
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
ID: 40380065
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
ID: 40380078
I think this is the problem line

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

Expert Comment

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

Author Comment

by:compdigit44
ID: 40380190
same error...
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40380268
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
ID: 40380363
Error line: "On Error GoTo 99"

States syntax error
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40380413
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
ID: 40380444
Same syntax error on line 11 again..
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40380453
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
ID: 40380480
>>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
ID: 40380491
OK I do no understand the syntax error
0
 
LVL 17

Expert Comment

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

Expert Comment

by:vb_elmar
ID: 40380495
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
ID: 40380531
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
ID: 40380567
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
ID: 40380575
Error on line 21 again...
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40380595
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
ID: 40380624
New error....
Synatax error on :     Set oWMI = GetObject("winmgmts:" & L77)
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40380626
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
ID: 40380631
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
ID: 40380670
How can I do this?
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40380708
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
ID: 40380722
Error line 20 expect statement
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40380758
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
ID: 40380768
Error on line 25   Set oWMI = GetObject("winmgmts:" & L77)
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40380791
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
ID: 40380824
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
ID: 40380916
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
ID: 40381207
The MSGBox did not return the name of the UNC but was blank
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40383246
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
ID: 40384148
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
ID: 40384159
You could start a new thread and ask this question again.
0
 
LVL 19

Author Comment

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

Author Closing Comment

by:compdigit44
ID: 40390437
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

16 Experts available now in Live!

Get 1:1 Help Now