Only part of Logon script  works on Windows 2003 Domain

Posted on 2006-04-20
Last Modified: 2012-06-27

  Well I am having an issue with a logon script that only does part of what it should do. The script I have pasted below is the one in question. This script reads from a SQL database the workstation name and drives/printers to map and then does its thing. The part of the script that maps my printers works fine but the part that maps the drives does not work. It is at line 107 inside of the Function MapDrive (DriveLetter, UNCPath) and this line "WshNetwork.MapNetworkDrive DriveLetter,UNCPath" is where it gets the error shown at the bottom of this message.    
If you go to the bottom of this message you will see where I have queried a few of the variables and determined that they are correct, but it gives the error Network Path not found. And I have verified that all of the drive mappings are correct... I can copy right from the database the share name and paste it in to a Start-Run command and it brings up the share just fine. The printer function works fine. I can read and write inside the shares with no problem. This script also works on another site where I work with no issues. Any ideas? Thanks,


'MapWorkStationDrivesAndPrinters.vbs - Script to Map Network (NOT LOCAL) Drives and Printers for a Workstation
'                              - The Drive and Printer Mappings Come from the LCPC_WSDriveAndPrinterMaps
'                                table in the AppsMain database on the ISSQL2 server
'* Create network objects for later reference
Dim WshNetwork
Dim ObjSysInfo
Dim ClientName
Dim wsName
Dim mPrinters
Dim mDrives

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objSysInfo = CreateObject("ADSystemInfo")

'Open connection to LCPC created tables store on the ISSQL2 sql server
Dim conn, rs,SQLText
 Set conn=CreateObject("ADODB.Connection")
With conn
    If .State = 0 Then
            .Provider = "SQLOLEDB"
            .ConnectionString = "User ID=WSmap;" & _
                                "Password='';" & _
                                "Data Source=ISSQL2;" & _
                                "Initial Catalog=AppsMain"
            .ConnectionTimeout = 20
    End If
End With
'Find the drive and printer mappings that have been assign to this computer name in the table then MapDrive
'LCPC_WSDriveAndPrinterMaps Row Example:
'WSMapID   WSName      DriveLetter  ShareName                DefaultPrinterFlg
'   1      W4657           O       \\ISSQL2\Medinfo                0
'   2        *                 S           \\ISTRM2\Users                0
'   3      W4657         (NULL)    \\SomePrinterShareName         1
'   4      W4657           (NULL)    \\AnotherPrinterShareName      0
'Notice the * for WSName.  This is a Global setting whereby all workstations that run this script will get this
'                        mapping; however, the global setting will be overwritten if the workstation
'                        has an explicit entry for a drive letter
 Set rs=CreateObject("ADODB.Recordset")
 wsName = WshNetwork.ComputerName

 SQLText = "Select * from LCPC_WSDriveAndPrinterMaps  where WSName = '" & wsName & "'"
 rs.Open SQLText, Conn, 0,1
 On Error Resume Next
 If err.number = 3021 Then WScript.Quit
Set oDrives = WshNetwork.EnumNetworkDrives
      mDrives = oDrives.Count - 1
          For i = 0 to mDrives Step 2
                If oDrives.Item(i) <> "U:" Then
                      WshNetwork.RemoveNetworkDrive oDrives.Item(i), True, True
                End If
Set oPrints = WshNetwork.EnumPrinterConnections
          mPrinters = oPrints.Count - 1
      For i = 0 to oPrints.Count - 1 Step 2
                WshNetwork.RemovePrinterConnection oPrints.Item(i+1)
If Err.Number<>0 Then Err.Clear


 SQLText = "Select * from LCPC_WSDriveAndPrinterMaps  where WSName = '" & wsName & "'" & " Or WSName = '*' Order By WSName"
 rs.Open SQLText, Conn, 0,1
 On Error Resume Next
 Do While not rs.EOF
      If IsNull(rs("DriveLetter")) Then
            MapPrinter rs("ShareName")
            If rs("DefaultPrinterFlg") Then WshNetwork.SetDefaultPrinter rs("ShareName")                  
             MapDrive rs("DriveLetter") & ":" ,rs("ShareName")
      End If


Set rs=Nothing
Set Conn=Nothing

'* Functions

Function MapDrive (DriveLetter, UNCPath)
    On Error Resume Next
    WshNetwork.RemoveNetworkDrive DriveLetter
    WshNetwork.MapNetworkDrive DriveLetter,UNCPath
    If Err.Number <> 0 Then
        PrintMessageBox "Error mapping " & DriveLetter & " to path " & UNCPath
    End If
End Function

Function MapPrinter (UNCPath)
    WshNetwork.AddWindowsPrinterConnection UNCPath
    If Err.Number <> 0 Then
        PrintMessageBox "Error mapping printer " & UNCPath
    End If
Exit Function
BailonFailure err.number, err.description
End Function

'* Error Output to User
Sub BailOnFailure(ErrNum, ErrText)
    strText = "Please report the following to your department's system administrator:" & vbCR & ErrText & vbCR & "Error 0x" & Hex(ErrNum)
    MsgBox strText, vbInformation, "Logon Script Error"
End Sub

Sub PrintMessageBox(ErrText)
Exit Sub
    strText = "Please report the following to your department's system administrator:" & vbCR & ErrText
    MsgBox strText, vbInformation, "Logon Script Error"
End Sub

************  Results while in Debug mode **********

The network path was not found.



Question by:SOMCEE
    LVL 51

    Accepted Solution

    The script below works for me when I replace \\server\share with a valid share.  It's just the portion of your script that maps the drive.

    Use it to see if \\ISSQL2\MEDINFO is there - also check to see if this isn't a hidden share MEDINFO$.


    Dim WshNetwork
    Dim DriveLetter
    Dim UNCPath

    UNCPath = "\\server\share"

    Set WshNetwork = WScript.CreateObject("WScript.Network")

    WshNetwork.MapNetworkDrive DriveLetter, UNCPath



    Author Comment


     DUH!!! I seen my mistake as soon as I looked at your code... I thought I had changed this and tried it once before but evidently NOT!! The problem was I had in the database "\\server\share\" it should be "\\server\share" NO ending backslash!! Thanks for helping by just letting me see it on paper... feel really stupid!! Just tried it and it works like a champ!!
    LVL 51

    Expert Comment

    No problem!  Sometimes, when you work at something long enough you look right past the obvious.  An extra set of eyes is always welcome.

    Glad to help!


    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Scenerio: You have a server running Server 2003 and have applied a retail pack of Terminal Server Licenses.  You want to change servers or your server has crashed and you need to reapply the Terminal Server Licenses. When you enter the 16-digit lic…
    Learn about cloud computing and its benefits for small business owners.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now