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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Amatures Borrow Professionals Steal

Status
Not open for further replies.

gmagerr

Technical User
Aug 11, 2001
323
US
Don't take offense to my heading I was just playing. Anyway I have gathered some scripts from around (maybe all from here I can't remember) I've left the authors names in the script to give credit where credit is due. Mark provided the bulk of this script. I added other things as I found them to try to make a logon script that maps printers, drives and does a couple of other cool things. Problem is when i click it while it sits on my desktop it works fine (I'm in the domain admins group) When i put it as a logon script through AD and logon as testuser (in LA_Test Group) i get a message saying undefined error quitting. What does that mean? Did i just set the DC on fire? anyway here's my monstrosity. Hopefully someone can help me get this working. thanks in advance guys and again I was only having fun with the post title.

'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 4.0
'
' NAME: LogonScript.vbs
'
' AUTHOR: Mark D. MacLachlan, The Spider's Parlor
' URL : ' DATE : 4/10/2003
'
' COMMENT: Enumerates current users' group memberships in given domain.
' Maps and disconnects drives and printers
'
'==========================================================================
'Option Explicit

Dim WSHShell, WSHNetwork, objDomain, DomainString, GroupObj
Dim UserString, UserObj, Path, strComputer, oDrives

' 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()


'==========================================================================
'Automatically find the domain name
'==========================================================================
Set objDomain = getObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")

'==========================================================================
'Grab the user name
'==========================================================================
UserString = WSHNetwork.UserName

'==========================================================================
'Bind to the user object to get user name and check for group memberships later
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)
'==========================================================================

'==========================================================================
'Grab the computer name for use in add-on code later
strComputer = WSHNetwork.ComputerName
'==========================================================================

'==========================================================================
' Map drives needed by all
'==========================================================================
Mapit "U", "\\netapp1\users", "", ""

'==========================================================================
'Now check for group memberships and map appropriate drives and printers
'==========================================================================
For Each GroupObj In UserObj.Groups
Select Case GroupObj.Name

Case "Domain Admins"
Mapit "M", "\\misadmin\root", "", ""
Mapit "P", "\\mgashare1\Product Development", "", ""

Call AddPrinter ("mgae", "rscprint", "misprint")
call AddPrinter ("mgae", "", "test")

Case "LA_Test"
Mapit "P", "\\mgashare1\Product Development", "", ""
Call AddPrinter ("mgae", "rscprint", "pdclr")
End Select
Next

'Add On Code goes below this line
'==========================================================================

'==========================================================================
'Configure the PC to show the Windows Version and Service Pack
'as an overlay to the desktop above the System Tray
'==========================================================================
HKEY_CURRENT_USER = &H80000001
strComputer = WSHNetwork.Computername
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Control Panel\Desktop"
objReg.CreateKey HKEY_CURRENT_USER, strKeyPath
ValueName = "PaintDesktopVersion"
dwValue = 1
objReg.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, ValueName, dwValue

'==========================================================================
'Change My Computer Icon to the name of the computer
'==========================================================================
Const MY_COMPUTER = &H11&

Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_COMPUTER)
Set objFolderItem = objFolder.Self
objFolderItem.Name = strComputer

'==========================================================================
'Change My Computer Icon from the name of the computer
'back to the default My Computer
'==========================================================================
'Const MY_COMPUTER = &H11&

'Set objShell = CreateObject("Shell.Application")
'Set objFolder = objShell.Namespace(MY_COMPUTER)
'Set objFolderItem = objFolder.Self
'objFolderItem.Name = "My Computer"

'==========================================================================
'
' Sub: AddPrinter
'
' Purpose: Connect to shared network printer
'
' Input:
' strPrtServerDomain Domain in which print server is a member
' strPrtServer Name of print server
' strPrtShare Share name of printer
'
' Output:
'
' Usage:
' Call AddPrinter ("Mydomain2", "MyPrtSvr2", "Bld1Rm101-HP4050")
'
'==========================================================================
Private Sub AddPrinter(strPrtServerDomain, strPrtServer, strPrtShare)

On Error Resume Next

Dim strPrtPath 'Full path to printer share
Dim objPrinter 'Object reference to printer
Dim strMsg 'Message output to user
Dim blnError 'True / False error condition

blnError = False

'==========================================================================
'Build path to printer share
strPrtPath = "\\" & strPrtServer & "\" & strPrtShare

'==========================================================================
'Test to see if shared printer exists.
'Proceed if yes, set error condition msg if no.
'==========================================================================
Set objPrinter = GetObject("WinNT://" & strPrtServerDomain & "/" & strPrtServer & "/" & strPrtShare)
If IsObject( objPrinter ) AND (objPrinter.Name <> "" AND objPrinter.Class = "PrintQueue") Then

'==========================================================================
'Different mapping techniques depending on OS version
'==========================================================================
If WSHShell.ExpandEnvironmentStrings( "%OS%" ) = "Windows_NT" Then
Err.Clear
'==========================================================================
'Map printer
'==========================================================================
WSHNetwork.AddWindowsPrinterConnection strPrtPath
Else
'==========================================================================
'Mapping printers for Win9x & ME is a pain and unreliable.
'==========================================================================
End If

Else
blnError = True
End If

'==========================================================================
'Check error condition and output appropriate user message
'I modified this code to not show a Print Server in the
'error message if on did not exsist.
'==========================================================================

If strPrtServer <> "" Then
If Err <> 0 OR blnError = True Then
strMsg = "Unable to connect to network printer. " & vbCrLf & _
"Please contact MIS and ask them to check the " & vbCrLf & _
"" & strPrtServer & " server." & _
vbCrLf & "================================" & vbCrLf & _
"Let them know that this message box appeared" & vbCrLf &_
"and you are unable to connect to the '" _
& strPrtShare & "' printer" & vbCrLf &_
"Thank You "
WScript.Echo strMsg
Else
WScript.Echo ("Successfully added printer connection to " & strPrtPath)
End If
Else
If Err <> 0 OR blnError = True Then
strMsg = "Unable to connect to network printer. " & vbCrLf & _
"Please contact MIS." & vbCrLf & _
vbCrLf & "================================" & vbCrLf & _
"Let them know that this message box appeared" & vbCrLf &_
"and you are unable to connect to the '" _
& strPrtShare & "' printer" & vbCrLf &_
"Thank You "
WScript.Echo strMsg
Else
WScript.Echo ("Successfully added printer connection to " & strPrtPath)
End If
End If
Set objPrinter = Nothing

End Sub


'==========================================================================
' Written in VBScript.
' Paul DeBrino .:. .:. March 2004.
' Establishes map drives.
' Assign to OU Group Policy under USER CONFIG, WINDOWS SETTINGS, SCRIPTS, LOGON SCRIPT.
'
' 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: ""
'
' Reference Microsoft info at:
' '==========================================================================

'==========================================================================
' Create the Shell or environment for the commands:
'==========================================================================
'Set oDrives = WSHNetwork.EnumNetworkDrives()

'==========================================================================
' DEFINE WHO TO CONTACT for pop-up messages:
'==========================================================================
strContactMessage = "If you require assistance, please send an email to MIS Support."

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

'==========================================================================
' Define the DriveLetter:
Dim DriveLetter, RemotePath, bPopReminder, bForceRemoveFromProfile
Dim bRemoveFromProfile, bStoreInProfile, AlreadyConnected, i, objWSH


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 _

End If

'==========================================================================
' Display warning message:
'==========================================================================
strPopMessage = "WARNING!! WARNING!! WARNING!! WARNING!!" _
& VbCrLf &"================================"_
& vbCrLf & VbCrLf & strPopMessage & 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

'==========================================================================
'Add On Code goes above this line
'==========================================================================

'==========================================================================
'Clean Up Memory We Used
'==========================================================================
Set oDrives = Nothing
Set UserObj = Nothing
set GroupObj = Nothing
set WSHNetwork = Nothing
set DomainString = Nothing
set WSHSHell = Nothing
Set WSHPrinters = Nothing

'==========================================================================
'Quit the Script
'==========================================================================
wscript.quit
 
I do not have an answer to your question but have to make the comment: that is one HUGE script! wowsa! :p

A+, Network+, MCP
========================================>
My first computer was the Atari 400 (heh)
 
Couple of ideas,
What 'rights' do lowly users have on the desktop? You are writing to the registry. Can your users do that?
Your Mapit message to cal the system admin is outside the subroutine (probably more housekeeping than anything else.)
You mention you can run it from the desktop as an admin. Does it run from the network share as admin? Who has hat rights to it on the network?

 
Thanks for the replies. I haven't tried to run it from the desktop as the test user. I'll do that. Also i didn't try it as a logon script for admin I'll try that as well. Users at my work get admin rights for their local accounts to their computers. One thing I didn't mention, When i uncomment on error resume next in the addprinter sub I get an error message on the line that says Set objPrinter, that objPrinter needs an object. Something like that i can get the exact message if you need. Thanks again.
 
That would be a good indication of your problem :).

Here is a snipit of your code...
Code:
Set objPrinter = GetObject("WinNT://" & strPrtServerDomain & "/" & strPrtServer & "/" & strPrtShare)

Do a search for your variables "strPrtServerDomain", "strPrtServer", and "strPrtShare". Are you assigning them any values? I searched on the first one and couldn't find where a value was being assigned to them. If you aren't, your script is trying to create a printer object that from this string...

"WinNT:////"

... which I doubt is valid :).

Assign values to these variables, and you should get further in your script.

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
Curious if you got this all resolved.

Seems to me you go through a lot of work on the drive mappings just to be able to display a message saying it was successful or not.

Personally I would not display anything on success, it simply delays the login process. I would also suggest that in place of the sub you could simply turn error checking on an off and display a message if there is an error.

Something like this:
Code:
On Error Resume Next
WSHNetwork.MapNetworkDrive "U:", "\\server\users",True
If err then
	msgbox "Error Encountered Mapping Drive"
else
	'Do Nothing It Worked As Expected
End if
err.clear

I hope you find this post helpful.

Regards,

Mark
 
Thanks for the replies. Yes I did get it working. It's turning into a monster. We have a sharepoint portal that uses https. I added parts to make the portal be in the trusted sites in IE so the users don't need to re-login once they are in, Then it'll make their department team site their homepage, and add a link in IE favorites as well. Added a part based on what group your in, it will map your department folder and put a shortcut on the desktop to that folder. Stuff like that. will i ever use all of this? i don't know. It's great practice for me I've learned a ton with this logon script. I want to have it interact with a hta and display progress while it doing all the things it's doing. Any thoughts on that? Thanks again Mark the original script was very inspiring. I'll post what I have if you guys are interested. Thanks.
 
Glad you got it working. Some feedback.
You should set the homepage using GPO rather than script.

For the progress bar, check out thread329-898604. I got 5 stars for that solution, so I think you will like it.

I would just caution you to do your best to minimize the end user experience during log in. It is great to have all this stuff configured for the user, but don't let the automation get in the way of the user being able to sit down and start working. Your users don't understand what a mapping is. They don't care. They just expect to find their files. If your script is doing persistent mapping, there is little cause to display a notification if it fails because if the script does not execute, the mapping from the previous login should still be in affect.

If all you are doing is practicing, then I applaud your efforts. Scripting is VERY exciting stuff in my opinion and that is why I like to share what I have done.

I would like to see your end result. I'm particularly interested in your solution for adding the site to the trusted sites list.



I hope you find this post helpful.

Regards,

Mark
 
Thanks again Mark. Here's what i have. Most of what I was talking about is in the Case Domain Admins portion. I do have a question once you have a look at the script. What if someone is in multiple security groups? what happens then in the case statements? Also I am just doing this to learn. When i put this into the production environment it will not need any input from the users. Mostly just the error boxes will pop up if something doesn't map. I'm doing this because we add and remove printers all of the time. I noticed some of the users were getting messages (regarding the printer not mapping) but it was saying something about the script and the sysvol. I thought something instructing them to contact mis would be better. Anyway here's what i have so far.

'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 4.0
'
' NAME: LogonScript.vbs
'
' AUTHOR: Mark D. MacLachlan, The Spider's Parlor
' URL : ' DATE : 4/10/2003
'
' COMMENT: Enumerates current users' group memberships in given domain.
' Maps and disconnects drives and printers
'
' AUTHOR: Gene Magerr, Magerr Media
' DATE : 11/25/2005
'
' COMMENT: Modifications on a great script
'
'==========================================================================
'Option Explicit

'==========================================================================
' If testmode is set to true, a message will pop up telling the user
' the shortcuts to their desktops are being created
' this is a part of the addshortcuttodesktop sub
'==========================================================================
TestMode = True

'==========================================================================
' 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()

'==========================================================================
'Automatically find the domain name
'==========================================================================
Set objDomain = getObject("LDAP://rootDse")
strDomain = objDomain.Get("dnsHostName")

'==========================================================================
'Grab the user name
'==========================================================================
UserString = WshNetwork.UserName

'==========================================================================
'Bind to the user object to get user name and check for group memberships later
'==========================================================================
Set objUser = GetObject("WinNT://" & strDomain & "/" & UserString)

'==========================================================================
'Grab the computer name for use in add-on code later
'==========================================================================
strComputer = WshNetwork.ComputerName

'==========================================================================
'Set Trusted Sites for MGA Portal
'==========================================================================
Const HKEY_CURRENT_USER = &H80000001

Set objReg=GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\" _
& "ZoneMap\Domains\mgae.com"

objReg.CreateKey HKEY_CURRENT_USER, strKeyPath

strValueName = "https"
dwValue = 2

objReg.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue

'==========================================================================
' Map drives needed by all
'==========================================================================
Mapit "U", "\\netapp1\users", "", ""

'==========================================================================
'Now check for group memberships and map appropriate drives and printers
'==========================================================================
For Each objGroup In objUser.Groups
Select Case objGroup.Name

Case "Domain Admins"
Mapit "M", "\\misadmin\root", "", ""
Mapit "N", "\\netapp1\MIS Archive", "", ""

Call DesktopSC("LA_MIS", "\\netapp1\users\LA_MIS", "\\netapp1\users")
Call DesktopSC("U-Drive", "\\netapp1\users", "")

Call AddPrinter ("mgae", "rscprint", "misprint")
Call AddPrinter ("mgae", "", "test")
Call AddPrinter ("mgae", "rscprint", "sam")

Call FavoritesSC ("-= LA MIS Team Site =-", "
WshNetwork.SetDefaultPrinter "\\rscprint\misprint"
strHomePage = "
Case "LA_Test"
Mapit "P", "\\mgashare1\Product Development", "", ""
Call AddPrinter ("mgae", "rscprint", "pdclr")

Case "LA_Accounting"
Mapit "X", "\\rscprint\acctshared", "", ""

Call AddPrinter ("mgae", "rscprint", "HP5si")

Case "LA_Consumer Research"
Call AddPrinter ("mgae", "rscprint", "hp8000")
Call AddPrinter ("mgae", "rscprint", "sale4050")
Call AddPrinter ("mgae", "rscprint", "salesclr")
Call AddPrinter ("mgae", "rscprint", "saleclr1")
Call AddPrinter ("mgae", "rscprint", "slssharp")

Case "LA_Credit"
Mapit "X", "\\rscprint\acctshared", "", ""

Call AddPrinter ("mgae", "rscprint", "credit")
Call AddPrinter ("mgae", "rscprint", "hp8000")
Call AddPrinter ("mgae", "rscprint", "ricoh1045")
Call AddPrinter ("mgae", "rscprint", "saleclr1")
Call AddPrinter ("mgae", "rscprint", "sale4050")

Case "LA_CS_Users"
Mapit "L", "\\Licensing\Root", "", ""

Call AddPrinter ("mgae", "rscprint", "cus_print")
Call AddPrinter ("mgae", "", "CF3102\print")


End Select
Next

'==========================================================================
'Set default homepage
'==========================================================================
If strHomePage <> "" Then
Err.Clear
WshShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\Start Page", strHomePage
End If

'==========================================================================
'Configure the PC to show the Windows Version and Service Pack
'as an overlay to the desktop above the System Tray
'==========================================================================
'HKEY_CURRENT_USER = &H80000001
'strComputer = WshNetwork.Computername

Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Control Panel\Desktop"
objReg.CreateKey HKEY_CURRENT_USER, strKeyPath
ValueName = "PaintDesktopVersion"
dwValue = 1
objReg.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, ValueName, dwValue

'==========================================================================
'Change My Computer Icon to the name of the computer
'==========================================================================
Const MY_COMPUTER = &H11&

Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_COMPUTER)
Set objFolderItem = objFolder.Self
objFolderItem.Name = strComputer

'==========================================================================
'Change My Computer Icon from the name of the computer
'back to the default My Computer
'==========================================================================
'Const MY_COMPUTER = &H11&

'Set objShell = CreateObject("Shell.Application")
'Set objFolder = objShell.Namespace(MY_COMPUTER)
'Set objFolderItem = objFolder.Self
'objFolderItem.Name = "My Computer"

'==========================================================================
'
' Sub: AddPrinter
'
' Purpose: Connect to shared network printer
'
' Input:
' strPrtServerDomain Domain in which print server is a member
' strPrtServer Name of print server
' strPrtShare Share name of printer
'
' Output:
'
' Usage:
' Call AddPrinter ("Mydomain2", "MyPrtSvr2", "Bld1Rm101-HP4050")
'
'==========================================================================
Private Sub AddPrinter(strPrtServerDomain, strPrtServer, strPrtShare)

On Error Resume Next

Dim strPrtPath 'Full path to printer share
Dim objPrinter 'Object reference to printer
Dim strMsg 'Message output to user
Dim blnError 'True / False error condition

blnError = False

'==========================================================================
'Build path to printer share
'==========================================================================
strPrtPath = "\\" & strPrtServer & "\" & strPrtShare

'==========================================================================
'Test to see if shared printer exists.
'Proceed if yes, set error condition msg if no.
'==========================================================================
Set objPrinter = GetObject("WinNT://" & strPrtServerDomain & "/" & strPrtServer & "/" & strPrtShare)
If IsObject( objPrinter ) AND (objPrinter.Name <> "" AND objPrinter.Class = "PrintQueue") Then

'==========================================================================
'Different mapping techniques depending on OS version
'==========================================================================
If WshShell.ExpandEnvironmentStrings( "%OS%" ) = "Windows_NT" Then
Err.Clear

'==========================================================================
'Map printer
'==========================================================================
WshNetwork.AddWindowsPrinterConnection strPrtPath
Else

'==========================================================================
'Mapping printers for Win9x & ME is a pain and unreliable.
'==========================================================================
End If

Else
blnError = True
End If

'==========================================================================
'Check error condition and output appropriate user message
'I modified this code to not show a Print Server in the
'error message if on did not exsist.
'==========================================================================
If strPrtServer <> "" Then
If Err <> 0 OR blnError = True Then
strMsg = "Unable to connect to network printer. " & vbCrLf & _
"Please contact MIS and ask them to check the " & vbCrLf & _
"" & strPrtServer & " server." & _
vbCrLf & "================================" & vbCrLf & _
"Let them know that this message box appeared" & vbCrLf &_
"and you are unable to connect to the '" _
& strPrtShare & "' printer" & vbCrLf &_
"Thank You "
WScript.Echo strMsg
Else
WScript.Echo ("Successfully added printer connection to " & strPrtPath)
End If
Else
If Err <> 0 OR blnError = True Then
strMsg = "Unable to connect to network printer. " & vbCrLf & _
"Please contact MIS." & vbCrLf & _
vbCrLf & "================================" & vbCrLf & _
"Let them know that this message box appeared" & vbCrLf &_
"and you are unable to connect to the '" _
& strPrtShare & "' printer" & vbCrLf &_
"Thank You "
WScript.Echo strMsg
Else
WScript.Echo ("Successfully added printer connection to " & strPrtPath)
End If
End If
Set objPrinter = Nothing

End Sub

'==========================================================================
' Written in VBScript.
' Paul DeBrino .:. .:. March 2004.
' Establishes map drives.
' Assign to OU Group Policy under USER CONFIG, WINDOWS SETTINGS, SCRIPTS, LOGON SCRIPT.
'
' 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: ""
'
' Reference Microsoft info at:
' '==========================================================================

'==========================================================================
' Create the Shell or environment for the commands:
'==========================================================================
'Set oDrives = WshNetwork.EnumNetworkDrives()

'==========================================================================
' DEFINE WHO TO CONTACT for pop-up messages:
'==========================================================================
strContactMessage = "If you require assistance, please send an email to MIS Support."

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

'==========================================================================
' Define the DriveLetter:
'==========================================================================
Dim DriveLetter, RemotePath, bPopReminder, bForceRemoveFromProfile
Dim bRemoveFromProfile, bStoreInProfile, AlreadyConnected, i, objWSH


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 = True 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 _

End If

'==========================================================================
' Display warning message:
'==========================================================================
strPopMessage = "WARNING!! WARNING!! WARNING!! WARNING!!" _
& VbCrLf &"================================"_
& vbCrLf & VbCrLf & strPopMessage & vbCrLf & VbCrLf & strContactMessage
WshShell.PopUp strPopMessage

Else
If bPopReminder = True 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

'==========================================================================
'Useage
'Call DesktopSC ("Test", "C:\Documents and Settings", "C:\Documents and Settings")
'==========================================================================
Sub DesktopSC (strName, strPath, strDir)

If TestMode = True Then
wscript.echo "=========== Desktop Shortcut ==========" & vbCrLf &_
"Adding Short Cut : " & strName & ".lnk To the desktop"
'wscript.echo "Target Path : " & tpath
'wscript.echo "Working Directory : " & tdir
End If

'Set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
Set oShellLink = WshShell.CreateShortcut(strDesktop & "\" & strName & ".lnk")
oShellLink.TargetPath = strPath
oShellLink.WindowStyle = 1
'oShellLink.Hotkey = "CTRL+SHIFT+F"
'oShellLink.IconLocation = "C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE, 0"
oShellLink.Description = strName & "Shortcut"
oShellLink.WorkingDirectory = strDir
oShellLink.Save

End Sub

'==========================================================================
'Useage
'Call FavoritesSC ("-= Name for shortcut =-", "'==========================================================================
Sub FavoritesSC (strName, strPath)

strFavorites = WSHShell.SpecialFolders("Favorites")
strPath = " Set objShortcut = WSHShell.CreateShortcut(strFavorites & "\" & strName & ".lnk")
objShortcut.TargetPath = strPath
objShortcut.Save

End Sub

'==========================================================================
'Add On Code goes above this line
'==========================================================================

'==========================================================================
'Clean Up Memory We Used
'==========================================================================
Set oDrives = Nothing
Set objUser = Nothing
Set objGroup = Nothing
Set WshNetwork = Nothing
Set strDomain = Nothing
Set WshShell = Nothing
Set WshPrinters = Nothing
Set oShellLink = Nothing

'==========================================================================
'Quit the Script
'==========================================================================
wscript.quit
 
Looks like the only place you will run into trouble is if the users are a member of LA_ACCOUNTING and LA_CREDIT since you map the same drive letter. If you change one of them away from an X drive, then a member of both groups will get both drive letters.

I can however offer you an interesting solution to this dilemma.

Create a new group for members of both departments. Create a new share for this new group. Inside that share create folder shortcuts that point to the shares of both LA_ACCOUNTING and LA_CREDIT. If you can, give it a drive letter other than X. I say that because the enumeration of the groups can to my knowledge happen in weird orders based on the naming conventions you use for the groups. If you give the group a name starting with a Z it should get enumerated last (I believe but have not tested) and then you could have it go to X like the others. I would be interested in your feedback on that order if you test it.

I hope you find this post helpful.

Regards,

Mark
 
ok sounds good I'll try the group suggestion. I'll put my test user in a couple of groups, test it then try what you've suggested. Thanks Mark
 
I have a questiion. On many of the posts here, when someone post code it's in a seperate window and says code. The box is white. How do I do that?
 
enclose the code like HTML.

I/m seperating with spaces so it doesn't become the box.

[ code ] [ /code]

I hope you find this post helpful.

Regards,

Mark
 
markdmac, to show:

[tt][ignore]
Code:
[/ignore][/tt] tags like this, just wrap them in:

[tt][ignore][ignore][/ignore][/ignore][/tt] tags.

NB I had to wrap the [tt]ignore[/tt] tags in [tt]ignore[/tt] tags to get them to display [smile]


Hope this helps.

[vampire][bat]
 
LOL., thanks for that tip. I knew there was one but was too lazy to look it up.

I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top