3

I have several scripts that must use UNC paths - not DFS - and would like to be able to determine the UNC path programmatically from the DFS path. For example, we have something like:

\\domain\fs\Home\HomeFolder\MyUserID

and I would like to get from it the UNC path like this:

\\Server1\HomeFolder\MyUserID

I cannot count on a utility like DFSUtil.exe to be available. It will need to be in VBScript.

I found the following code in NET which uses WMI but I can't tease out what's happening to convert it to a usable VBS: http://www.codeproject.com/Tips/158829/Convert-a-file-path-to-a-UNC-Path

Can anyone lend a hand? I'm lost in translation (don't speak NET)...

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Lizz
  • 1,442
  • 5
  • 25
  • 51
  • Seems Raymund kindly posted good C# code for this http://stackoverflow.com/questions/3938669/how-can-i-get-an-active-unc-path-in-dfs-programatically but it isn't in VBS... – Lizz Jan 16 '13 at 09:11
  • 1
    http://include.wutils.com/wmi/ROOT%5Ccimv2/CIM_ManagedSystemElement/CIM_LogicalElement/Win32_DfsTarget.html might be a staring point – stuartd Jan 16 '13 at 16:32
  • @StuartDunkeld +1 link. I tried a couple of their scripts and got one "working" though it doesn't spit back any info - at all. Also, if a WMI query will need to be run against a domain controller, then I'll need admin rights on those, and this isn't always the case, so the script would be less than reliable... BTW, this link was also helpful in creating a VBS to guide in better direction: http://www.tech-archive.net/Archive/Scripting/microsoft.public.scripting.vbscript/2007-07/msg00125.html – Lizz Jan 17 '13 at 23:02

1 Answers1

1

This is a minimal VBScript translation of what you linked to:

Option Explicit

Dim wmi
Dim col
DIm itm

Set wmi = GetObject("winmgmts:\\.\root\cimv2")
Set col = wmi.ExecQuery("Select DeviceID,ProviderName From Win32_LogicalDisk where DriveType=4")

With WScript
  For Each itm in col
    .Echo "========="
    .Echo "DeviceID:" & vbTab & itm.DeviceID
    .Echo "ProviderName:" & vbTab & itm.ProviderName
  Next
End With

Set wmi = Nothing
Set col = Nothing
WScript.Quit

In short, I don't think it is quite what you want... I think you would have to map drives first before running this.

The tidier way to do it would be to run a query against Active Directory to find the DFS share definitions, but where to actually look within AD can be difficult to identify

Jobbo
  • 1,358
  • 1
  • 8
  • 21
  • Thanks Jobbo! I don't have an environment to test this now, but will see what I can do. In the meantime, I looked again at a 2nd answer (also in C#) to one of the links above, and it doesn't use WMI, but rather NetAPI32.dll: http://stackoverflow.com/a/13470440/1569434. This is likely the way a typical user would be able to get the UNC path. Again, I don't know C#. Bonus points if you can help with its translation to VBS. :) Pretty please? – Lizz Oct 15 '13 at 15:24
  • 1
    You're welcome. I can't test it either, but at least its a translation.I inspected that other question. NetAPI32.dll is mentioned as a reference to the Win32 API, can't do that with VBScript I'm afraid. The accepted answer in that question is also using WMI but is just doing something similar to my answer here, and would require you to run it on loads of servers (if you know their names) and have the rights to do so – Jobbo Oct 15 '13 at 15:59