• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2034
  • Last Modified:

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

1 Solution
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...


7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

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

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.


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now