Tuesday, October 29, 2019

Can I tell windows to request network login for network drives on startup?


I have two mapped network drives on my windows machine, from a NAS. Both drives require the same username/password configuration, and I opted not to tell windows to remember this, because well, I want the bare minimum requirement for accessing any of my files should be at least entering one password right, right?


Because of this, Windows will not connect the network drives on login and gives me the usual error: Could not reconnect all network drives


To no one's surprise, windows has no idea what to do when it doesn't know the password to my network share.


So here is how I want to automate this process: After booting up and logging in, I want to get a network login popup window, like the one you get when you try to access a window. I want to log in successfully, and then I want Windows to try and connect the network drives.


How would I achieve this?


(I am well aware that I could use the same username and login on my Windows machine as the network login to automate this, but I don't want to do that either, even if it is what I would normally recommend to others)


Answer



I tried achieving this with my basic batch skills. Here's what I came up with.


First configure windows to launch folder windows as a separate process (rather than keeping all folders under the same explorer.exe) this allows folders to be closed via command prompt/batch scripts.


Enable folders as separate processes


Next write this batch file, and place it in **C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup** (Why is this not bold lettered?)


@echo off
start explorer "D:\"
:loop
timeout 0
(net use | findstr OK) > nul 2>&1
if errorlevel 1 goto loop
taskkill /F /FI "WINDOWTITLE eq NETWORKDRIVELABEL*" /IM explorer.exe
start /wait explorer "G:\"
timeout 0
taskkill /F /FI "WINDOWTITLE eq NETWORKDRIVE2LABEL*" /IM explorer.exe
start /wait explorer "F:\"
timeout 0
taskkill /F /FI "WINDOWTITLE eq NETWORKDRIVE3LABEL*" /IM explorer.exe
start /wait explorer "H:\"
timeout 0
taskkill /F /FI "WINDOWTITLE eq NETWORKDRIVE4LABEL*" /IM explorer.exe
stop

Simply replace the drive letters with the ones that apply to your system, and the NETWORKDRIVELABEL with the names of the appropriate network drives. This is needed because the full name of the window title is "NETWORKDRIVELABEL (\SHARENAME) (DRIVELETTER)" but instead of writing a complete window title I simply use a star (*) to define that it is meant to close any explorer process with a window title starting with NETWORKDRIVELABEL.


What this does is it tries to open network share "D:\" with windows's file explorer. As it does since it's a network share you get the prompt to log in. Afterwards a loop is run where the program runs net use | findstr OK (looking for "OK" in the output of "net use") and does not move on until it finds an OK (This part assumes that you have no network drives initialized. I'm not sure how to be more elaborate with findstr; it is not as nice as bash's grep).


Then as soon as it detects a network drive is properly connected, it will proceed to try and map the other network drives by way of opening them with the explorer and then closing them.


I had to use the stop command at the end of the batch file or it wouldn't end normally (waited for user input).


Refer to here if you want to do this but hide the command window: How to run a batch file without launching a "command window"?


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...