WMI, CIM_LogicalFile and UNC Paths

hi all,

i'm trying to connect to a remote server via WMI and then establish the presence of a file on a UNC path.

is this possible using WMI and CIM_LogicalFile or CIM_DataFile? If not, how would i go about doing this?

only restriction is that it needs to be done in vb6...

what i'm currently doing is visible below, however it never seems to find anything, though the file does exist and i can access it from remote-a when logged into the terminal.

Set locator = New SWbemLocator
  Set services = locator.ConnectServer("remote-a", "root\cimv2", "user", "password")
 services.Security_.ImpersonationLevel = wbemImpersonationLevelImpersonate
Dim fileName As String
fileName = "\\\\remote-b\\path\\file.txt"
Set objset = services.ExecQuery("select name from CIM_LogicalFile where name='" & fileName & "'")

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Here is a simple script that doesn't use the above method since CIM_LogicaFile is extremely slow.

I'm using a simple Scripting.FileSystemObject here:

All you have to do is change:
1) sComputer to reflect the computer you want to connect to
2) sFile to reflect the location you want to search the file in.

Good luck :-)
Set oFS = CreateObject("Scripting.FileSystemObject")
sComputer = "ComputerNameHere"
sFile = "\\" & sComputer & "\C$\Folder\file.txt"
If oFS.FileExists(sFile) Then
    Set oFolder = oFS.GetFile(sFile)
    WScript.Echo "File found on: " & sComputer
    Wscript.Echo "File does not exist."
End If

Open in new window

appakAuthor Commented:
i only wish it were that easy ;)

this absolutely needs to be done in the context of the computer i'm connecting to.

as an example:
LOCAL is the computer i'm running the program from
REMOTE-A is the server i'm connecting to via WMI
\\REMOTE-B\share\file.txt is the unc path/file i'm checking for

so, from LOCAL, my application runs and connects to REMOTE-A, and then, in the context of REMOTE-A, attempts to check for \\REMOTE-B\share\file.txt

does anyone know for certain if wmi queries against CIM_LogicalFile/etc support UNC paths? a couple of msdn scripting articles allude to the fact that they should but i can't seem to find any working examples anywhere, nor can i get my own code to work... very frustrating!
Hi, there

>> and then, in the context of REMOTE-A, attempts to check for \\REMOTE-B\share\file.txt

this kind of confuses me.....are you saying you want to know whether a certain user account has access to a certain file?  If so, this user doesn't necessarily need to log onto a specific machine does it?  Couldn't you run a script *as* that user account on *any* computer, which then checks whether a file exists, and therefore has access to it, under that user context?

As Hubasan states, the CIM_DataFile query is very slow, so it would be best to avoid that if possible...


The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

appakAuthor Commented:
i'm trying to keep as closely as i can to the current architecture of an application...

basically, i suppose my real question at this point is:

do WMI queries to Win32_Process or CIM_LogicalFile support UNC paths? or is this a local-file-only thing?

Hi, from the testing I've done, UNC paths do not appear to work.  I even tried having a drive mapped to the network share and trying to find the file. That failed too.

The only think I could manage was to use PSExec to execute a script on Remote-A under that user context, that used the FileSystemObject to check for the file using the Remote-B UNC path.

See the script below, and see if it works for you.


Dim fileName
strServer = "remote-a"
fileName = "\\remote-b\path\file.txt"
strUser = "domain\user"
strPassword = "password"
strPSExec = "\\server\share\psexec_194.exe"
strScriptPath = "\\server\share\TempScript.vbs"
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objScript = objFSO.CreateTextFile(strScriptPath, True)
objScript.WriteLine "Set objFSO = CreateObject(""Scripting.FileSystemObject"")"
objScript.WriteLine "If objFSO.FileExists(""" & fileName & """) = True Then"
objScript.WriteLine "	WScript.Quit(0)"
objScript.WriteLine "Else"
objScript.WriteLine "	WScript.Quit(1)"
objScript.WriteLine "End If"
intReturn = objShell.Run(objFSO.GetFile(strPSExec).ShortPath & " -accepteula -i -u " & strUser & " -p " & strPassword & " \\" & strServer & " wscript.exe """ & objFSO.GetFile(strScriptPath).ShortPath & """", 1, True)
objFSO.DeleteFile strScriptPath, True
If intReturn = 0 Then
	WScript.Echo "File exists."
	WScript.Echo "File does not exist."
End If

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I'm confused...  Why are  you trying to connect to a remote PC in order to find a file on yet another remote PC?    Why not skip the "middle man" and perform the search directly on the server where the UNC share is?
... I obvously must be missing something, eh?
I thought the same thing....Unless there's a specific user account that you're testing that is only allowed to log onto one machine, we should really just be using those credentials from any PC, to see if that account has access to certain files....

appakAuthor Commented:
close enough for government work... thanks for the alternate solutions ;)
No problem. Thanks for the grade.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.