Network drive after restart disconnect - windows

I need advice.
I run a script on my computer that copies the files to the network drive. Periodically, it turns on via the task scheduler.
But it happens that after a reboot the network drive does not connect - even though when I set it up I checked "reconnect at login". (Usually I just double-click on the drive and it reconnects). But if I don't do this, the script can't put it anywhere because the disk doesn't seem to be there.
I tried:
turning off fast booting,
put a batch file in the folder after boot:
#echo Create new Y: drive mapping
net use Y: \\server\folder
:exit
put another batch file in the "after boot" folder
#echo off
:Start
timeout /t 5 /nobreak >NUL
if exist Y:\NUL goto End
net use Y: \\server\folder /USER:domain\user password /PERSISTENT:YES
if ERRORLEVEL 1 goto Start
:End
But neither solved the problem.

I had exactly this problem some days ago.
The solution I used was nothing really professional, but it worked.
I just created a textfile via powershell within the networkfolder.
Everytime I now restart my PC it just writes the newest date in this txt.
I know its not perfect, but it works.
New-Item \\Server\Folder\RecentLogin.txt
$var_date = Get-Date
Set-Content \\Server\Folder\RecentLogin.txt "$var_date"

Ensure that fast boot is disabled.
Go to the Device Manager -> Network adapters -> Properties of your network adapter
Under "Power Management" disable "Allow the Computer to Turn Off This Device to Save Power"
Under "Advanced" set "Wait for Link" to "On"
Go to Group Policies (execute gpedit.msc) -> Computer Configuration -> Administrative Templates -> System -> Logon and enable "Always Wait for the Network at Computer Startup and Logon".
Add the following values to your registry:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System]
"EnableLinkedConnections"=dword:00000001
"LocalAccountTokenFilterPolicy"=dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]
"GpNetworkStartTimeoutPolicyValue"=dword:0000003c
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\NetworkProvider]
"RestoreConnection"=dword:00000001
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Dnscache]
"Start"=dword:00000002
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters]
"Autodisconnect"=dword:ffffffff
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer]
"LocalKnownFoldersMigrated"=dword:00000001
Remove the following registry entries, if they exist (where "X" is the letter of your connected network drive):
[HKEY_CURRENT_USER\Network\X]
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Map Network Drive MRU]
"a"="\Server\Path"
"MRUList"="a"
Reboot
Map your network drive manually:
open the File Explorer, right click on "This PC" and select "Map network drive"
use the IP instead of the DNS name (e.g. \\192.168.0.10\MyShare) and activate "Reconnect at sign-in"
Add the following value to the corresponding registry key (where "X" is the letter of your connected network drive)
[HKEY_CURRENT_USER\Network\X]
"ProviderFlags"=dword:00000001
Optionally: Delay any existing tasks which access the network drive to about 10 seconds.
Reboot and check if everything works as expected. The status of net use (shell command) should say "OK".

Related

Map Drive in RunOnce Batch File

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. :)
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.

Setting internet time server on windows embedded standard through batch or similar

I have a PLC running windows embedded standard where I make some installations and modifications specific to my company's software as part of our installation process (which is all manual today). I'm now in the process of automating this procedure.
I want to set the Date and time properties->internet time server to pool.ntp.org through a batch file or similar. The command
w32tm
is not recognized.
I've tried using the command
net time /setsntp:pool.ntp.org
which returns
the command completed succefully..
Using the command
net time /querysntp
also returns
The current SNTP value is: pool.ntp.org
The command completed successfully.
But these changes are not reflected when I manually check them under Date and time properties->internet time server, not even after a restart. So I'm left wondering if the command I'm using is actually working? Is there another way to accomplish this?
EDIT:
The following .bat file works. The changes in the registry alone only added the server to the list and made it default but it didn't enable syncing.
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v "0" /t REG_SZ /d "pool.ntp.org" /f
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /ve /d "0" /f
NET TIME /setsntp:pool.ntp.org
Have a look in regedit under the following path;
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers
Add your server(s) into that folder as String Values - or you could modify the default MS provided ones if you prefer. Check what you 'default' server is (mine shows as 0) as that's the one Windows will use first, and put at the top of the list in the time settings.
If you delete the default MS servers and add your own ensure you have at least 2 servers in the list or when you try & access the 'Time & Date' setting / Internet time setting tab within the clock it will crash and never display the list.
If you want to configre any special ntp options then have a look at this path;
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time
Restart the 'Windows Time' service and you should be good to go. There is no reason you couldn't script this either through a .bat or power shell.

Win8.1 Net Use Succeeds, Mapped Drive Fails

A client has a nas box (nas01) and a batch script to connect three drives to it at startup.
net use h: \\nas01\home\Foo /user:Foo bar
net use m: \\nas01\music /user:Foo bar
net use p: \\nas01\media /user:Foo bar
From windows Explorer clicking on M and H drive gets him into the folders. P drive fails with a cross through it.
I opened a command prompt (cmd) as administrator (right click run as administrator) and ran the batch.
It succeeded under CMD.
It failed to let the user in under Windows Explorer.
I used net use * /delete from the CMD prompt then ran the following line manually.
net use p: \\nas01\media /user:Foo bar
In cmd I can do a dir p: and see the files, I can change to p: drive, change folders and see sub folders and files. From Windows Explorer it get Nada.
A manual disconnect for each drive then re map of drives under windows explorer fixed the issue - all four drives accessible, but it leaves me with a batch file my client cant use when Windows drops the mapped drives again in the future.
Why is one side of windows (CMD) working but not useable under Windows Explorer? Any thoughts.
User has full admin rights on PC, local login, no UAC getting in the way.

Script to remap current network drive?

We need to disconnect and re-map a network drive on Windows 7, using a set of scripts (or an app) that runs off the same network path.
That is, I need something that loads itself into RAM before it runs, so it continues to run after the drive is disconnected.
Any ideas?
Please note that 16-bit apps are NOT supported in 64 bit systems (this explains why the Novell utility failed).
You would need a vbs file running throughout a logon session to remap drives if it's disconnected by user. Need to make this script to run when domain user logs on - e.g. Logon Script in AD or GPO. There are many ways to do it.
You could even disable "Remove Network drives" feature from Explorer GUI via GPO or Reg key (net use command still works).
Or you can tweak solution by Julius for this SO question to fit your need. But consider performance impact of the vbs - only check every n minute(s) in an infinite loop.
We do something similar. We have a batch file on the network that maps the drives a user needs. We update the batch file from time to time, and users run it from a shortcut that we've placed on their desktop:
C:\WINDOWS\system32\cmd.exe /c (#echo off&if not exist \\172.x.x.x\Login (echo Unable to access server&pause) else (md c:\TMP > NUL 2>&1 &copy \\172.x.x.x\Login\MapDrives.bat C:\TMP /y > NUL 2>&1 &call C:\TMP\MapDrives.bat&del C:\TMP\MapDrives.bat&rd c:\TMP))
You can see that it checks to see if they can access the server, and if they can, it creates a folder C:\TMP, copies the MapDrives.bat file locally, then runs it. Since it is running locally, it can remap network drives without terminating it own execution. And we can update the batch file on the server without pushing it to each user's computer.
If you don't want to create a shortcut with the long command line above, it might work to create a second batch file on the server (e.g., RunMe.bat) that users run from the server. You could place all of the code from the shortcut in the RunMe.bat and accomplish the same thing. Of course, you'd want to add one more line of code to change to the local drive (so Windows doesn't hold open a handle to the network drive). Something like this:
#echo off
C:
if not exist \\172.x.x.x\Login\MapDrives.bat (
echo Unable to access server
pause
) else (
md c:\TMP > NUL 2>&1
copy \\172.x.x.x1\Login\MapDrives.bat C:\TMP /y > NUL 2>&1
C:\TMP\MapDrives.bat
)
I kept the if not exist ... because you might place the RunMe.bat in a different location than the MapDrives.bat, so it still makes sense to verify the user can access the file. Because I didn't use call C:\TMP\MapDrives.bat, it transfers control to the local batch file and any handles to the server should be closed so the drive can be remapped. This means however, that you cannot place more commands after the C:\TMP\MapDrives.bat command.

How do I deny log on through Terminal Services (RDP) from a command line?

In Windows 2003, I can start...
Control Panel -> Administrative Tools -> Local Security Policy
Then, if I go to...
Local Policies -> User Rights Assignment -> Deny log on through Terminal Services
... it lets me deny RDP access to a certain user account (even if that account is an admin).
How can I do the same thing from the command line, so I can automate it?
You should be able to use the reg command to modify the registry key that corresponds to this group policy setting.
To disable, try this from a batch file:
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server"
/v fDenyTSConnections
/t REG_DWORD
/d 1
/f
I've wrapped the switches onto multiple lines for readability, make sure to put all that on one line in your batch file. I don't have access to a Windows 2003 server to confirm those two settings are one in the same but I believe they are. You could use Process Monitor to watch which registry key changes when you change that GP setting to sniff out which one it is in the event that I have the wrong key.
It appears you can also change this using the NTRights Utility and the SeDenyRemoteInteractiveLogonRight right. The syntax for that would be:
NTRights.exe -u user +r SeDenyRemoteInteractiveLogonRight

Resources