Batch file to map drives doesn't remember credentials after a reboot

HCHTech

Well-Known Member
Reaction score
4,384
Location
Pittsburgh, PA - USA
This is a small new office, all 6 computers on Win11 Pro 25H2. Like I have done dozens of times in past for other clients, I made a 3-line batch file to map 2 drives, and ran it through our RMM. One drive is on a computer running Quickbooks, and the other drive is a shared folder on the NAS follows:

netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes
net use q: "\\QBSERVER\CompanyFiles" /user:QBUser XXXXXX /persistent:yes
net use s: \\172.xx.xxx.xx\CompanyShare /user:ShareUser XXXXXXX /persistent:yes

The drives map instantly, but when you reboot, the credentials aren't being saved. The drives are in explorer with the red-X and clicking on them prompts for the credentials. So first, I copy the batch file manually to one of the workstations and add a 'pause' to the end so I can see what's going on. When run, no errors are listed at all and the drives show up in Explorer. Reboot, back to the red-Xes and prompts for credentials when trying to access them anyway.

Ok, so I can jam in the credentials by adding a couple of more lines to the batch, like this:

netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes
cmdkey /add:QBSERVER /user:QBUser /pass:XXXXXXX
cmdkey /add:172.xx.xxx.xx /user:ShareUser /pass:XXXXXXX
net use q: "\\QBSERVER\CompanyFiles" /user:QBUser XXXXXX /persistent:yes
net use s: \\172.xx.xxx.xx\CompanyShare /user:ShareUser XXXXXXX /persistent:yes
pause

Again, no errors at all, the drives show up instantly in Explorer and you can access them, but after a reboot, they have the red Xes, and when trying to access, you get prompted for credentials. Looking in credentials manager, the two entries are there.

Next try is that since one password has an underscore character and the other one has an asterisk, I added quotes surrounding hte password in the batch file. This shouldn't be the problem though, since it works when the batch file is run, and the drives are mapped successfully. No surprise, this didn't change the symptom.

Next.....I tried doing the mapping using the GUI and checking the "remember my credentials" box manually. SAME RESULT.

I also checked the SIDs just for fun, and all of the computers have different SIDs so that's not the problem either. Also Fast Startup is NOT enabled - we disable that on all computers through our RMM. Deleting the credentials from credential manager and the mapping the drives again doesn't help either. Grabbing at straws, I went into group policy and disabled the Microsoft network client: Digitally sign communications (always) policy. It was previously set to 'Not Defined". Rebooting and trying again to map the drives gives they same symptom. The map credentials don't survive a reboot.

I really don't want to put the batch file with passwords in plain text in the startup group, but I don't think I have any option at this point.

What is going on here?
 
How's it lookin' in Credential Mangler?
There was also some /savecred switch you could toss on the end of the bat file that maps the drive...I believe used when you don't have the /user:joewanker line in there because it will ask next time it's run.
 
There are definitely bits in there that seem to apply

Thank you, Brian. I already added the cmdkey lines which were correctly storing the credentials in credential manager. Apparently, the problem came when I continued to specify the credentials in the map lines which came afterwards. The "correct" way to do it as pointed out by Perplexity, is to specify the credentials with cmdkey, then map the drive WITHOUT specifying credentials. This appears to work as desired.

All I have to say about this is that this is a new thing. I don't know when this change was made but it had to be this year at some point - I know I had used my same 3-line batch file with other clients successfully. I can't recall exactly when, but it wasn't that long ago. Well, as usual, my attempt at automating something only took 4 times as long as doing it manually - haha.
 
Isn't it always the way.
Yep - seems so. I didn't try asking ChatGPT, I'll do that to see if it knows as much as Perplexity. Quick results:

Gemini: Fail - So close. Mentioned the cmdkey command but didn't explain how to use that in conjunction with net use
Perplexity: Win - Provided the correct answer on or about line 16 of their explanation
ChatGPT: Win - Provided the correct answer, but was much more long-winded. The actual correct command was on or about line 48 of their explanation. This may be due to my previous use writing RMM scripts since it spent some time talking about how RMMs can introduce extra challenges related to their use of the SYSTEM account.
Llama4: Fail - missed the sailent point and talked instead about what can cause credential manager to NOT save credentials

I only have a sub for ChatGPT, now I'm wondering if I picked the wrong horse - haha.
 
I'm using the free version of Perplexity. It's by far and away my favorite just based upon how it formats its answers and provides the direct references for the source material.

If/when I need to do a deeper dive on any one of the points it presents, the specific references used to generate that point are included at the end of the point, as well as the full collection of references at the end of the entire answer.

It also has not had any hallucination problems for me when a solid prompt is given.
 
I use ChatGPT a bit but frequently it will answer a tech question with a wall of text and 20 instructions or commands to run. Often I have to tell it "high level, concepts only" or ask it for only one step at a time.
 
Back
Top