locked
WScript.Network.EnumNetworkDrives returns nothing when the associated vbs script is ran by a service RRS feed

  • Question

  • I use a network monitoring system called Zabbix where I work. It is agent based and the agent has the ability to run an external program / script and then return the results to the Zabbix server.

    I wrote a script to get a list of the currently mapped network drives and then compare it to a hard-coded list of expected drive letters. If they are all present then the agent returns a 1. It returns 0 otherwise.

    The script runs fine if I run it on the client machine manually via a command window. However if the Zabbix agent runs it the EnumNetworkDrives method returns nothing. That results in the agent returning a 0 to the server, which in turn sends out all kinds of email alerts.

    I have tried changing the run as user to a local machine admin account but that didn't change anything.

    Here is the script:

    REM This script will return true or false to the calling process indicating if all expected network drives
    REM are properly mounted. This information is used by Zabbix to generate user alerts and emails if needed
    
    Option Explicit
    
    Dim objNetwork, objDrives
    Dim strSubst, strSubstVal, strSubstName, strEnumDrive
    Dim aExpectedDrives(9)
    Dim bDrivesMapped
    Dim strCurrentDrives
    Dim i REM Just a loop index
    Dim myLog
    Dim objFSO
    Const For_Writing = 2
    
    Set objNetwork = CreateObject("WScript.Network")
    Set objDrives = objNetwork.EnumNetworkDrives
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set myLog = objFSO.OpenTextFile("C:\zabbix\scripts\detect_net_drives.log", For_Writing, True)
    aExpectedDrives(0)="F:"
    aExpectedDrives(1)="G:"
    aExpectedDrives(2)="J:"
    aExpectedDrives(3)="N:"
    aExpectedDrives(4)="O:"
    aExpectedDrives(5)="P:"
    aExpectedDrives(6)="X:"
    aExpectedDrives(7)="Y:"
    aExpectedDrives(8)="Z:"
    bDrivesMapped=1
    strCurrentDrives = ""
    
    myLog.WriteLine("objDrives.Count is: " & objDrives.Count)
    
    For i = 0 to objDrives.Count -1 Step 2
    	strCurrentDrives = strCurrentDrives & " " & objDrives.Item(i)
    Next
    Trim(strCurrentDrives)
    
    myLog.WriteLine("The current drives on the machine are: " & strCurrentDrives & vbNewLine)
    
    Dim expected
    For Each expected In aExpectedDrives
    	REM if a drive letter in the strExpectedDrives array is not found in the 
    	REM objDrives object then set bDrivesMapped to false, exit loop and return
    	REM bDrivesMapped to calling process
    	
    	Rem MsgBox "Looking for drive: " & expected & " in current drives " & strCurrentDrives ,, "Blah"
    	
    	If InStr(strCurrentDrives, expected) = 0 Then
    		myLog.WriteLine("Drive: " & expected & " not found!" & vbNewLine)
    		bDrivesMapped = 0
    		Exit For
    	End If
    Next
    
    wscript.Echo(bDrivesMapped)
    myLog.WriteLine("Result: " & bDrivesMapped & vbNewLine)
    
    myLog.Close
    set myLog = Nothing
    Set objNetwork = Nothing
    Set objDrives = Nothing
    
    wscript.Quit(bDrivesMapped)

    Tuesday, April 22, 2014 8:02 PM

Answers

  • Mapped drives are a per-user setting (they sit in HKEY_CURRENT_USER), so what you're trying to do won't work.

    I recommend running your script in the user's context (e.g., a logon script) and write the results somewhere.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Eric Coan Wednesday, April 23, 2014 7:44 PM
    Tuesday, April 22, 2014 8:07 PM