Help writing script to check for NAS availability

Status
Not open for further replies.

CLSegraves

Explorer
Joined
Sep 13, 2013
Messages
84
Hey everyone, I need some guidance on how to accomplish something.



I want to write a script that will check to see if my laptop can see the NAS (my NAS on my local/home network), and if so, map the drives. Otherwise (ie: I’m not at home), I don’t want Windows to bother trying to map the drives.



My idea was to run a test-connection script that would go out and try to find the NAS. The following powershell command:



Test-connection –Computername NAS01 –Buffersize 16 –Count 1 –Quiet





returns a “True” (if the NAS is there), but I am not sure where to go from there. From back in the day when I did some java programing, I had thought to use an

if (test-connection***** = True)
{
"map drives"
}

but I'm not sure of the exact verbiage in windows.

On login/wakeup I would have Windows execute a bat file with the “if” statement that would execute the test-connection command and if a “True” is returned, execute the following to map the drive:



@echo off

ping localhost -n %delay%

net use E: "\\SomePC\SomeShare"

net use F: "\\SomePC\SecondShare"

net use G: "\\SomePC\ThirdShare"



I use this mapping (along with a short delay) on my home desktop to keep from Windows trying to automatically map my network drives on startup (my desktop takes a few seconds to initialize and get an IP).



Ideas?



Thanks,

Chris
 
Last edited:

Starpulkka

Contributor
Joined
Apr 9, 2013
Messages
179
Well you really would find your answer more faster by using google than posting that in here. Also passwords in plain bat command is sou 80's.. but in a then google wasnt in 80's so you would ask that question in bbs. So have a nice day jou.

Code:
Ping AAA.BBB.C.DDD | find "TTL"
If errorlevel 1 GOTO :END

) ELSE (
net use x: \\AAA.BBB.C.DDD\SHARE0 PASSWORD /user:USERNAME
net use y: \\AAA.BBB.C.DDD\SHARE1 PASSWORD /user:USERNAME
net use w: \\AAA.BBB.C.DDD\SHARE2 PASSWORD /user:USERNAME
)
:END
EXIT

heh. wont even need ()else() stuff if you use in bat.

Another as an example.
Code:
net view | find "FreeNAS"
If errorlevel 1 GOTO :END

net use x: \\FreeNAS\share passwrd /user:username

:END
EXIT

Im quessin unmap goes like this
Code:
net use x: /DELETE
net use y: /DELETE
net use w: /DELETE
net use z: /DELETE


Made one more
Code:
Ping AAA.BBB.CCC.DDD  | find "TTL"
IF ERRORLEVEL 1 GOTO WAIT

:LOGIN
net use x: \\AAA.BBB.CCC.DDD\SHARENAME PASSWORD /user:NAME
If NOT errorlevel 1 GOTO :END
TIMEOUT -T 10 /NOBREAK
net use x: \\AAA.BBB.CCC.DDD\SHARENAME PASSWORD /user:NAME

:END
exit


:WAIT
TIMEOUT -T 60 /NOBREAK
Ping AAA.BBB.CCC.DDD  | find "TTL"
IF ERRORLEVEL 1 GOTO WAIT2
GOTO LOGIN

:WAIT2
TIMEOUT -T 40 /NOBREAK
Ping AAA.BBB.CCC.DDD  | find "TTL"
IF ERRORLEVEL 1 GOTO WAIT3
GOTO LOGIN

:WAIT3
TIMEOUT -T 80 /NOBREAK
Ping AAA.BBB.CCC.DDD  | find "TTL"
IF ERRORLEVEL 1 GOTO END
GOTO LOGIN


Had strip a noise beep when script has completed task, looks like site does not understand that mark what is used to make a windows beep, maby script kiddie security makes it unable to mark. But oh well, theres few login programs what actually does far more checkin than this, so this was just for my fun.
 
Last edited:

CLSegraves

Explorer
Joined
Sep 13, 2013
Messages
84
I appreciate the help. I ended up using your first set of code and it works like a champ. I set up a new windows task that would only run when the wifi was connected to my home network.

In response to the google suggestion, I looked for the answer on google, which is where I got the code I brought with me. Once I ran into the limit of what I could glean from google, I came here.
 
Status
Not open for further replies.
Top