Script - How to check if a network path is available and then map it

13

4

I would like an screen saver / logon script that checks if a network path is available and then map it to a unit. If it is not available it disconnects/don't connect.

Network path is \192.168.1.1\drive1

Also I need to use username/password to connect to that path.

FernandoSBS

Posted 2013-05-21T18:53:24.557

Reputation: 1 541

Answers

15

You can use the exist command to check if the path is valid:

if exist \\192.168.1.1\drive1 net use s: \\192.168.1.1\drive1

If you need to provide credentials (i.e. your current Windows user doesn't have access to that share), add /user:

if exist \\192.168.1.1\drive1 net use s: \\192.168.1.1\drive1 /user:myDomain\myUser myPassword

If there's a chance that the share already exists, and you want to delete it if it's no longer available, add an else clause:

if exist \\192.168.1.1\drive1 (net use s: \\192.168.1.1\drive1) else (net use /delete s:)

And once again, add the /user if you need it.

You can tie this all together in a batch file similar to the following:

@echo off
if exist \\192.168.1.1\drive1 (set shareExists=1) else (set shareExists=0)
if exist y:\ (set driveExists=1) else (set driveExists=0)
if %shareExists%==1 if not %driveExists%==1 (net use y: \\192.168.1.1\drive1)
if %shareExists%==0 if %driveExists%==1 (net use /delete y:)
set driveExists=
set shareExists=

Geoff

Posted 2013-05-21T18:53:24.557

Reputation: 2 335

Would "if exist" also confirm if the shared network path is accessible or not? Similar to how one would manually click the link to make sure it isn't down? – BV45 – 2018-07-16T20:07:27.017

ok I am just pasting that in notepad and saving as .vbs but when I run a task of it it gives Visual basic error? – FernandoSBS – 2013-05-21T21:35:29.830

1That command is a batch command - it should work from any standard batch file. – Geoff – 2013-05-21T22:28:30.357

C:\Windows>if exist \192.168.1.1\volume1 (net use y: \192.168.1.1\volume1 ) e lse (net use /delete y: ) The network connection could not be found.

More help is available by typing NET HELPMSG 2250. – FernandoSBS – 2013-05-21T22:44:28.677

I'll add an edit... – Geoff – 2013-05-22T12:53:54.420

i'm sorry? (5 chars) – FernandoSBS – 2013-05-22T21:17:43.480

I added an extra piece to my answer - see the bottom batch file – Geoff – 2013-05-22T21:33:44.513

6

Powershell would make this easy:

If(Test-Path \\192.168.1.1\Drive1)
  {
    net use M: \\192.168.1.1\Drive1 /user:Domain\UserName Password
  }
else {net use M: /delete > nul}

Austin T French

Posted 2013-05-21T18:53:24.557

Reputation: 9 766

ok I am just pasting that in notepad and saving as .vbs but when I run a task of it it gives Visual basic error? – FernandoSBS – 2013-05-21T21:35:21.530

1Save it as a .ps1 and run it from Powershell. – Austin T French – 2013-05-21T21:36:48.997

i'm not familiar with powershell, how can I automate it in Task Scheduler? – FernandoSBS – 2013-05-21T21:44:39.307

ok got it. What the bonus of using powershell? – FernandoSBS – 2013-05-21T22:02:51.050

Can use the full .NET libraries, extensible through C#, can use native windows Commands... It has an ISE and IDEs through PowerGUI and others. Great syntax, automatic and explicit data types... In short it does *everything* – Austin T French – 2013-05-21T22:12:09.540

powershell.exe : File C:\windows\logon.ps1 cannot be loaded because the executi on of scripts is At line:1 char:11

  • powershell <<<< -c c:\windows\logon.ps1 -verbose >> c:\windows\logon.txt 2>&

1 + CategoryInfo : NotSpecified: (File C:\windows... of scripts is :String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError

disabled on this system. Please see "get-help about_signing" for more details. At line:1 char:21

  • c:\windows\logon.ps1 <<<< -verbose
    • CategoryInfo : NotSpecified: (:) [], PSSecurityException
    • FullyQualifiedErrorId : RuntimeException
  • < – FernandoSBS – 2013-05-21T22:19:11.957

ok I've set the restriction off. now: net.exe : The network connection could not be found. At C:\Windows\logon.ps1:7 char:10

  • else {net <<<< use Y: /delete}
    • CategoryInfo : NotSpecified: (The network con...d not be found.:String) [], RemoteException
    • FullyQualifiedErrorId : NativeCommandError

More help is available by typing NET HELPMSG 2250. – FernandoSBS – 2013-05-21T22:56:46.990

let us continue this discussion in chat

– FernandoSBS – 2013-05-21T23:07:59.250

0

It is just simplier to just try to map it using the Windows File explorer or using the net use command. Either it works or it doesn't.

mdpc

Posted 2013-05-21T18:53:24.557

Reputation: 4 176

If it's available I want to be automatically connected when I logon / screen saver, if it's not then I want it removed from the mapped drives. So what you suggested is not an option. – FernandoSBS – 2013-05-21T19:20:15.923

I don't understand, it you issue a net use, if the command succeeds it will be connected. If the command fails, then it is removed from the mapped drives. Seems simple to me. – mdpc – 2013-05-21T19:47:44.433

0

This is the final code:

function run{
net use
If(Test-Path \\192.168.1.1\volume1)
  {
    if (((New-Object System.IO.DriveInfo("Y:")).DriveType -ne "NoRootDirectory")) 
        {
            "already mounted and accessible"
        }
    else
        {
            net use Y: \\192.168.1.1\volume1
            "mounting"
        }
  }
else { 
    if (((New-Object System.IO.DriveInfo("Y:")).DriveType -ne "NoRootDirectory"))
        {
            net use Y: /delete
            "removing"
        }
}
exit 4
}

run 

I use Test-Path \\192.168.1.1\volume1 as suggested to check if the network path is available and ((New-Object System.IO.DriveInfo("Y:")).DriveType -ne "NoRootDirectory") to check if the drive letter exists.

FernandoSBS

Posted 2013-05-21T18:53:24.557

Reputation: 1 541