Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

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=
share|improve this answer
    
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 May 21 '13 at 21:35
1  
That command is a batch command - it should work from any standard batch file. –  Geoff May 21 '13 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. –  FernandoSBS May 21 '13 at 22:44
    
I'll add an edit... –  Geoff May 22 '13 at 12:53
    
i'm sorry? (5 chars) –  FernandoSBS May 22 '13 at 21:17

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}
share|improve this answer
    
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 May 21 '13 at 21:35
1  
Save it as a .ps1 and run it from Powershell. –  AthomSfere May 21 '13 at 21:36
    
i'm not familiar with powershell, how can I automate it in Task Scheduler? –  FernandoSBS May 21 '13 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/… –  AthomSfere May 21 '13 at 21:47
    
ok got it. What the bonus of using powershell? –  FernandoSBS May 21 '13 at 22:02

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.

share|improve this answer
    
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 May 21 '13 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 '13 at 19:47

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")) 
        {
            "Já está montado e funcionando"
        }
    else
        {
            net use Y: \\192.168.1.1\volume1
            "Montando"
        }
  }
else { 
    if (((New-Object System.IO.DriveInfo("Y:")).DriveType -ne "NoRootDirectory"))
        {
            net use Y: /delete
            "Removendo"
        }
}
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.