none
Silently re-connect to already mapped network drive?

    質問

  • We have an environment where there are several users that work out of the office on a regular basis.  All use Windows 7 32-bit Enterprise OS.

    I would like to create a Scheduled Task to kick off a series of events when our VPN client is successfully connected:

    1. VPN is established, thus initializing the Task
    2. Silently reconnect the disconnected, but already mapped, P: drive network share
    3. (..other steps)

    It seems simple, but I'm not sure how to do #2.  I know that I can use "net use" to re-map the drive, but I don't want to re-map it since it's already there.  I just want to establish the connection.  Usually, users need to double-click the P: drive in order to get it to reconnect.  It's a simple step and seems like it should be easy enough to automate, but I'm not sure how without re-mapping the share.  Ideas?

    2012年3月13日 17:02

回答

  • Executing Scripts on VPN Connection

    This can be achieved by creating a task in task sceduler to run on vpn connection (using event id).

    Type the below command in command prompt after replacing the data accordingly

    schtasks /create /F /TN "abc" /TR "C:\test.bat" /SC ONEVENT /EC Application /MO " *[System[(Level=4 or Level=0) and (EventID=20225)]] and *[EventData[Data='VPN NAME']]"

    1. Replace abc by any name, just to identify your task. 
    2. c:\test.bat should be replaced by path to your batch file which you need to run on vpn connection
    3. VPN Name should be replaced by the name of the vpn connection, after connection of which you wish to execute the batch file above.

    Once you have created the task, you can view the task in task scheduler @ Windows Control Panel > Administrative Tools > Task Sceduler

    Reconnecting the Share

    Either of these 3 methods should work this out

    1. Have a batch file to open an explorer window minimized to the share path and then auto close it based on the Window Title using taskill
    2. Simply change drive to the mapped drive letter in batch file (works for me on my office vpn)
    3. Try using the command, replacing z by drive letter mapped and \\server\share by share path
      net use z: \\server\share 2> nul

    For option - 2,  use these commands in batch file

    Start /min explorer x:\
    timeout 3 /nobreak
    Taskkill /fi "windowtitle"

    here replace X with the share drive letter and replace windowtitle with the title of the window when you open the share when it is connected

    Reference Links:


    Regards
    Ashok J Prakash | My Blog

    If a post solved your problem, click “Mark as Answer” on the post.
    If a post helped you, click "Vote As Helpful" on the left side of post.




    2012年3月13日 19:13

すべての返信

  • Executing Scripts on VPN Connection

    This can be achieved by creating a task in task sceduler to run on vpn connection (using event id).

    Type the below command in command prompt after replacing the data accordingly

    schtasks /create /F /TN "abc" /TR "C:\test.bat" /SC ONEVENT /EC Application /MO " *[System[(Level=4 or Level=0) and (EventID=20225)]] and *[EventData[Data='VPN NAME']]"

    1. Replace abc by any name, just to identify your task. 
    2. c:\test.bat should be replaced by path to your batch file which you need to run on vpn connection
    3. VPN Name should be replaced by the name of the vpn connection, after connection of which you wish to execute the batch file above.

    Once you have created the task, you can view the task in task scheduler @ Windows Control Panel > Administrative Tools > Task Sceduler

    Reconnecting the Share

    Either of these 3 methods should work this out

    1. Have a batch file to open an explorer window minimized to the share path and then auto close it based on the Window Title using taskill
    2. Simply change drive to the mapped drive letter in batch file (works for me on my office vpn)
    3. Try using the command, replacing z by drive letter mapped and \\server\share by share path
      net use z: \\server\share 2> nul

    For option - 2,  use these commands in batch file

    Start /min explorer x:\
    timeout 3 /nobreak
    Taskkill /fi "windowtitle"

    here replace X with the share drive letter and replace windowtitle with the title of the window when you open the share when it is connected

    Reference Links:


    Regards
    Ashok J Prakash | My Blog

    If a post solved your problem, click “Mark as Answer” on the post.
    If a post helped you, click "Vote As Helpful" on the left side of post.




    2012年3月13日 19:13
  • Jus checking, did this work out for you ?

    Regards
    Ashok J Prakash

    If a post solved your problem, click “Mark as Answer” on the post.
    If a post helped you, click "Vote As Helpful" on the left side of post.

    2012年3月14日 19:07
  • Thanks for your reply!

    The problem I'm running into now is the VPN client we use is SonicWall and it doesn't appear that any events are added to the event log when a connection is established, so I'm just trying to find out how to trigger the event.  I can see when the service starts in the application log, but not when the actual connection is established.  Sonicwall has its own logging, but I'm not sure if Windows sees any of it.

    Thanks for the batch file informaiton - very helpful!

    2012年3月28日 20:39
  • SonicWall is a 3rd Party software, windows cannot catch events of what you are expecting in this scenario. 

    I guess SonicWall Support Forums / Technical Team would be a better place to find some script to catch the sonicwall connection event. Rest to do the actions on Windows side, you can use the above post :-)


    Regards
    Ashok J Prakash

    If a post solved your problem, click “Mark as Answer” on the post.
    If a post helped you, click "Vote As Helpful" on the left side of post.

    2012年3月28日 21:00
  • I figured out how to kick off the batch file using SonicWall.

    I want to use this batch file, but I am having problems figuring out what the WINDOWTITLE should be:

    Start /min explorer x:\
    timeout 3 /nobreak
    Taskkill /fi "windowtitle"

    I've found that I actually need the syntax:

    Taskkill /fi "WINDOWTITLE eq (window title)"

    But I've tried everything I can think of for the window title and I get the following error (just testing in cmd to start):

    INFO:  No tasks running with the specified criteria.

    How do I find the proper window title to use in that batch file?

    EDIT:

    I tried running a tasklist /v to see what the explorer task window title is, and there were no explorer.exe tasks even listed. 

    Ran CMD as administrator and tried again, got one explorer.exe task but the window title was "N/A" - I have two explorer windows open currently.

    2012年3月28日 23:06
  • Hello

    I would like to know if it possible to reconnect the share from Powershell, without launching a bat script?

    2018年5月13日 14:04