We help IT Professionals succeed at work.

Monitor UNC Path Availability?

We need to monitor the availability of a UNC at regular intervals and alert when the UNC path is unable to be accessed by a given user name and password.

Does anyone know of a free app that will do this?

Otherwise, it looks like a script may be needed.  Something that would access a UNC with a specific user name and password – would more than likely need to check for the presence of a file or something from the UNC location and then blat an alert if it is unable to do so.

I don’t really have a background in scripting or the time to learn \ test \ implement.  So an app would be great.

Otherwise, I could use some scripting examples that may help (full explanations needed).

Thanks.
Comment
Watch Question

Author

Commented:
Okay, say I am traveling down the scripting road on this one.

And I’m tackling it in this way:

Create a script that maps a drive to the UNC in question.  

Via Scheduled Tasks (win 2003 svr), the script can be set to map the drive at regular intervals with the correct account and password.

Now setting up the script to send an alert when the script is not able to map is another matter.

May need some advise here – keeping in mind that I do not have much experience in scripting (examples would be great).

Commented:
Acmi,

a simple:

IF EXIST \\<host_name>\<share_name> (ECHO --YES--) ELSE (ECHO --NO--)

Open in new window


would do it IF the user context that will run the script already has access to the share.

Otherwise you could use:

NET USE * \\<host_name>\<share_name> /USER:<domain_or_host>\<user_id> <password> & if errorlevel 1 ECHO --YES--

Open in new window


the two commands above can be used as a single line the command window.  They can be made more elaborate in a script to capture and log errors found but first see if this works for you, then we can improve it more.

Cheers!

Commented:
Great!

In the case of the Scheduled Task, you can run in the context of the user that you desire thus you should be able the first sample without having to resort to the second sample where you would need to hardcode the password in clear text.

Cheers!

Try this on a batch file:
@echo off	

ECHO ==IF DIRECTORY C:\TEMP  DOES NOT EXIST, CREATE IT
IF NOT EXIST C:\TEMP MAKEDIR C:\TEMP 


IF EXIST \\<host_name>\<share_name> (
  ECHO [%TIME% %DATE%]--SHARE IS STILL AVAILABE-- >> C:\TEMP\VALIDATE_SHARE.TXT
  ) ELSE (
  ECHO [%TIME% %DATE%]--ERROR!!, SHARE IS UNAVAILABE-- >> C:\TEMP\VALIDATE_SHARE.TXT
)

Open in new window

Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi, try something like this.

Regards,

Rob.
strBLAT = "C:\Tools\Blat.exe"
strDriveLetter = "Z:"
strUNCPath = "\\server\share"
strFile = "Z:\MyFile.txt"
strUsername = "domain\username"
strPassword = "password"
strSMTPServer = "mail.host.com"
strTo = "user@abc.com"
strFrom = "from@abc.com"
strSubject = "UNC Access Test"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
On Error Resume Next
If objFSO.DriveExists(strDriveLetter) = True Then
	objNetwork.RemoveNetworkDrive strDriveLetter, True, True
	WScript.Sleep 2000
End If
If Err.Number = 0 Then
	objNetwork.MapNetworkDrive strDriveLetter, strUNCPath, False, strUsername, strPassword
	If Err.Number = 0 Then
		WScript.Sleep 2000
		If objFSO.FileExists(strFile) = True Then
			strBody = strDriveLetter & " was mapped to " & strUNCPath & " and " & strFile & " was found."			
		Else
			strBody = strDriveLetter & " was mapped to " & strUNCPath & " but " & strFile & " was not found."
		End If
		WScript.Sleep 2000
		objNetwork.RemoveNetworkDrive strDriveLetter, True, True
	Else
		strBody = "Unable to map network drive " & strDriveLetter & " to " & strUNCPath & ". Error " & Err.Number & ": " & Err.Description
	End If
Else
	strBody = "Unable to remove network drive " & strDriveLetter & ". Error " & Err.Number & ": " & Err.Description
End If

Err.Clear
strBLAT = objFSO.GetFile(strBLAT).ShortPath
strCommand = "cmd /c " & strBLAT & " """ & strBody & """ -server " & strSMTPServer & " -to " & strTo & " -f " & strFrom & " -s """ & strSubject & """"
objShell.Run strCommand, 0, True

Open in new window

Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016

Commented:
Just a small remark about the net use command: you don't need the * as it will create connection and using an available drive letter. The net use command works perfectly without it like this:
NET USE \\<host_name>\<share_name> /USER:<domain_or_host>\<user_id> <password>

Open in new window

After testing you should 'disconnect' the connection like this:
net use \\<host_name>\<share_name> /delete /yes

Open in new window

Author

Commented:
ah, thank you everyone for such quick responses.  i have not had a chance to go though and test each response but i wanted to post what i have, now, below.

again, i am using window's scheduler to handle the scheduling as well as the authentication in regards to having the script run under the correct account.

i still need to plug in the code that will handle a logon error and send an alert.

.......

On Error Resume Next


Set objNetwork = CreateObject("WScript.Network")


strUserName = objNetwork.UserName
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath _
& "\" & strUserName


strDriveLetter1 = "x:"


strRemotePath1 = "\\server\share"


Set objNetwork = CreateObject("WScript.Network")


objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1


WScript.Quit

Author

Commented:
Hi Telczj9,

The bat file script in your second posting is great in that the temp folder receives the log file telling whether the share is available or not.

I’m not quite sure of how to tie blat into this and have it trigger on the error.  I may need to do some reading on blat.

I’ll try some of the other suggestions below and then circle back around to this if I don’t find anything that works (path of least resistance).
Most Valuable Expert 2012
Top Expert 2014

Commented:
acmi, your script doesn't use an alternate username and password.  The script I posted does, and also should fire BLAT for you.

Rob.

Author

Commented:
Hi RobSampson,

Yes, the credentials will be provided via the Scheduled Task settings when all is said and done and will run under the correct account.

For testing (without using scheduled task), I am now looking at your script.

Using blat, I can send mail to my account from the server.

From the script though, I’m not having any luck.

With no errors, I am unsure of where to focus.

Will keep plugging away…
Most Valuable Expert 2012
Top Expert 2014

Commented:
OK, run it manually, without using a scheduled task, by starting a command prompt and typing this:
cscript c:\scripts\TestPath.vbs

First, change this:
strCommand = "cmd /c " & strBLAT & " """ & strBody & """ -server " & strSMTPServer & " -to " & strTo & " -f " & strFrom & " -s """ & strSubject & """"
objShell.Run strCommand, 0, True

to this
strCommand = "cmd /k " & strBLAT & " """ & strBody & """ -server " & strSMTPServer & " -to " & strTo & " -f " & strFrom & " -s """ & strSubject & """"
WScript.Echo "Executing " & strCommand
objShell.Run strCommand, 1, True

so that you get some more output.  You should be able to see what's happening with the BLAT command.

Rob.

Author

Commented:
Hi RobSampson (2),

Quick followup.  When I run the script, I do notice the drive mapping when viewing the drives – the mapped drive then disappears shortly there after.

From the surface, that seems to be the extent of what the script is doing at the moment.

Is there a way to turn on error checking to better know the line in the code that may be causing the issue?

Author

Commented:
Hi RobSampson (2),

Thank you for your additional comments - will give them a try first thing in the morning (as long as nothing else is on fire :)

Again, thanks for everyone's help on this.

Commented:
Acmi,

i though the question was how to monitor UNC path availability, now it is evolving to alerting via email too.

I would recommend a separate question for the latter.

The original question is solved, right?

Cheers!
Most Valuable Expert 2012
Top Expert 2014

Commented:
The author stated "for the presence of a file or something from the UNC location and then blat an alert if it is unable to do so", so I think the use of BLAT is within the scrope of the original question.  It's only an extra line or or two anyway.

Rob.
Most Valuable Expert 2012
Top Expert 2014

Commented:
>> scrope  --> scope

Author

Commented:
Hi Telczj9,

Actually, if you read the first sentence of my posting, you can see that the main purpose is to alert when the UNC is unavailable to a given user account.

I don’t really have a reason to separate this posting into two postings – my original posting still stands.

No evolution has taken place here:)

And I have not had a chance to go though today’s responses so I am unsure if I have a working solution at this point.

Author

Commented:
Hi RobSampson,

Unfortunately, I have yet to test the changes you recommended last night at 06:20 PM.

I have a few emergencies to deal with for the first part of today but plan to jump on your recommendations as soon as possible.

Thanks everyone for your help on this.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Sure, whenever you're ready is fine.

Author

Commented:
I apologize for the delay in getting back to this – have blocked off time today to continue and further test.

Author

Commented:
Behavior after making the recommended changes:

When the script is run, the following popup below is displayed:

Executing cmd /k c:\blat.exe “x: was mapped to \\server\share and x:\myfile.txt was found.” –server mailserver.domain.com –to me@domain.com –f me@domain.com –s “UNC Access Test”

When I choose OK, a command window opens with the text below:

Blat v2.7.6 w/GSS encryption (build : Oct 25 2011 21:12:01)
unknown error code 3 when trying to open x: was mapped to \\server\share
 and x:\MyFile.txt was found.

If I remove the myfile.txt from the NAS and rerun the script, the popup below is displayed:

Executing cmd /k c:\blat.exe “x: was mapped to \\server\share and x:\myfile.txt was not found.” –server mailserver.domain.com –to me@domain.com –f me@domain.com –s “UNC Access Test”

When I choose OK, a command window opens with the text below:

Blat v2.7.6 w/GSS encryption (build : Oct 25 2011 21:12:01)
unknown error code 3 when trying to open x: was mapped to \\tn-nas-marcom\marcom
 but x:\MyFile.txt was not found.

No email alert is sent when the file was not found.

When I change the name of the share (to make it unavailable) and rerun the script, the popup basically says that the drive can not be mapped as no network provider accepted the given network path.

Here is where we would want an alert as well – we basically need to know whenever the share is not accessible for everyone.

After further testing, we are far more concerned with the availability of the share and less concerned on whether a specific account can access the share as it seems to either be up or down for everyone together.

Author

Commented:
Please ignore the inconsistant path info above - the path can be concidered as \\server\share.
Most Valuable Expert 2012
Top Expert 2014
Commented:
OK, looking at the BLAT syntax, the message appears to be trying to be read from a file.  To have it read the body of the message from the command line, change this:
strCommand = "cmd /k " & strBLAT & " """ & strBody & """ -server " & strSMTPServer & " -to " & strTo & " -f " & strFrom & " -s """ & strSubject & """"


to this:
strCommand = "cmd /k " & strBLAT & " - -body """ & strBody & """ -server " & strSMTPServer & " -to " & strTo & " -f " & strFrom & " -s """ & strSubject & """"


and see what that does.

Regards,

Rob.

Author

Commented:
Unfortunately, I have been unable to get the script to work properly and have little time to test further.

Will need to close this one down – will award points to avoid any issues closing this posting.