Roberto Alves

Scripting and server based computing

XML-based map network drives for login scripts

I found at Forum a very useful script wrote by a user called “ginolard”, that can be used at login scripts or if you need, you can use the basic structure of this script into others scripts with different proposes.

Basically, this script read a XML file and execute mapnetwork commands considering the information gathered. The great advantage of use this model (scripts reading XML or INI files to get information), is that you can keep the same script for a long time without amends.

For example, if your login script map network drives according user groups, you can keep the “rules” in the information file (XML or INI) and in the future, to add groups or maps, you need just change the file. Without touch in a script that can cause problems in a production environment.

XML-based script:

Sub MapNetworkDrives
On Error Resume Next
‘Map group-specific drives based on the entries in the MappedDrives.xml file.
Dim objNetwork    :     Set objNetwork = CreateObject(“WScript.Network”)
Dim objFSO        :    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Dim objSysInfo    :     Set objSysInfo = CreateObject(“ADSystemInfo”)
Dim objUser     :    Set objUser = GetObject(“LDAP://” & objSysInfo.UserName)
Dim XMLDoc        :     Set xmlDoc = CreateObject(“Microsoft.XMLDOM”)

Dim MD_XMLPath    :    MD_XMLPath = GetLogonServer & “NETLOGONMWTESTMappedDrives.xml”
Dim GroupObj,objDriveNode
‘Load the XML file

xmlDoc.async = False
If objFSO.FileExists(MD_XMLPath) Then

‘Compare each group the user is a member of and see if it is found in the XML file
‘If so, retrieve the UNC path and drive latter to be mapped.

For Each GroupObj In objUser.Groups
Set objDriveNode = xmlDoc.selectSingleNode(“//MappedDrives/Group[@Name = '" & Ucase( & "']“)
If Not objDriveNode Is Nothing Then
objNetwork.MapNetworkDrive objDriveNode.childNodes(1).text & “:”, objDriveNode.childNodes(0).text,True
If Err.Number <> 0 Then
WriteToErrorLog “Error mapping ” & objDriveNode.childNodes(1).text & ” to ” & objDriveNode.childNodes(0).text
End If
End If
End If
End Sub

XML format to be followed:

<Group Name=”SERVER_GROUP1″>

Category: Scripts
Tag: ,

Your email address will not be published. Required fields are marked *