1

I searched all over and haven't found this answer yet.

I have a script to set up user accounts and windows customizations for new or reimaged computers. I'm mapping drive via a RUNONCE registry entry, and I'm having trouble. This local network is a Workgroup, not a domain, all PC's running Win7Pro or Win10Pro. The office manager's PC is the local file server, and I'm attempting to map a drive to it from the other computers.

Here is the portion of the RUNONCE batch file I'm having an issue with:

:PROMPTFORPMHOSTNAME
ECHO This PC was identified during InitialSetup as a Leasing Office PC.
ECHO This PC's Host Name is %computername%.
ECHO.
SET /P PMHOSTNAME="Enter the Property Manager PC Hostname: "

:MAPDRIVE
ECHO - Map M Drive
NET USE M: /delete >nul 2>&1
NET USE M: \\%PMHOSTNAME%\Data >nul 2>&1
NET USE M: \\%PMHOSTNAME%\Data /user:%computername%\[username] [password] /persistent:yes
NET USE /persistent:yes >nul 2>&1
TIMEOUT /T 5 /NOBREAK >nul 2>&1

In my scenario, the initial script uses a local admin account to create a user account via NET USER, then places the RUNONCE in the registry. After a reboot, I enter the newly created account, and the RUNONCE runs as planned.

The issue is that the RUNONCE is being run as administrator. So when it mapped the drives, it does so under the administrator-level and not the user-level. It says that the drive has been mapped successfully, but it doesn't show up.

I'm able to replicate this by running CMD in two instances, once as admin, and once as user. When I map the drive as admin, it says it's successful and doesn't show up in Explorer. When I map the drive as user, it's successful and shows as it should.

So I need to know how to get the RUNONCE to run as the logged in user so this mapped properly. Or show what in the hell I'm doing wrong and what I'm missing that should be obvious and just isn't given my current level of frustration. :P

Thanks so much everyone! I really appreciate your help in advance. :)

MaxKorban
  • 107
  • 1
  • 3
  • 13
  • If you would run it without redirecting any errormessages to NUL, you'd notice an errormessage that the driveletter already is in use. You do `net use M:` twice! – Stephan Jun 28 '16 at 16:45
  • That was my fault. I was tinkering with the script in Notepad+. I forgot to remove the top NET USE line before I copied/pasted into the post here. My bad. :P – MaxKorban Jun 28 '16 at 17:54

1 Answers1

1

Persistent network drive mappings are always registered by Windows per user account and the network drives are connected only when the user logs in and are automatically disconnected on user logs out.

There are two RunOnce registry keys as described by the Microsoft documentation page Run and RunOnce Registry Keys:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce

On 64-bit Windows there are even two more RunOnce keys as above are for 64-bit applications and below are for 32-bit applications which does not matter for this task:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\RunOnce

It is no good idea to use HKLM or HKCU of the administrator account on using the administrator account to register the batch file to be executed once for persistent mapping the share to drive letter M.

Better would be registering the batch file under

HKEY_USERS\.DEFAULT\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

And the batch file uses reg delete for deleting itself from

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

But this works only if the user account on which the persistent drive mapping should be done once is not already created when registering the batch file in default user account registry hive.

I would be also possible not using RunOnce at all and create instead a shortcut (*.lnk) file in the directory read from registry with reg query from value Startup under registry key

HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders

And the batch file on execution deletes the shortcut file in the startup directory of the current user account, i.e. in directory read from registry with reg query from value Startup under registry key

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders

This approach, also working only with user account on which to execute the batch file does not already exist, is perhaps even better than running the batch file via RunOnce as the shortcut file can contain properties like window height and width suitable for this task.

By the way: The command line

NET USE M: \\%PMHOSTNAME%\Data /user:%computername%\[username] [password] /persistent:yes

is enough to create the drive mapping and enable persistent saving of all network drive connections in registry for current user account. The line above and the line below this line are counterproductive in worst case.

Please note that the option /PERSISTENT:YES changes the registry value SaveConnections under registry key HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Network\Persistent Connections to the string value yes which results in remembering from now on all network connections under registry key HKEY_CURRENT_USER\Network. The option /PERSISTENT:YES can be omitted if the registry value SaveConnections has already the string value yes on using NET USE to create the network connection and store it in Windows registry hive for current user.

For the deletion use:

%SystemRoot%\System32\net.exe USE M: /DELETE /YES 2>nul

It is better to specify in a batch file external Windows commands like NET with their fully qualified file names for a more fail-safe and faster execution because in this case the Windows command processor does not need to search for the file with name net in current directory and next in one directory after the other listed in value of environment variable PATH having a file extension listed in value of environment variable PATHEXT.

Mofi
  • 46,139
  • 17
  • 80
  • 143