none
Script to re-map network drives that become unavailable RRS feed

  • Question

  • Trying to find a vbscript on how to re-map all network drives with "unavailable" or "disconnected" status.
    Verena Tay
    Friday, October 14, 2011 9:05 AM

Answers

  • As jrv said, WMI in VBS as derived from the Scriptomatic tool (with some minor edits) ...

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkConnection",,48)
    For Each objItem in colItems
        Wscript.Echo objItem.LocalName, objItem.RemoteName, objItem.ConnectionState
    Next
    
    

    Tom Lavedas
    Friday, October 14, 2011 1:07 PM
    Moderator

All replies

  • The simplest solution would be a batch file like this one:

    @echo off
    for /F "tokens=1-3" %%a in ('net use') do (
      if /i %%a==Disconnected  net use %%b: %%c
      if /i %%a==Unavailable   net use %%b: %%c
    )

    If you prefer a VB Script solution then it would be great if you could post your own effort and ask for advice on the finer points.

    • Proposed as answer by JerryCici Tuesday, April 16, 2013 3:56 PM
    Friday, October 14, 2011 9:30 AM
  • Trying to find a vbscript on how to re-map all network drives with "unavailable" or "disconnected" status.
    Verena Tay

    Drives that are unavailable or disconencted are still mapped.  The next time they are accessedthe map willbe refreshed as long as teh resurce is still available.

    Drives are automaticallt disconencted after some time.  Some admins shortenthis time to reduce resource contention.  Remapping will change nothing.

    If remapping appears to solve teh problem then you have a network or system issue that needs to be addressed.

     


    jv
    Friday, October 14, 2011 10:10 AM
  • Thanks. Hv searched web and MS scripting centers; there was scripts to map or delete network drives but not any script to list out network drives that are either disconnected or unavailable. Working a vbscript for user to reconnect those network drives (different drive letters and shared directories, with different users). The following script only show mapped drives but not those network drives that are disconnected or unavailable.

    Set objNetwork = WScript.CreateObject("WScript.Network")
    Set colDrives = objNetwork.EnumNetworkDrives
    For i = 0 to colDrives.Count-1 Step 2
     Wscript.Echo colDrives.Item(i) & vbTab & colDrives.Item (i + 1)
    Next


    Verena Tay
    Friday, October 14, 2011 11:20 AM
  • Thanks. This is our constant "issue" because user's computer and the network servers are of different domains (not trusted). Working on a script to workaround this issue.
    Verena Tay
    Friday, October 14, 2011 11:23 AM
  • Your question is too vague.

     


    jv
    Friday, October 14, 2011 11:23 AM
  • Q: In VBscript, how to list out network drives (Drive letter and its network folder) that are unavailable or disconnected as shown by "net use" in my computer?
    Verena Tay
    Friday, October 14, 2011 11:32 AM
  • Q: In VBscript, how to list out network drives (Drive letter and its network folder) that are unavailable or disconnected as shown by "net use" in my computer?
    Verena Tay


    Here is a POwerShell script that can do that.

    gwmi Win32_NetworkConnection|select localname, remotename, ConnectionState

    You can aslo convert that to VBscripted WMI.

     


    jv
    Friday, October 14, 2011 11:36 AM
  • As jrv said, WMI in VBS as derived from the Scriptomatic tool (with some minor edits) ...

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkConnection",,48)
    For Each objItem in colItems
        Wscript.Echo objItem.LocalName, objItem.RemoteName, objItem.ConnectionState
    Next
    
    

    Tom Lavedas
    Friday, October 14, 2011 1:07 PM
    Moderator
  • Tom & JV, that's what I am looking for. Thank you!
    Verena Tay

    • Edited by VernTay Monday, October 17, 2011 4:02 AM
    Monday, October 17, 2011 3:59 AM
  • Calling Win32_NetworkConnection has performance hit and I think that is because it is trying to reconnect "Disconnected" maps and times out.

    Following script takes long time to enum all shares, active, disconnected, unavailable

    ---------

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkConnection",,48)
    For Each objItem in colItems
        Wscript.Echo objItem.LocalName, objItem.RemoteName, objItem.ConnectionState
    Next

    ------

    Compare to this following code is very fast 

    -------

    Set objNetwork = WScript.CreateObject("WScript.Network")
    Set colDrives = objNetwork.EnumNetworkDrives
    For i = 0 to colDrives.Count-1 Step 2
     Wscript.Echo colDrives.Item(i) & vbTab & colDrives.Item (i + 1)
    Next

    -------

    I've C++ code that uses Win32_NetworkConnection to list all mapped connection, but don't know how to use "WScript.CreateObject("WScript.Network") in C++? I'm only interested in "OK" and "Disconnected" connection.

    Thanks.

    Friday, August 30, 2013 4:29 PM
  • This topic has been answered and closed for years.  Please start a new topic.  If you question is about C++ then you need to post in the C++ developers forum.

    Any slowness you are experiencing is not an issue with the scrip but is an issue of something broken or misconfigured in your system.

    You also need to note that the disconnected status does not allow for reconnection.  Drives are auto-disconnected by default.  They will auto-connect the next time they are accessed. You should not try to reconnect them.  If there are open files you will lose data.

    Search the MS KB for more info.  This will change the auto-disconnect interval (not-recommended)

    net config server /autodisconnect:(-1 to max)

    net config server


    ¯\_(ツ)_/¯

    Friday, August 30, 2013 4:44 PM
  • Sample C++ code that uses win32_networkconnection WMI class...

    HRESULT QueryNetworkShares()
    {
     
     HRESULT hres = S_OK;

     hres =  CoInitializeEx(0, COINIT_MULTITHREADED);
     if (hres!=S_OK) {
      if (hres != RPC_E_CHANGED_MODE) {
       return hres;                    }
     }

     hres =  CoInitializeSecurity(NULL,  -1,    NULL,  NULL,  RPC_C_AUTHN_LEVEL_DEFAULT,   RPC_C_IMP_LEVEL_IMPERSONATE,
      NULL,  EOAC_NONE,   NULL             );
     if (hres!=S_OK) {
      if (hres == RPC_E_TOO_LATE) {
       hres =S_OK;
      }
      else {
       CoUninitialize();
       return hres;
      }
     }

     IWbemLocator *pLoc = NULL;
     hres = CoCreateInstance(CLSID_WbemLocator,   0,
      CLSCTX_INPROC_SERVER,  IID_IWbemLocator, (LPVOID *) &pLoc);
     if (hres!=S_OK) {
      CoUninitialize();
      return hres;              
     }

     IWbemServices *pSvc = NULL;
     hres = pLoc->ConnectServer(
      bstr_t(L"root\\CIMV2"),
      NULL,     NULL,  NULL,  NULL,  0,  0,  &pSvc  );

     if (hres!=S_OK) {
      pLoc->Release();    
      CoUninitialize();
      return hres;  
     }

     hres = CoSetProxyBlanket(
      pSvc,  RPC_C_AUTHN_WINNT,  RPC_C_AUTHZ_NONE,  NULL,  RPC_C_AUTHN_LEVEL_CALL, 
      RPC_C_IMP_LEVEL_IMPERSONATE,   NULL,  EOAC_NONE  );
     if (hres!=S_OK) {
      pSvc->Release();
      pLoc->Release();    
      CoUninitialize();
      return hres;       
     }

     IEnumWbemClassObject* pEnumerator = NULL; //SQL Service
     hres = pSvc->ExecQuery(
      bstr_t("WQL"),
      bstr_t("SELECT RemoteName FROM Win32_NetworkConnection"),
      WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY ,
      NULL,
      &pEnumerator);

     if (hres!=S_OK) {
      pSvc->Release();
      pLoc->Release();
      CoUninitialize();
      return hres;   
     }

     IWbemClassObject *pclsObj;
     ULONG uReturn = 0;

     while (pEnumerator) {

      HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
       &pclsObj, &uReturn);

      if(0 == uReturn) {
       break;
      }
      VARIANT vtProp;
      bFound = TRUE;

      // Get the value of the Name property
      hr = pclsObj->Get(L"LocalName", 0, &vtProp, 0, 0);
      wstring sPath = BSTR2WStr(vtProp.bstrVal);

      hr = pclsObj->Get(L"RemoteName", 0, &vtProp, 0, 0);
      wstring sVol = BSTR2WStr(vtProp.bstrVal);
     
      wcout << "RemoteName: " << sVol << endl;

      VariantClear(&vtProp);
      pclsObj->Release();
     }
     pSvc->Release();
     pLoc->Release();
     pEnumerator->Release();
     CoUninitialize();

     return hres;
    }

    Friday, August 30, 2013 4:46 PM
  • Please post in the developer forum.  This forum is not for C++ questions.


    ¯\_(ツ)_/¯

    Friday, August 30, 2013 4:49 PM
  • This worked perfect!  I desperately needed a way to find disconnected drives and then write a loop re-mount the disconnected ones.  I had no idea this could be done. Works like a charm in Win7... hope it work in Win10 (but haven't tested it yet).  Thanks for taking the time to post this solution.

    Thursday, September 15, 2016 5:39 AM