1

This query runs fine on my local machine:

strComputer = "."
drive = "C:"
path = "\\path\\to\\local\\folder\\"

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * From CIM_DataFile Where Path = '"&path&"' and drive='"&drive&"'")

If colFiles.Count < 1 Then
    Wscript.Echo "Folder does not exist"
Else
    Wscript.Echo "Folder does exist"
End If

But when I try to query a mapped network drive, the program fails with 'Folders does not exist'. Yet I am sure it is the correct path to the file.

The only parts that change are:

drive = "Z:"
path = "\\path\\to\\mapped\\drive\\folder\\"

Any clues as to why this would not work?

Lizz
  • 1,442
  • 5
  • 25
  • 51
Chris M
  • 75
  • 7
  • Using double backslashes like that is not appropriate in vbscript. – Hans Passant Oct 18 '12 at 13:05
  • Ok, but the program runs fine for a local file. And that uses double backslashes. So I don't think it would be the backslashes. – Chris M Oct 19 '12 at 09:50
  • Avoid thinking about it. Actually remove the backslashes and try again. What happens? – Hans Passant Oct 19 '12 at 12:08
  • When I remove the backslashes from the path name, I receive an error from windows script host reading 'Invalid query', source SWbemObjectSet, code 80041017. – Chris M Oct 22 '12 at 09:04

2 Answers2

2

Trying to map drives on a remote computer via WMI will fail, though there is a workaround. Thanks to Frank White's inspirational code, a fully fleshed process now exists to map a drive on a remote computer via WMI using a command prompt and passing explicit credentials.

https://stackoverflow.com/a/11948096/1569434

Community
  • 1
  • 1
Lizz
  • 1,442
  • 5
  • 25
  • 51
0

So to debug this I ran the following:

strComputer = "."
 Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 Set colFiles = objWMIService.ExecQuery("Select * from CIM_Datafile Where Drive = 'Z:'")
 For Each objFile in colFiles
    Wscript.Echo objFile.Name
 Next

This resulted in the error 'remote procedure call failed', which I understand means that the mapped drive does not support WMI.

Chris M
  • 75
  • 7