VBScript - Logon Script for home users

Posted on 2012-08-30
Last Modified: 2012-09-25
I have users working from home (25% of their time) using their personal computers to VPN into the network.  Users need access to their mapped drives when working from home.  Providing instructions to end-users on mapping drives has not been greatly UNsuccessful and has generated a number of helpdesk requests.  

Desired Outcome:
Create a custom logon script for home users to map drives.  Modify the existing logon script (vbscript) for this purpose.

Home users are logging into their home computer and not providing domain credentials.  The users home folder is mapped via AD User Profile property settings and not a logon script.  Include error handling, should the user input an invalid uname\pword they are prompted to retype these values

I hope that is enough detail to get us started.  So far I’ve been able to modify my script to  request network credentials but I am quickly outreaching my level of scripting competency.  I’ve looked at the following two scripts (below) and they appear to contain the elements that I need:

A)      Using the provided credentials, Query AD to get the list of groups to which the users belongs.
B)      Using the provided credentials,  Query AD to get the home folder path for the user.

What I need help with now, is mashing these together with a bit of error control should the user input invalid credentials.

Query AD for group membership:

Query to obtain Home Folder path:

Thanks for your help!
Question by:lpbenergy
    LVL 43

    Assisted Solution

    by:Steve Knight
    I don't have time to write this up specifically for you at the moment but I do have a script I wrote which you may be able to pull the relevant parts out of or give some pointers.

    That does drive mappings, OU and group checks etc. amongst other things along with an IE based status window.


    Author Comment

    Thanks Steve, I will take a look and see if I can use it.
    LVL 43

    Expert Comment

    by:Steve Knight
    Ok.  Real busy time at the moment before kids go back to school amongst other things...

    Just post back any questions or issues and will help if I can, reading your question better it is a little different to how I imagined it anyway.

    LVL 65

    Accepted Solution

    Hi, this is a HTA, but if you put this HTA on the users desktop (as MapDrives.hta) and run it, it should be able to map the drives for the home drive, and by group membership as well.

    The first thing you need to change it this:
    	' Specify a server (Domain Controller).
    	strServer = "YOURPDC"

    Open in new window

    and then you can configure "global" groups here:
    	' Configure your global resources here - they will be connected for every user
    	MapDrive "Z:","\\Server\User Storage\Pupils\" & lisYear.value & "\" & txtName
    	MapDrive "T:","\\Server\student shared$"

    Open in new window

    and your group membership mappings are configured in the MapDriveByGroup sub procedure.

    Hopefully it works out for you.



    <title>Drive Mapper</title>
         APPLICATIONNAME="Drive Mapper"
    body {
    td,input	{
    a	{
    Set objNetwork = CreateObject("Wscript.Network")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Sub Window_OnLoad
    	Dim X, Y, strComputer, objWMIService, colItems, objItem, intHorizontal, strYear
    	window.resizeTo X,Y
    		' resize the HTA
    	strComputer = "."
    	Set objWMIService = GetObject("Winmgmts:\\" & strComputer & "\root\cimv2")
    	Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor")
    	For Each objItem in colItems
    		intHorizontal = objItem.ScreenWidth
    		intVertical = objItem.ScreenHeight
    	window.moveTo (intHorizontal - X) / 2, (intVertical - Y) / 2
    		' centre it
    End Sub
    Sub btn_Configure_OnClick
    	If txt_Username.value="" Then
    		MsgBox "Please enter your name",16,"ERROR"
    		Exit Sub
    	End If
    	If txt_Password.value="" Then
    		MsgBox "Please enter your password",16,"ERROR"
    		Exit Sub
    	End If
    	' This will call the GetGroupMembership sub which will in turn call the MapDriveByGroup sub
    	' Configure your global resources here - they will be connected for every user
    	MapDrive "Z:","\\Server\User Storage\Pupils\" & lisYear.value & "\" & txtName
    	MapDrive "T:","\\Server\student shared$"
    	MsgBox "Network resources have been configured",64,"DONE"
    End Sub
    Sub MapDrive(DriveLetter,DrivePath)
    	If objFSO.DriveExists(DriveLetter) Then
    		objNetwork.RemoveNetworkDrive DriveLetter, True
    	End If
    	objNetwork.MapNetworkDrive DriveLetter, DrivePath, False, txt_Username.value, txt_Password.value
    End Sub
    Sub MapDriveByGroup(sGroupName)
    	Select Case LCase(sGroupName)
    		Case LCase("IT")
    			MapDrive "G:", "\\server\IT_Share"
    		Case LCase("HR")
    			MapDrive "G:", "\\server\HR_Share"
    	End Select
    End Sub
    Sub GetGroupMembership
    	Const ADS_SERVER_BIND = &H200
    	' Specify a server (Domain Controller).
    	strServer = "YOURPDC"
    	' Specify or prompt for credentials.
    	strUser = txt_username.value
    	strPassword = txt_password.value
    	' Determine DNS domain name. Use server binding and alternate
    	' credentials. The value of strDNSDomain can also be hard coded.
    	Set objNS = GetObject("LDAP:")
    	On Error Resume Next
    	Set objRootDSE = objNS.OpenDSObject("LDAP://" & strServer & "/RootDSE", strUser, strPassword, ADS_SERVER_BIND Or ADS_SECURE_AUTHENTICATION)
    	If Err.Number = 0 Then
    		strDNSDomain = objRootDSE.Get("defaultNamingContext")
    		' Use ADO to search Active Directory.
    		' Use alternate credentials.
    		Set adoCommand = CreateObject("ADODB.Command")
    		Set adoConnection = CreateObject("ADODB.Connection")
    		adoConnection.Provider = "ADsDSOObject"
    		adoConnection.Properties("User ID") = strUser
    		adoConnection.Properties("Password") = strPassword
    		adoConnection.Properties("Encrypt Password") = True
    		adoConnection.Properties("ADSI Flag") = ADS_SERVER_BIND Or ADS_SECURE_AUTHENTICATION
    		adoConnection.Open "Active Directory Provider"
    		Set adoCommand.ActiveConnection = adoConnection
    		' Search entire domain. Use server binding.
    		strBase = "<LDAP://" & strServer & "/" & strDNSDomain & ">"
    		' Search for all users.
    		strFilter = "(&(objectCategory=person)(objectClass=user)(samAccountName=" & strUser & "))"
    		' Comma delimited list of attribute values to retrieve.
    		strAttributes = "distinguishedName"
    		' Construct the LDAP query.
    		strQuery = strBase & ";" & strFilter & ";" _
    		     & strAttributes & ";subtree"
    		' Run the query.
    		adoCommand.CommandText = strQuery
    		adoCommand.Properties("Page Size") = 100
    		adoCommand.Properties("Timeout") = 30
    		adoCommand.Properties("Cache Results") = False
    		Set adoRecordset = adoCommand.Execute
    		' Enumerate the resulting recordset.
    		strDN = ""
    		Do Until adoRecordset.EOF
    		     ' Retrieve values.
    		     strDN = adoRecordset.Fields("distinguishedName").Value
    		If strDN = "" Then
    			MsgBox "User " & strUser & " was not found."
    			Set objUser = GetObject("LDAP://" & strDN)
    			' This line will map the home drive of the user
    			MapDrive objUser.HomeDrive, objUser.HomeDirectory
    			' Now we will map the other drives by group membership
    			If TypeName(objUser.MemberOf) = "Empty" Then
    				MsgBox "You are not a member of any groups."
    			ElseIf TypeName(objUser.MemberOf) = "String" Then
    				strGroupName = Mid(Split(objUser.MemberOf, ",")(0), 4)
    				MapDriveByGroup strGroupName
    				For Each strGroup In objUser.MemberOf       'Walk through the groups that user is a member of
    					strGroupName = Mid(Split(strGroup, ",")(0), 4)              'Get the group name
    				    MapDriveByGroup strGroupName
    			End If
    		End If
    		MsgBox "Connection failure.  Wrong password?"
    	End If
    	On Error GoTo 0
    End Sub
    	<td>Enter your username?<br/></td>
    	<td><input type="text" id="txt_Username" name="txt_Username" /></td>
    	<td>What is your password?</td>
    	<td><input type="password" id="txt_Password" name="txt_Password" /></td>
    	<td colspan="2">&nbsp;</td>
    	<td colspan="2" align="center"><input type="button" value="Configure Resources" id="btn_Configure" /></td>

    Open in new window


    Author Comment

    Thanks Rob, it first glance it appears that your script has many of the elements I am looking for.  I'll take a look and let you know how it goes.

    Author Closing Comment

    Thanks to you both for your submissions I liked them both.  I gave Rob Sampson higher points because his solution is a closer fit to what I am requesting.  Dragon-IT, i really like what you are doing with the IE Popup.  I am going to try and utilize it in my next script.

    Nice work both of you.
    LVL 43

    Expert Comment

    by:Steve Knight
    No problem, can't remember where I saw it originally but it has been much fiddled with, and in environments with IE in known states normally.  If you get stuck with any of it post a new question and a link back here and we'll see it.

    LVL 65

    Expert Comment

    Thanks for the grade.


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
    Communication between departments might not happen in two different languages, but they do exist in two different worlds. With different targets and performance goals the same phrase often means something completely different to each party. Learn ho…
    Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

    729 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