Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Login script question 1

Status
Not open for further replies.

OregonITFella

IS-IT--Management
Jun 4, 2004
17
0
0
US
Can someone look at this for me and tell me how to accomplish the following. I want the script I have pasted in here to map the users P drive to their home folder(I tried using %username% but that of course didn't work), but I have had nothing but errors trying it myself. Anyone feel like tweaking my script and showing me what I need to do to get this to work. I don't want a new script because this one works beautifully. I just need the P drive to point to their home folder (based on their login name).

Code:
'  This script will: 
'  (1) check if the drive is already connected and, if so, disconnect it.
'  (2) map the drive.
'
'  Arguments are as follows: 
'     MAPIT  DRIVE-LETTER as string,  PATH as string, USER as string, PASSWORD as string
'     (1) Do not specify colon in drive letter.
'     (2) Do not end path with a forward slash.
'     (3) If user and password are not required to establish map, then specify a zero-length string as follows:  ""

' Create the Shell or environment for the commands:
Set WshShell = WScript.CreateObject("WScript.Shell")
' Define objects:
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives()

' ====================================
' DEFINE WHO TO CONTACT for pop-up messages:
' ====================================
strContactMessage = "If you require assistance, please contact xxx-xxxx."

' ==================
' DEFINE DRIVES TO MAP:
' ==================
Mapit "M", "\\server 3\path", "", ""
Mapit "P", "\\server2\path\home", "", ""
Mapit "J", "\\server1\path\groups", "", ""

' ========
' CLEAN UP:
' ========
Set WshShell = Nothing
Set WshNetwork = Nothing
Set oDrives = Nothing

' ##################################
' DO NOT MODIFY ANYTHING BELOW THIS POINT...
'   unless you are familiar with the proper settings.
' ##################################
Sub Mapit(strLetter, strPath, strUser, strPass)

    ' Define the DriveLetter:
    DriveLetter = strLetter & ":"

    ' Define the remote path:
    RemotePath = strPath

'TEST SECTION

Dim objNet, strUserName
Set objNet = CreateObject("Wscript.Network")
strUserName =objNet.UserName 


    ' Pop-up Notices (set to False to disable notices, otherwise set to True):
    bPopReminder = True

    ' Define known errors to trap:
    Dim arrErrCode(1)
    Dim arrErrDesc(1)
    arrErrCode(0) = -2147023694
    arrErrCode(1) = -2147024811
    arrErrDesc(0) = "Unable to map drive " & DriveLetter & " to " & RemotePath _
        & " due to a previously defined remembered map with the same letter." _
        & vbCrLf & vbCrLf & "Please MANUALLY disconnect map drive " & DriveLetter _
        & ", then Log Out and Log back in."
    arrErrDesc(1) = "Unable to map drive " & DriveLetter & " to " & RemotePath _
        & " since " & DriveLetter & ": was previously reserved by your computer." _
        & vbCrLf & vbCrLf & "(Refer to Management, Shared Folders, Shares)"

    ' Define whether the map information should be removed from the current user's profile:
    bForceRemoveFromProfile = True
    bRemoveFromProfile = True

    ' Define whether the map information should be stored in the current user's profile:
    bStoreInProfile = False

    ' Check if already connected:
    AlreadyConnected = False
    For i = 0 To oDrives.Count - 1 Step 2
        If LCase(oDrives.Item(i)) = LCase(DriveLetter) Then AlreadyConnected = True
    Next

    ' Attempt to map the drive.  If already mapped, first attempt disconnect:
    If AlreadyConnected = True then
        WshNetwork.RemoveNetworkDrive DriveLetter, bForceRemoveFromProfile, bRemoveFromProfile
        If Not strUser = "" Then
            WshNetwork.MapNetworkDrive DriveLetter, RemotePath, bStoreInProfile, strUser, strPass
        Else
            WshNetwork.MapNetworkDrive DriveLetter, RemotePath, bStoreInProfile
        End If
        If bPopReminder Then WshShell.PopUp "Drive " & DriveLetter & " disconnected, then connected successfully to " & RemotePath
    Else
        On Error Resume Next
        If Not strUser = "" Then
            WshNetwork.MapNetworkDrive DriveLetter, RemotePath, bStoreInProfile, strUser, strPass
        Else
            WshNetwork.MapNetworkDrive DriveLetter, RemotePath, bStoreInProfile
        End If
        If Err.Number <> 0 Then
            bKnownError = False
            For I = LBound(arrErrCode) To UBound(arrErrCode)
                If Err.Number = arrErrCode(I) Then
                    bKnownError = True
                    strPopMessage = arrErrDesc(I)
                    ' Display the Disconnect Network Drives window:
                    If Err.Number = arrErrCode(0) Then
                        Set objWSH = Wscript.CreateObject("WScript.Shell")
                        objWSH.Run "rundll32.exe shell32.dll,SHHelpShortcuts_RunDLL Disconnect", 1, true
                    End If
                    Exit For
                End If
            Next
            If Not bKnownError Then
                strPopMessage = "Unable to map drive " & DriveLetter & " to " & RemotePath _
                    & " due to reason stated below."
            End If
            ' Display warning message:
            strPopMessage = "WARNING!!   WARNING!!   WARNING!!   WARNING!!" _
                & vbCrLf & vbCrLf & strPopMessage _
                & vbCrLf & vbCrLf & Err.Description & " (error " & Err.Number & ")" _
                & vbCrLf & vbCrLf & strContactMessage
            WshShell.PopUp strPopMessage
        Else
            If bPopReminder Then WshShell.PopUp "Drive " & DriveLetter & " connected successfully to " & RemotePath
        End If
    End If

    ' Release resources:
    Set objWSH = Nothing

    ' Slight pause to ensure each pass has time to commit:
    wscript.sleep 200
End Sub
 
I made one Faux Pas. The section that says

'TEST SECTION

Dim objNet, strUserName
Set objNet = CreateObject("Wscript.Network")
strUserName =objNet.UserName



is in here incorrectly. I am not using that. I pasted the wrong version.
 
I have had nothing but errors trying it myself
Which errors trying what ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV,

The code below is what I am actually using and it works quite well. The above posted code had some extraneous code that would break it. I assure you this portion works because I have it up and running on WinXP and Server 2003 as we speak. The little tweak I need is to map that P drive to their \\server\home\%username% but I can't figure it out.

Code:
'  This script will: 
'  (1) check if the drive is already connected and, if so, disconnect it.
'  (2) map the drive.
'
'  Arguments are as follows: 
'     MAPIT  DRIVE-LETTER as string,  PATH as string, USER as string, PASSWORD as string
'     (1) Do not specify colon in drive letter.
'     (2) Do not end path with a forward slash.
'     (3) If user and password are not required to establish map, then specify a zero-length string as follows:  ""

' Create the Shell or environment for the commands:
Set WshShell = WScript.CreateObject("WScript.Shell")
' Define objects:
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives()

' ====================================
' DEFINE WHO TO CONTACT for pop-up messages:
' ====================================
strContactMessage = "If you require assistance, please contact xxx-xxxx."

' ==================
' DEFINE DRIVES TO MAP:
' ==================
Mapit "M", "\\server 3\path", "", ""
Mapit "P", "\\server2\path\home", "", ""
Mapit "J", "\\server1\path\groups", "", ""

' ========
' CLEAN UP:
' ========
Set WshShell = Nothing
Set WshNetwork = Nothing
Set oDrives = Nothing

' ##################################
' DO NOT MODIFY ANYTHING BELOW THIS POINT...
'   unless you are familiar with the proper settings.
' ##################################
Sub Mapit(strLetter, strPath, strUser, strPass)

    ' Define the DriveLetter:
    DriveLetter = strLetter & ":"

    ' Define the remote path:
    RemotePath = strPath


    ' Pop-up Notices (set to False to disable notices, otherwise set to True):
    bPopReminder = True

    ' Define known errors to trap:
    Dim arrErrCode(1)
    Dim arrErrDesc(1)
    arrErrCode(0) = -2147023694
    arrErrCode(1) = -2147024811
    arrErrDesc(0) = "Unable to map drive " & DriveLetter & " to " & RemotePath _
        & " due to a previously defined remembered map with the same letter." _
        & vbCrLf & vbCrLf & "Please MANUALLY disconnect map drive " & DriveLetter _
        & ", then Log Out and Log back in."
    arrErrDesc(1) = "Unable to map drive " & DriveLetter & " to " & RemotePath _
        & " since " & DriveLetter & ": was previously reserved by your computer." _
        & vbCrLf & vbCrLf & "(Refer to Management, Shared Folders, Shares)"

    ' Define whether the map information should be removed from the current user's profile:
    bForceRemoveFromProfile = True
    bRemoveFromProfile = True

    ' Define whether the map information should be stored in the current user's profile:
    bStoreInProfile = False

    ' Check if already connected:
    AlreadyConnected = False
    For i = 0 To oDrives.Count - 1 Step 2
        If LCase(oDrives.Item(i)) = LCase(DriveLetter) Then AlreadyConnected = True
    Next

    ' Attempt to map the drive.  If already mapped, first attempt disconnect:
    If AlreadyConnected = True then
        WshNetwork.RemoveNetworkDrive DriveLetter, bForceRemoveFromProfile, bRemoveFromProfile
        If Not strUser = "" Then
            WshNetwork.MapNetworkDrive DriveLetter, RemotePath, bStoreInProfile, strUser, strPass
        Else
            WshNetwork.MapNetworkDrive DriveLetter, RemotePath, bStoreInProfile
        End If
        If bPopReminder Then WshShell.PopUp "Drive " & DriveLetter & " disconnected, then connected successfully to " & RemotePath
    Else
        On Error Resume Next
        If Not strUser = "" Then
            WshNetwork.MapNetworkDrive DriveLetter, RemotePath, bStoreInProfile, strUser, strPass
        Else
            WshNetwork.MapNetworkDrive DriveLetter, RemotePath, bStoreInProfile
        End If
        If Err.Number <> 0 Then
            bKnownError = False
            For I = LBound(arrErrCode) To UBound(arrErrCode)
                If Err.Number = arrErrCode(I) Then
                    bKnownError = True
                    strPopMessage = arrErrDesc(I)
                    ' Display the Disconnect Network Drives window:
                    If Err.Number = arrErrCode(0) Then
                        Set objWSH = Wscript.CreateObject("WScript.Shell")
                        objWSH.Run "rundll32.exe shell32.dll,SHHelpShortcuts_RunDLL Disconnect", 1, true
                    End If
                    Exit For
                End If
            Next
            If Not bKnownError Then
                strPopMessage = "Unable to map drive " & DriveLetter & " to " & RemotePath _
                    & " due to reason stated below."
            End If
            ' Display warning message:
            strPopMessage = "WARNING!!   WARNING!!   WARNING!!   WARNING!!" _
                & vbCrLf & vbCrLf & strPopMessage _
                & vbCrLf & vbCrLf & Err.Description & " (error " & Err.Number & ")" _
                & vbCrLf & vbCrLf & strContactMessage
            WshShell.PopUp strPopMessage
        Else
            If bPopReminder Then WshShell.PopUp "Drive " & DriveLetter & " connected successfully to " & RemotePath
        End If
    End If

    ' Release resources:
    Set objWSH = Nothing

    ' Slight pause to ensure each pass has time to commit:
    wscript.sleep 200
End Sub
 
Something like this ?
Mapit "P", "\\server\home\" & WshNetwork.UserName, "", ""

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Perfect. It always has to be something simple! You get my vote for a knowlegable programmer. Thank you very much.
 
I have one more issue I am hoping to get help with. If I want to map the J drive to a folder based on the logged in users groups they belong to. I.E. If the user belongs to group B, map the J drive to \\server\groups\B

Is this something that could be easily accomplished. I would love to figure this out myself. Where would I start?

-Thanks in advance
 
I have the following code. Could this be incorporated easily?

Code:
For Each GroupObj In UserObj.Groups
    Select Case GroupObj.Name
    'Check for group memberships and take needed action
        Case "B"
            WSHNetwork.MapNetworkDrive "j:", "\\server\groups\B",True
       
    End Select
 
I'm sorry to bring this post backup to the front after I had got the info I needed, but this last issue is really perplexing me. I am just too new at VBS and WSH to know how to accomplish this. PHV, you seem to be THE expert. Any chance you can throw me a bone on this one. I want to learn VBS in a bad way, but in the mean time I need this solution for a job related issue. Can you or anyone else please take a look at my last problem in this thread and point me in the right direction to accomplish this without much change to my current script? I would be very grateful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top