Link to home
Get AccessLog in
Avatar of slayer199
slayer199Flag for United States of America

asked on

VBScript logon script, creating home directories based on group, mapping drives, etc...problems

I'm relatively new to VBScript so please forgive me...I would have had this completed in batch already if the ifmember command didn't choke in our environment.  Time to teach this old dog some new tricks.

In any case, I'm writing a VBScript that will map the home drives based on group membership.  If the folder doesn't exist, it will create the home drive, assign permissions (not added yet) and ownership to the folder(not added yet), then map the drive.  If it exists, it will simply map the home drive on login.  I'll add the permissions/ownership part once I get the essentials completed.

My initial issue is that  I'm getting a redefined error on this line:  

Dim objFSO, objNetwork

The other 2 groups are commented out because their environments aren't ready.



On Error Resume Next
Dim objShell,objNetwork
Set objShell=CreateObject("Wscript.Shell")
Set objNetwork=CreateObject("Wscript.Network")
 
 
If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"mich") Then 
Set strUserLoc=mich
'If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"nynj") Then 
'Set strUserLoc=nynj
'If IsAMemberOf(objNetwork.UserDomain,objNetwork.UserName,"illinois") Then 
'Set strUserLoc=Illinois
 
Set objNetwork=Nothing
 
Else
    strMessage = "ERROR: Unable to find group " 
    DisplayCustomError(strMessage)
    WScript.Quit
End If
 
If strUserLoc=mich Then 
Set strParentDir="\\michigan\home$\"
'If strUserLoc=nynj Then
'Set strParentDir="\\newyork\home$\"
'If strUserLoc=lisle Then
'Set strParentDir="\\illinois\home$\"
 
'Check to see if the user has a home drive and if not, create it .
Dim objFSO, objNetwork
 
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objNetwork=CreateObject("Wscript.Network")
 
strUser=objNetwork.UserName
 
objNetwork.RemoveNetworkDrive "H:", True, True
 
If Not fso.FolderExists(strParentDir & strUser) Then
Set objFSO = fso.CreateFolder(strParentDir & strUser)
Else objNetwork.MapNetworkDrive "H:", & strParentDir & strUser & ""
End If

Open in new window

SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
SOLUTION
Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
Avatar of slayer199

ASKER

Good point.   I already have a script outside the logon script that assigns permissions and ownership to all folders on the home dirve...it's been working for some time.  I thought there was a way I could do this at logon without causing a security issue.  I'll have to look that over a bit.

In any case, if I remove the 2nd Dim objNetwork I then get a syntax error in line 41.
ASKER CERTIFIED SOLUTION
Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access

I would actually change the script slightly. I wanted to post this modification so you could take a look through.

It makes it a tiny bit longer, but only because of the white-space I've added, collapse that and it's a bit more straight-forward.

We've lost one of the "If" statement groups because it seemed to be checking the same value twice (in effect).

Chris

On Error Resume Next
 
Dim objShell, objNetwork, objFSO, objFolder
 
Set objShell = CreateObject("Wscript.Shell")
Set objNetwork = CreateObject("Wscript.Network")
 
' Moved to reduce calls to Username property
strUser = objNetwork.UserName
 
If IsAMemberOf(objNetwork.UserDomain, strUser, "mich") Then 
	strUserLoc = mich ' Set is not required if this is just a string
 
	strParentDir = "\\michigan\home$\"
 
'ElseIf IsAMemberOf(objNetwork.UserDomain, strUser, "nynj") Then 
	' strUserLoc = nynj
 
	' strParentDir="\\newyork\home$\"
 
'ElseIf IsAMemberOf(objNetwork.UserDomain, strUser, "illinois") Then 
	' strUserLoc = Illinois
 
	' strParentDir="\\illinois\home$\"
Else
	strMessage = "ERROR: Unable to find group " 
 
	' Parentheses not required when calling a Sub
	DisplayCustomError strMessage
	WScript.Quit
End If
 
'Check to see if the user has a home drive and if not, create it .
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
objNetwork.RemoveNetworkDrive "H:", True, True
 
If Not objFSO.FolderExists(strParentDir & strUser) Then
	Set objFolder = objFSO.CreateFolder(strParentDir & strUser)
	Set objFolder = Nothing
Else
	objNetwork.MapNetworkDrive "H:", & strParentDir & strUser & ""
End If
 
Set objFSO = Nothing
Set objNetwork = Nothing

Open in new window

Excellent, thank you! Now I just have to work in the error-handling.
Do you have any suggestions for error-handling?  

You'll need Error handling for the drive mapping operation, On Error Goto 0 is used to turn that off again. Then you can pick up the error as an Err object, it has a number of properties including Number and Description.

You won't get the VbScript Line Number from that method of error handling, it's not something that's available to it.

Otherwise, the example below shows some basic error control on the RemoveNetworkDrive and MapNetworkDrive methods.

Chris


On Error Resume Next : Err.Clear
 
objNetwork.RemoveNetworkDrive "H:", True, True
If Err.Number <> 0 Then
    WScript.Echo "Failed to remove drive H: - " & Err.Description
End If
 
If Not objFSO.FolderExists(strParentDir & strUser) Then
        Set objFolder = objFSO.CreateFolder(strParentDir & strUser)
        Set objFolder = Nothing
Else
        Err.Clear
        objNetwork.MapNetworkDrive "H:", & strParentDir & strUser & ""
        If Err.Number <> 0 Then
            WScript.Echo "Failed to add drive H: - " & Err.Description
        End If
End If
 
On Error Goto 0 
 
Set objFSO = Nothing
Set objNetwork = Nothing

Open in new window

Great.  thanks again for all your help!