16

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.

4 Answers 4

19

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=
9
  • 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? May 21, 2013 at 21:35
  • 1
    That command is a batch command - it should work from any standard batch file.
    – Geoff
    May 21, 2013 at 22:28
  • 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. May 21, 2013 at 22:44
  • I'll add an edit...
    – Geoff
    May 22, 2013 at 12:53
  • i'm sorry? (5 chars) May 22, 2013 at 21:17
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}
9
  • 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? May 21, 2013 at 21:35
  • 1
    Save it as a .ps1 and run it from Powershell. May 21, 2013 at 21:36
  • i'm not familiar with powershell, how can I automate it in Task Scheduler? May 21, 2013 at 21:44
  • You should really split the 2 questions. This is a QA site. Also, google it if you have not already: google.com/… May 21, 2013 at 21:47
  • ok got it. What the bonus of using powershell? May 21, 2013 at 22:02
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.

2
  • 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. May 21, 2013 at 19:20
  • 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
    May 21, 2013 at 19:47
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .