'net use' works in cmd window, not in .bat file


  1. Posts : 55
    Windows 10 Anniversary (Pro)
       #1

    'net use' works in cmd window, not in .bat file


    Win10 Pro, Version 2004, build 19041.804
    (Suggestion to Microsoft- put a 'copy to clipboard' button on the WinVer window).

    On my home network I have some servers and a couple of NAS drives. (Eight altogether). In startup I call a batch file to map them using 'net use'. Most of the time this works just fine. But once in a while, and with no particular repeatability (sometimes it's drive X, sometimes drive Y or one of the other net drives), the net use on the batch file fails. Most often with an error 67- unknown host, or network name not found.

    In all fails from the batch file, if I open a separate cmd window and manually enter the net use command line, it works.

    Here is my "top" bat file called from startup:
    Code:
    echo off
    rem Call each network mount in separate batch files
    
    rem Cleanup
    net use m: /d /y
    net use n: /d /y
    net use t: /d /y
    net use u: /d /y
    net use w: /d /y
    net use x: /d /y
    net use y: /d /y
    net use z: /d /y
    
    
    :Continue
    rem ---------------------------
    rem Wait for the Network Service to be started
    set /a x=0
    :starting
    Rem Send one ping to the router.  Write the results to a file.
    ping 192.168.1.1 -n 1 > e:\pingtest.txt
    
    Rem Search for unreachable in the file. 
    c:\windows\system32\findstr.exe "unreachable" e:\pingtest.txt
    
    Rem errorlevel 0 is success, 1=fail (reverse logic, fail means the router is pingable)
    if %errorlevel%==1 goto online
    
    rem Wait one second and try again.
    timeout /t  1
    if %x% lss 10 (
      set /a x+=1
      goto starting
    )
    if %x% geq 10 (
      echo Network not responding after ten seconds.
      goto exit
    )
    :online
    echo "Router is online"
    rem ---------------------
    
    
    
    rem Ping the NAS
    rem Wait for the Network Service to be started
    set /a x=0
    :nucstart
    Rem Send one ping to the NUC.  Write the results to a file.
    ping nuc -n 1 > e:\pingtest.txt
    
    Rem Search for unreachable in the file. 
    c:\windows\system32\findstr.exe "unreachable" e:\pingtest.txt
    
    Rem errorlevel 0 is success, 1=fail (reverse logic, fail means the router is pingable)
    if %errorlevel%==1 goto nuconline
    
    rem Wait one second and try again.
    timeout /t  1
    if %x% lss 10 (
      set /a x+=1
      goto nucstart
    )
    if %x% geq 10 (
      echo Network not responding after ten seconds.
      goto exit
    )
    :nuconline
    echo "NUC is online"
    
    
    echo on
    call mntMverbose.bat
    if exist "M:" echo M: online.
    call mntNverbose.bat
    if exist "N:" echo N: online.
    call mntPverbose.bat
    if exist "P:" echo P: online.
    call mntTverbose.bat
    if exist "T:" echo T: online.
    call mntUverbose.bat
    if exist "U:" echo U: online.
    call mntWverbose.bat
    if exist "W:" echo W: online.
    call mntXverbose.bat
    if exist "X:" echo X: online.
    call mntYverbose.bat
    if exist "Y:" echo Y: online.
    call mntZverbose.bat
    if exist "Z:" echo Z: online.
    
    :exit
    echo off
    rem Show the connections
    net use
    TIMEOUT 30

    And this is typical of my subroutine bat files (fake password):
    Code:
    rem echo off
    echo .......... 
    echo mntY.bat 
    net use y: /d /y
    
    rem ATA drive in Nuc
    IF EXIST "y:\" goto xit
    net use y: \\nuc5\mntD /user:nuc raspberry /y 
    IF %ERRORLEVEL% EQU 0 goto xit
    
    rem Try deleting the map and try again
    net use y: /d /y 
    net use y: \\nuc5\mntD /user:nuc raspberry /y
    
    rem sleep 5s
    :xit
    Any solutions would be appreciated....
      My Computer


  2. Posts : 8,401
    windows 10
       #2

    It can take some time to see pcs in a network and the master browser needs to be up to date. If you use IPs it should solve the problem as you wont need to wait for name resolution to kick in
      My Computer


  3. Posts : 55
    Windows 10 Anniversary (Pro)
    Thread Starter
       #3

    Samuria said:
    It can take some time to see pcs in a network and the master browser needs to be up to date. If you use IPs it should solve the problem as you wont need to wait for name resolution to kick in
    Thanks for the input. You may be on to something, I'll try putting a short delay between the subroutine calls.

    The problem with using discrete IP addresses is that Windows does not allow me to use the same IP for different mount points. For example, I have an NAS folder and a Mediawiki database that are both on 192.168.1.54 and I can't use
    'net use t: \\192.18.1.54\storage'
    then later
    'net use w: \\192.18.1.54\wikimedia' .

    If I do, I get: System error 1219
    ERROR: "Multiple Connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again."

    The solution was to add a few entries for the share server in the hosts file, C:\Windows\System32\drivers\etc\hosts 192.168.1.54 nuc1
    192.168.1.54 nuc2
    192.168.1.54 nuc3
    192.168.1.54 nuc4
    192.168.1.54 nuc5

    So,
    'net use t: \\nuc1\storage'
    'net use w: \\nuc2\wikimedia'
    does not give the 1219 error.

    This is why I have hostnames for the IP.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 19:31.
Find Us




Windows 10 Forums