Friday, October 27, 2006

DriveMapEmployee.vbs: Map network drive

Author: Robert Lawson
Environment: Windows Server, Active Directory, Group Policy, vb-script
Description: This script maps a user's network drive to a pre-reserved Windows drive for two campus. The campus is determined by user's primary IP address. This script is run at log on from Active Directory Group Policy. Each step is written to application event log for easy trouble shooting.

Code:

'=========================================================================
' FILE : DriveMapEmployee.vbs
' AUTHOR : Robert Lawson
' COMPANY: Soka University of America
' DATE : 10/27/2006
' COMMENT: Map primary drive for employees
'
'=========================================================================
Option Explicit
On Error Resume Next ' Required, or fails if already mapped

const conScriptName = "DriveMapEmployee.vbs"
const conDEBUG = TRUE

Dim objNetwork, objShell, objDrives
Dim strComputerName, strUserName, strUserDomain
Dim strDrive, strShare, strCurrentCampus, strDriveOld, strShareOld
Dim bolAlreadyMapped, bolAVCampus, bolCALCampus
Dim wmi, nics, nic, ip, strIP, i
Dim strMsg

Const conAVIP = "10."
Const conCALIP = "172.16"
Const conAVCampus = "AlisoViejo"
Const conCALCampus = "Calabasas"
Const conUNKCampus = "Unknown"
Const conAVshare = "\\server1\userstore\"
Const conCALshare = "\\rserver2\Employees\"
Const conMapDrive = "U:"

' ============================================================
' Setup
' ============================================================
Set objNetwork = Wscript.CreateObject("Wscript.Network")
Set objShell = CreateObject( "WScript.Shell" )

strMsg = "BEGIN: " & conScriptName
if conDEBUG then Wscript.Echo strMsg
objShell.LogEvent 0,strMsg

' ============================================================
' Get user info
' ============================================================
strComputerName = objNetwork.ComputerName
strUserName = objNetwork.UserName
strUserDomain = objNetwork.UserDomain
strMsg = conScriptName & " values: CN = " & strComputerName & "; UN = " & strUserName & "; DN = " & strUserDomain
if conDEBUG then Wscript.Echo strMsg
objShell.LogEvent 0,strMsg

' Get IP address (there could be multiple)
bolAVCampus = FALSE
bolCALCampus = FALSE
Set wmi = GetObject("winmgmts:\\.\root\cimv2")
Set nics = wmi.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each nic In nics
For Each ip In nic.ipaddress
strIP = ip
'strIP = "172.16.10.11" '*************** DEBUG
if conDEBUG then WScript.Echo "IP: " & strIP
if LEN(strIP) > 0 then
if left(strIP,3) = conAVIP then
bolAVCampus = TRUE
elseif left(strIP,6) = conCALIP then
bolCALCampus = TRUE
End if
End if
Next
Next

' You know current campus
if bolAVCampus then
strCurrentCampus = conAVCampus
elseif bolCALCampus then
strCurrentCampus = conCALCampus
else
strCurrentCampus = conUNKCampus
end if
if conDEBUG then Wscript.Echo "Campus = " & strCurrentCampus

' Did you figure out campus?
if strCurrentCampus = conUNKCampus then
strMsg = conScriptName & ": Unable to deterine campus from IP address"
if conDEBUG then Wscript.Echo strMsg
objShell.LogEvent 0,strMsg
WScript.Quit
end if

' ============================================================
' Map away
' ============================================================
strDrive = conMapDrive

if strCurrentCampus = conAVCampus then
strShare = conAVshare & strUserName
elseif strCurrentCampus = conCALCampus then
strShare = conCALshare & strUserName
end if

' Get current mapping
bolAlreadyMapped = False
Set objDrives = objNetwork.EnumNetworkDrives
For i = 0 to objDrives.Count - 1 Step 2
strDriveOld = objDrives.Item(i)
strShareOld = objDrives.Item(i+1)
if conDEBUG then WScript.Echo "Drive " & strDriveOld & " = " & strShareOld
bolAlreadyMapped = (strDrive = strDriveOld) and (strShare = strShareOld)
Next
if conDEBUG then WScript.Echo "bolAlreadyMapped = " & bolAlreadyMapped

if NOT bolAlreadyMapped then
' Remove mapping if it is already there (else mapping will fail)
objNetwork.RemoveNetworkDrive strDrive, TRUE, TRUE
err.clear

' Map thu pig
objNetwork.MapNetworkDrive strDrive, strShare, TRUE
If err.number = 0 then
strMsg = conScriptName & ": Mapped " & strDrive & " to " & strShare
if conDEBUG then Wscript.Echo strMsg
objShell.LogEvent 0,strMsg
else
strMsg = conScriptName & ": Err = " & err.number & "; " & err.description
if conDEBUG then Wscript.Echo strMsg
objShell.LogEvent 0,strMsg
WScript.Quit
End if
else
strMsg = conScriptName & ": No change, alread mapped " & strDrive & " to " & strShare
if conDEBUG then Wscript.Echo strMsg
objShell.LogEvent 0,strMsg
end if ' bolAlreadyMapped

' ============================================================
' The End
' ============================================================
strMsg = "END: " & conScriptName
if conDEBUG then Wscript.Echo strMsg
objShell.LogEvent 0,strMsg

Set objNetwork = Nothing

No comments: