Stanford Windows Infrastructure Documentation

Generic Logon script to map drives and printers in GPO

The script below is designed to be embedded into a GPO that linked to an Organizational Unit that contains machines where this logon script will run for all users. The machines must be configured for GPO loopback policy processing because logon scripts are configured in the User section of the group policy, not the Computer section.  This is usually set in the same GPO.

Logon.vbs


'********************
'* GROUP NAME VARIABLES (for conditional mappings)
'*
'* Group names should match the "Pre-Windows 2000" group names in Active Directory
'* Note that the slash is a forward slash "/", not the normal backslash "\"
'* Using variables makes it easy to find and update group names later.
'********************

Group1 = "SU/GROUPNAME"

'********************
'* OPTIONS
'********************

'Turns on/off the hack to allow Stanford Registry Workgroup Manager workgroups to be detected. If you use
'Workgroups as mapping conditions directly, remember that ":" is converted to "-" in group names
'when imported into Active Directory when creating the group name variable.

DoWorkgroups = True

'********************
'* SETUP
'********************

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

'***** HACK to handle cross-realm logon
If WshNetwork.UserDomain = "stanford.edu" Then
    UserDN = "WinNT://WIN/" & WshNetwork.UserName
Else
    UserDN = "WinNT://" & WshNetwork.UserDomain & "/" & WshNetwork.UserName
End If
ComputerDN = "WinNT://" & objSysInfo.DomainShortName & "/" & WshNetwork.ComputerName & "$"

'***** HACK to handle Registry WorkGroups
Dim oRWG
If DoWorkgroups Then
    Set oRWG = CreateObject("Scripting.Dictionary")
    oRWG.CompareMode = 1
    Set oRegEx = new RegExp

    Set UserObject=GetObject(UserDN & ",user")
    For Each Group in UserObject.Groups
        oRegEx.Pattern = "(.*)-Member$"
        If oRegEx.Test(Group.Name) Then
            Set Matches = oRegEx.Execute(Group.Name)
            oRWG.Add "WinNT://WIN/" & Matches(0).SubMatches(0), Group.Name
        Else
            oRegEx.Pattern = "(.*)-\d{1,2}$"
            If oRegEx.Test(Group.Name) Then
                Set Matches = oRegEx.Execute(Group.Name)
                oRWG.Add "WinNT://WIN/" & Matches(0).SubMatches(0), Group.Name
                Else
                    oRWG.Add Group.Name, Group.Name
                End If
        End If
    Next
End If

Set oMapped = CreateObject("Scripting.Dictionary")
oMapped.CompareMode = 1

On Error Resume Next

Set oDrives = WshNetwork.EnumNetworkDrives
For i = 0 to oDrives.Count - 1 Step 2
    oMapped.Add oDrives.Item(i), oDrives.Item(i+1)
    Next
Set oPrints = WshNetwork.EnumPrinterConnections
For i = 0 to oPrints.Count - 1 Step 2
    oMapped.Add oPrints.Item(i+1), oPrints.Item(i)
    Next
If Err.Number<>0 Then Err.Clear

'*****************
'* MAIN LOGON SCRIPT EXECUTION
'*
'* Edits must be done here before deploying the script.
'* Group name variables (in Bold) need to be set for each conditional block. Copy the examples to make more conditional blocks
'* Drive letters and UNC paths (in italics) to be set to match your environment. Remove or add MapDrive and MapPrinter commands as needed.
'*****************

'Map default drives/printers example

MapDrive "X:","\\servername\shared"
MapPrinter "\\printserver\printer"

'Conditional Mappings example, Note that Group1 is the variable set at the beginning of the posted script

Set GroupObject = GetObject("WinNT://" & Group1 & ",group")
If Err.Number <> 0 Then BailonFailure Err.Number, "Group " & Group1 & " does not exist"
    'Map if user is a member of the target group
    If IsGroupMember(GroupObject,UserDN) Then
        MapDrive "X:","\\server\share"
        MapPrinter "\\printserver\groupprinter"
        End If
    'Map if the computer is a member of the target group
        If IsGroupMember(GroupObject,ComputerDN) Then
        MapDrive "Y:","\\server\share"
        MapPrinter "\\printserver\groupprinter"
        End If

'*****************
'* SCRIPT FUNCTIONS
'*****************
Function IsGroupMember (oGroup,strUser)
    IsGroupMember = False

    If DoWorkgroups Then
        if oRWG.Exists(oGroup.ADsPath) AND strUser = UserDN Then
            IsGroupMember = True
            Exit Function
        End If
    End If

    If oGroup.IsMember(strUser) Then
        IsGroupMember = True
        Exit Function
        End If

    set oMembers = oGroup.Members
    oMembers.Filter = Array("Group")
    For Each childGroup In oMembers
        If childGroup.Class = "Group" Then
        Set childGroup = GetObject(childGroup.ADsPath & ",group")
            If IsGroupMember(childGroup,strUser) Then
                IsGroupMember = True
                Exit Function
                End If
            End If
        Next

    End Function

Sub MapDrive (DriveLetter, UNCPath)
    On Error Resume Next
        If oMapped.Exists(DriveLetter) Then
            If oMapped.Item(DriveLetter) <> UNCPath Then
                UnMapDrive DriveLetter
            Else
                Exit Sub
            End If
        End If
    WshNetwork.MapNetworkDrive DriveLetter,UNCPath
    If Err.Number <> 0 Then
        PrintMessageBox "Error mapping " & DriveLetter & " to path " & UNCPath
        Err.Clear
    Else
        oMapped.Add DriveLetter,UNCPath
    End If
End Sub

Sub UnMapDrive (DriveLetter)
    WshNetwork.RemoveNetworkDrive DriveLetter
    oMapped.Remove(DriveLetter)
End Sub

Sub MapPrinter (UNCPath)
    On Error Resume Next
    If oMapped.Exists(UNCPath) Then
        Exit Sub
        End If
    WshNetwork.AddWindowsPrinterConnection UNCPath
    If Err.Number <> 0 Then
        PrintMessageBox "Error mapping printer " & UNCPath
        Err.Clear
    Else
        oMapped.Add UNCPath,UNCPath
    End If
End Sub

Sub MapDefaultPrinter (UNCPath)
    MapPrinter UNCPath
    WshNetwork.SetDefaultPrinter UNCPath
End Sub

Sub UnMapPrinter (UNCPath)
    If oMapped.Exists(UNCPath) Then
        WshNetwork.RemovePrinterConnection UNCPath
        oMapped.Remove(UNCPath)
        End If
End Sub

'**********************
'* ERROR OUTPUT FUNCTIONS
'**********************
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"
    WScript.Quit
End Sub

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



Created: May 5, 2005 by Ross Wilper
Last modified: October 09, 2007 by Ross Wilper
©2007 Trustees of the Leland Stanford Junior University
Information Technology Systems and Services