DFS namespace query tool

Posted on July 29th, 2009 in Server 2003, Server 2008, Storage by alt-92

For quick lookups to find out where DFS links are pointing to, I’ve built dfsquery.exe in C++.
standalone executable, 64bits support and works on NT5.1 and up (including Windows 7).

Based on MSDN code sample from http://msdn.microsoft.com/en-us/library/bb524791(VS.85).aspx

01
02
#define UNICODE
03
#include <stdio.h>
04
#include <windows.h>
05
#include <lm.h>
06
#include <lmdfs.h>
07
 
08
void wmain(int argc, wchar_t *argv[ ])
09
{
10
   PDFS_INFO_4 pData;
11
   PDFS_STORAGE_INFO ps;
12
   DWORD er=0, tr=0, res, j;
13
 
14
   //
15
   // Check command line arguments.
16
   //
17
   if (argc<2)
18
      wprintf(L"Syntax: %s DfsEntryPath\n", argv[0]);
19
   else
20
   {
21
      //
22
      // Call the NetDfsGetInfo function, specifying level 4.
23
      //
24
      res = NetDfsGetInfo(argv[1], NULL, NULL,  4, (LPBYTE *) &pData);
25
      //
26
      // If the call succeeds, print the data.
27
      //
28
      if(res==0)
29
      {
30
 printf("Report for: %-30S\nStorages: %u\nComment: %S\n",pData->EntryPath, pData->NumberOfStorages, pData->Comment, pData->Timeout);
31
 printf("Timeout: %u\n",pData->Timeout);
32
 ps = pData->Storage;
33
         //
34
         // Loop through each target.
35
         //
36
         for(j=1;j<=pData->NumberOfStorages;j++)
37
         {
38
            //
39
            // Print the status (Offline/Online) and the name 
40
            // of each target referenced by the DFS link.
41
            //
42
 printf("Target %S  ", (ps->State == DFS_STORAGE_STATE_OFFLINE) ? TEXT("Offline:"): TEXT("Online :"));
43
            printf("\\\\%S\\%S\n",ps->ServerName,ps->ShareName);
44
            ps++;
45
         }
46
         //
47
         // Free the allocated memory.
48
         //
49
         NetApiBufferFree(pData);
50
      }
51
      else
52
         printf("Error: %u\n", res);
53
   }
54
   return;
55
}
56

It’s quick, it’s probably dirty, but it works just fine (and no admin privileges needed as it does the same as your average DFS client code).

Sample output:

D:\>dfsquery \\alt-92.net\files\0054
Report for: \\ALT-92\files\0054
Storages: 2
Comment:Department 54 data store
Timeout: 300
Target Online : \\ENDEAVOUR\data\0054
Target Offline: \\Equinoxe\data\0054

It shows timeout value, comments (description field in DFS console) the number of link targets and their individual link state.

Comments are closed.