Link to home
Start Free TrialLog in
Avatar of -Matthew-
-Matthew-

asked on

VB Login Script

I need to convert this over to vbscript and fix a few things on the way. The main one, I need to stop the errors that come up when a drive is already mapped. Would it better to check for the drives existence or remove it then replace it?

I'm using this as an add on file that runs when connected to the vpn after connect.
@echo off
 
net use h: \\tsc72\%username%$
net use t: \\tsc73\collab$
 
ifmember "TSC\AEStaff"
if not %errorlevel% EQU 1 goto Endaestaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\baehare$
:Endaestaff
 
ifmember "TSC\BCStaff"
if not %errorlevel% EQU 1 goto Endbcstaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\bcshare$
:Endbcstaff
 
ifmember "TSC\BEStaff"
if not %errorlevel% EQU 1 goto Endbestaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\beshare$
:Endbestaff
 
ifmember "TSC\BMStaff"
if not %errorlevel% EQU 1 goto Endbmstaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\bmshare$
:Endbmstaff
 
ifmember "TSC\CEStaff"
if not %errorlevel% EQU 1 goto Endcestaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\ceshare$
:Endcestaff
 
ifmember "TSC\COStaff"
if not %errorlevel% EQU 1 goto Endcostaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\coshare$
:Endcostaff
 
ifmember "TSC\ITStaff"
if not %errorlevel% EQU 1 goto Enditstaff
echo Now mapping Z for the Domain Admins group..
net use n: \\tsc73\itshare$
net use k: \\tsc73\itadmin$
:Enditstaff
 
ifmember "TSC\DEStaff"
if not %errorlevel% EQU 1 goto Enddestaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\deshare$
:Enddestaff
 
ifmember "TSC\ETStaff"
if not %errorlevel% EQU 1 goto Endetstaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\etshare$
:Endetstaff
 
ifmember "TSC\HEStaff"
if not %errorlevel% EQU 1 goto Endhestaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\heshare$
:Endhestaff
 
ifmember "TSC\HHStaff"
if not %errorlevel% EQU 1 goto Endhhstaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\hhshare$
:Endhhstaff
 
ifmember "TSC\KEStaff"
if not %errorlevel% EQU 1 goto Endkestaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\keshare$
:Endkestaff
 
ifmember "TSC\KMStaff"
if not %errorlevel% EQU 1 goto Endkmstaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\kmshare$
:Endkmstaff
 
ifmember "TSC\MCStaff"
if not %errorlevel% EQU 1 goto Endmcstaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\mcshare$
:Endmcstaff
 
ifmember "TSC\MEStaff"
if not %errorlevel% EQU 1 goto Endmestaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\meshare$
:Endmestaff
 
ifmember "TSC\MMStaff"
if not %errorlevel% EQU 1 goto Endmmstaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\mmshare$
:Endmmstaff
 
ifmember "TSC\OCStaff"
if not %errorlevel% EQU 1 goto Endocstaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\ocshare$
:Endocstaff
 
ifmember "TSC\SWStaff"
if not %errorlevel% EQU 1 goto Endswstaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\swshare$
:Endswstaff
 
ifmember "TSC\TransStaff"
if not %errorlevel% EQU 1 goto Endtransstaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\transportation$
:Endtransstaff
 
ifmember "TSC\WEStaff"
if not %errorlevel% EQU 1 goto Endwestaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\weshare$
:Endwestaff
 
ifmember "TSC\WMStaff"
if not %errorlevel% EQU 1 goto Endwmstaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\wmshare$
:Endwmstaff
 
ifmember "TSC\WRStaff"
if not %errorlevel% EQU 1 goto Endwrstaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\wrshare$
:Endwrstaff
 
ifmember "TSC\WWStaff"
if not %errorlevel% EQU 1 goto Endwwstaff
echo Now mapping Z for the Domain Admins group..
net use s: \\tsc73\wwshare$
:Endwwstaff

Open in new window

Avatar of spinzr0
spinzr0
Flag of United States of America image

Please let me know if the attached works for you
Set oWShell = CreateObject("WScript.Shell")
Set oNetwork = CreateObject( "WScript.Network")
Set dMappedDrives = CreateObject("Scripting.Dictionary")
 
bForceDisconnect = True        ' True/False - Force disconnect of existing drive mappings before mapping new drives
bPersistentMappings = True      ' True/False - Drive mappings will remain at next logon, even if not connected to network
 
dMappedDrives.CompareMode = vbTextCompare
Call BuildMappedDrivesDictionary()
 
Call MapDrive("X:", "\\Server\Share")
 
Set dMappedDrives = Nothing
Set oNetwork = Nothing
Set oWShell = Nothing
 
 
'******************************************************************************
' MapDrive(sDrive, sNewShare)
'
' sDrive = Drive letter in the form "x:"
' sNewShare = Network share plus optional path in form "\\server\share[\path]"
'
' Maps a network share to a drive if the drive letter is not already taken.
' If the drive letter is already taken, and variable bForceDisconnect (at front 
' of script) has been set to "True", attempts to disconnect drive before
' before mapping new connection.  If the Path is already mapped to a different
' drive letter it will remove that mapping if bForceDisconnect (at front 
' of script) has been set to "True".
'******************************************************************************
Sub MapDrive(sDrive, sNewShare)
    On Error Resume Next
    '
    ' Check to see if the path is already mapped to the requested location
    '
    If dMappedDrives.Exists(sNewShare) Then
         If dMappedDrives.Item(sNewShare) = UCase(sDrive) Then
             Exit Sub
         Else
             If bForceDisconnect = True Then
                 oNetwork.RemoveNetworkDrive dMappedDrives.Item(sNewShare), True, True
                 dMappedDrives.Remove(dMappedDrives.Item(sNewShare))
                 dMappedDrives.Remove(sNewShare)
             End If
             Err.Clear
         End If
    End If
    '
    ' Loop through drives to check if there is an existing mapping
    '
    If dMappedDrives.Exists(UCase(sDrive)) Then
        If UCase(dMappedDrives.Item(sDrive)) = UCase(sNewShare) Then
            Exit Sub
        Else
            If bForceDisconnect = True Then
                Err.Clear
                oNetwork.RemoveNetworkDrive sDrive, True, True
                If Err.Number <> 0 Then Exit Sub
                dMappedDrives.Remove(dMappedDrives.Item(sDrive))
                dMappedDrives.Remove(sDrive)
            Else
                Exit Sub
            End If
        End If
    End If
    '
    ' Now the drive letter is free, so map the drive
    '
    Err.Clear
    oNetwork.MapNetworkDrive UCase(sDrive), sNewShare, bPersistentMappings
End Sub
 
'******************************************************************************
' BuildMappedDrivesDictionary
'
' Builds the dictionary of existing drive mappings
'******************************************************************************
Sub BuildMappedDrivesDictionary()
    On Error Resume Next
    Const HKEY_CURRENT_USER = &H80000001
    Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
 
    oRegistry.EnumKey HKEY_CURRENT_USER, "Network\", aSubKeys
 
    For Each oSubKey In aSubKeys
        sTempPath = oWShell.RegRead("HKCU\Network\" & oSubKey & "\RemotePath")
        If Not dMappedDrives.Exists(sTempPath) Then dMappedDrives.Add sTempPath, UCase(oSubKey & ":")
        .Add UCase(oSubKey & ":"), sTempPath
    Next
    Set oRegistry = Nothing
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of spinzr0
spinzr0
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
did this work for you?  Please let me know if you need anything else.