I have a Windows Server 2008 instance that typically has no one logged in. I want it to have a drive letter Z: mapped to a remote share \sharecomputer\sharename, so that a web-app on the Windows Server instance can access files on the remote share through Z: The remote share requires a username and password to be supplied to access files on it.
Technically it would probably be possible to write a program that uses
net use or something similar to connect to the share. Then make that program run as a service, set it to auto-start and run it under the same user account as the web application. Then, you'd have to assign
allow interaction with the desktop to the service. Even then, I'm not 100% sure it will work.
The best (and standard) thing to do here is run the web app as a user that has access to that share, and have it write to
\\server\share instead of to
Z:. Then you don't have to map the drive at all.
Start the application under the Service Account once. Then stop it. There should now be a profile with an
ntuser.dat file in it.
regedit. Navigate to
HKEY_USERS (the bottom toolbar must say
Computer\HKEY_USERS). File -> Load Hive -> Find that ntuser.dat file that you just created. Open. Give it a key name like "service account" or whatever, it doesn't matter. If the computer complains about the file being in use you may have to reboot.
Navigate to KEY_USERS\Service Account\Network. Right click that tree node, New -> Key. Name the key the drive letter in uppercase ("Z" in the Question).
Create the following values in that key:
Name Type Value ConnectionType DWORD32 0x1 DeferFlags DWORD32 0x4 ProviderFlags DWORD32 0x1 ProviderName STRING "Microsoft Windows Network" ProviderType DWORD32 0x00020000 RemotePath STRING "\\path\on\network" UserName DWORD32 0x0
Navigate back to the KEY_USERS\Service Account level (click on it), File -> Unload Hive. Yes.
Start the service, it now has the network drive mapped (assuming the service account has access to the network share and NTFS permissions therein). If you're not in a domain or similar environment where the service account can be assigned permission to the share, you'd have to store credentials for the share. If that's the case it'd be much easier to make the service account a full user account, login as it, setup the network drive and stored credentials, than to do it all through registry settings.