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

ERROR: Login script for Mapped drives ...

Status
Not open for further replies.

MojoZig

Technical User
Joined
Sep 27, 2005
Messages
61
Location
US
Ok, I am really having a tough time with this and need some help cleaning it up and fixing my error. This code is supposed to connect to LDAP and map the user's department S: drive and Division T: drive. The code works if you are getting an S: and T: but if the user doesn't have a Division, nothing gets mapped on my full script I run, on this one I get an error.

So I broke the entire full script down to just this portion. This is what happens: If my test user is in the first listed group only "COO_AP_Airport", the scirpt errors with "Line: 11 Char:1 Object not a collection" If I un-Rem the DIM line, same error ...

If the user IS a member of both groups below, they both get mapped properly. (The full script has many more departments and divisions)

Can anybody assist me please?? Line 11 is: "For Each strGroup in objUser.MemberOf"

Should the line to DIM those items be there or not and how do I fix it to where if the user is in only one group that one group gets mapped?


Code:
' VB Script Document

'These next lines connect to LDAP

'DIM strUserPath, strGroup, objUser, objSysInfo, strGroupPath, objGroup, strGroupName, objNetwork

Set objNetwork = CreateObject("WScript.Network")
Set objSysInfo = CreateObject("ADSystemInfo")
strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)

For Each strGroup in objUser.MemberOf
    strGroupPath = "LDAP://" & strGroup
    Set objGroup = GetObject(strGroupPath)
    strGroupName = objGroup.CN

   
on error resume next     

    Select Case strGroupName


   Case "COO_AP_Airport"
objNetwork.MapNetworkDrive "S:", "\\chldept\Drive\Airport"

   Case "COO_AP_Administration"
objNetwork.MapNetworkDrive "T:", "\\chldept\Drive\Airport\COO_AP_Administration"            

         End Select   

    Next
 
Rather than troubleshoot this, I'll offer an easier solution:

faq329-5798

I hope you find this post helpful.

Regards,

Mark
 
Thanks Mark, you do have a lot of information in your FAQ but I don't quite get it. The script I'm using works except for this one quirk ... I firmly believe the problem to be something with the:
"For Each strGroup in objUser.MemberOf"

Since strGroup was never defined ... I just don't know enough to know how to define it and what to define it as ... I'll look though your faq some more to see if I can figure it out I guess ...

Tommy
 
My FAQ has the code you need to bind to the groups and then do something similar with the Select Case statements to map the needed drives.

Feel free to ask questions on it, but I do have a lot of comments int he FAQ to help you follow what each section is doing.

I hope you find this post helpful.

Regards,

Mark
 
Mark, I think that I will use your script in the end, it's very clean and I appreciate your time in it! Question for you, Would I be able to use this script in yours where you reserved space for add on code? Would I need to change any of the variables or whatever?

Code:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''

'Adds the HOSTS file
Const OverwriteExisting = TRUE

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
objFSO.CopyFile "\\server\updates\hosts", WshShell.ExpandEnvironmentStrings("%systemroot%\system32\drivers\etc\"), OverwriteExisting

'This copies the SDAT.exe file to C:\ and then installes it with a silent switch
objFSO.CopyFile "\\server\updates\sdat.exe" , "c:\", OverwriteExisting
wshshell.run "c:\sdat.exe -s",,true
set wshshell=Nothing

'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''

'Runs NAI Stinger - WARNING: Before you activate this portion of the script, make sure
'the latest stinger.exe was downloaded and resides in the location it's supposed to be.
'UnREM the next 4 lines for stinger to run ...

'objFSO.CopyFile "\\server\updates\stinger.exe" , "c:\", OverwriteExisting
'set wshshell=createobject("wscript.shell")
'wshshell.run "c:\stinger.exe /ADL /GO /LOG /SILENT",,true
'set wshshell=nothing

'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''

This script just downloads and overwrites the Hosts file, downloads and installs the lateset SDAT as well as has a feature for running stinger at startup if unremmed ...

Also,

 
Yes, you could add this into my script, for keeping it clean put it in the add in section, but it coud actually go anywhere.

Remove the 2 lines starting with Set WSHShell as it is already initialized and you don't want to set it to nothing till the end of the script.

I hope you find this post helpful.

Regards,

Mark
 
Login Script.

I'm trying to create a login script to Map a drive if a users is a member of a group Im getting "Variable is undefined: 'IsMember'. Need some help please. Script below:.

Option Explicit
'On Error Resume Next
Dim objNetwork, strUserName, strGroup, shell, strPersonal
Set objNetwork = CreateObject("WScript.Network")
strUserName =objNetwork.UserName
strGroup = "My_Docs"

objNetwork.MapNetworkDrive "q:", "\\servername\home$\" & strUserName
objNetwork.MapNetworkDrive "h:", "\\servername
objNetwork.MapNetworkDrive "l:", "\\servername$"
objNetwork.MapNetworkDrive "p:", "\\servername\tsprofile$\" & strUserName

' ReMap My Documents folder to servername if member of My_Docs
If IsMember("My_Docs") Then
wscript.echo "Mapped Drive"
objNetwork.RemoveNetworkDrive "q:", true, true objNetwork.MapNetworkDrive "q:", "\\differentservername\home$\" & strUserName
End If

 
lpb71,
Do yourself a favor ... follow the link above to FAQ329-5798. Use Mark's script, it will allow your organization room to grow and is very customizable. I use it for 600+ users. He did a great job putting it together, no reason to do the work again when you can use that time to customize it to your liking ...


Here's my final script:

Code:
ON ERROR RESUME NEXT

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path


Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
'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


'Synchronizes the time with Server our NTP Server
WSHShell.Run "NET TIME \\YOURTIMESERVER /set /y"

'Disconnect any drive mappings as needed.
WSHNetwork.RemoveNetworkDrive "G:", True, True
WSHNetwork.RemoveNetworkDrive "H:", True, True
WSHNetwork.RemoveNetworkDrive "J:", True, True
WSHNetwork.RemoveNetworkDrive "M:", True, True
WSHNetwork.RemoveNetworkDrive "O:", True, True
WSHNetwork.RemoveNetworkDrive "Q:", True, True
WSHNetwork.RemoveNetworkDrive "R:", True, True
WSHNetwork.RemoveNetworkDrive "S:", True, True
WSHNetwork.RemoveNetworkDrive "T:", True, True
WSHNetwork.RemoveNetworkDrive "U:", True, True
WSHNetwork.RemoveNetworkDrive "X:", True, True
WSHNetwork.RemoveNetworkDrive "Y:", True, True




'===============================
'Disconnect ALL mapped drives
'===============================
'Set clDrives = WshNetwork.EnumNetworkDrives
'For i = 0 to clDrives.Count -1 Step 2
'    WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
'Next 
'===============================


'Give the PC time to do the disconnect, wait 300 milliseconds
wscript.sleep 300

'Map drives needed by all
'Note the first command uses the user name as a variable to map to a user share.
WSHNetwork.MapNetworkDrive "G:", "\\SERVER\drive\City_GIS",True
WSHNetwork.MapNetworkDrive "H:", "\\SERVER\drive\" & UserString,True
WSHNetwork.MapNetworkDrive "O:", "\\SERVER\Folder",True
WSHNetwork.MapNetworkDrive "U:", "\\SERVER\Folder",True
WSHNetwork.MapNetworkDrive "X:", "\\SERVER\Folder",True


'Now check for group memberships and map appropriate drives
For Each GroupObj In UserObj.Groups
    Select Case GroupObj.Name
    'Check for group memberships and take needed action
            
         '    These are for the Department's Main S: Drive Share and printers
    
        
        Case "FIRST_SECURITY_GROUP" 
            WSHNetwork.MapNetworkDrive "S:", "\\SERVER\folder\folder",True

        Case "SECOND_SECURITY_GROUP"
            WSHNetwork.MapNetworkDrive "S:", "\\SERVER\folder\Folder",True
            WSHNetwork.AddWindowsPrinterConnection "\\SERVER\Building Inspectors LaserJet 2200", "Building Inspectors LaserJet 2200"
                
            
                       
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''            
       
'    These are for the Department's Secondary Division T: Drive Share and printers      
       

        Case "THIRD_SECURITY_GROUP"
            WSHNetwork.MapNetworkDrive "T:", "\\SERVER\folder\",True        
       
       
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''        
            
'            This Section is for various other drives.    

         Case "Security_group_Name"       
            WSHNetwork.MapNetworkDrive "R:", "\\SERVER\folder",True  
WSHNetwork.MapNetworkDrive "M:", "\\SERVER\folder",True 
             
                   
    End Select
Next

                                
'Install Printers
'These next lines map the Central Services Aficio Copiers to everyone's PC
WSHNetwork.AddWindowsPrinterConnection "\\SERVER\Aficio 2060", "CMO Aficio 2060"
WSHNetwork.AddWindowsPrinterConnection "\\SERVER\Aficio 2232C", "CMO Aficio 2232C"
WSHNetwork.AddWindowsPrinterConnection "\\SERVER\Aficio 2060", "Purchasing Aficio 2060"
WSHNetwork.AddWindowsPrinterConnection "\\SERVER\Aficio 2232C", "Purchasing Aficio 2232C"

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



'=====================================
'Adds the HOSTS file
Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "\\SERVER\updates\hosts", WshShell.ExpandEnvironmentStrings("%systemroot%\system32\drivers\etc\"), OverwriteExisting
'=====================================
'This copies the SDAT.exe file to C:\ and then installs it with a silent switch
objFSO.CopyFile "\\SERVER\updates\sdat.exe" , "c:\", OverwriteExisting
wshshell.run "c:\sdat.exe -s",,true
Set objFSO=Nothing
'=====================================
'Runs NAI Stinger - WARNING: Before you activate this portion of the script, make sure
'the latest stinger.exe was downloaded and resides in the location it's supposed to be.
'UnREM the next 2 lines for stinger to run ...

'objFSO.CopyFile "\\SERVER\updates\stinger.exe" , "c:\", OverwriteExisting
'wshshell.run "c:\stinger.exe /ADL /GO /LOG /SILENT",,true
'=====================================



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

'Clean Up Memory We Used
set UserObj = Nothing
set GroupObj = Nothing
set WSHNetwork = Nothing
set DomainString = Nothing
set WSHSHell = Nothing
Set WSHPrinters = Nothing
  
  
  
'dtmEnd = Now()
'Wscript.Echo DateDiff("s", dtmStart, dtmEnd)  

'Quit the Script
wscript.quit
 
on a logonscript note. please be careful with using on error resume next and blindly connecting printers and drives. why try and map a drive or printer if it is already connected?
 
Blindly mapping? It's a login script. It's supposed to map drives and printers and everything else we want it to do. I wouln't say that this script blindly maps drives and printers... I'm not blindly managing the city's AD and my 600+ users and their profiles.

This script strategically unmaps the drive letters we have reserved and remaps them to make sure the users have all their drives everytime they login.

Why not use On Error Resume Next?

Mark did a great job, it's exactly what I was looking for, well layed out, nicely written, and cleans the memory in the end. If you have suggestion on making it better, please submit it. I'm all for making something better!

Very Respectfully,
MojoZig
 
lpb71,
you can remove the:

'dtmEnd = Now()
'Wscript.Echo DateDiff("s", dtmStart, dtmEnd)

at the bottom of the script or if you want to get a time on how long the script takes to run, just add:

'dtmStart = Now()

at the beginning and un-comment all said lines.. I have a bunch of mappings in my script and it still only takes 2-4 seconds to process to inlclude the virus updates and hosts file push.

Enjoy.

MojoZig
 
fair enough you

+ blindly disconnect mapped drives
+ then you re-map the same drive

the vast majority of users do not think 'i know i will unmap my homedrive which i use on a regular basis to map my H drive to \\iamaturd.com'

this being the case for 99% of the time your logonscirpt is going to disconnect and then reconnect the same flippin drives every flipping logon.

if you don think that this is probably the most inefficient method of handling printer and drive mappings and you consider this to be strategic then fair enough

i suppose your users shoudl be grateful that you dont decied to disconnect all the printers before re-mapping them, i mean, it might only take 5 seconds per drive mapping (in your script this means a minute) but a printer would be about 20 seconds each one!!! i do hope no one logs on via a RAS connection, lol :-)
 
Mr.M.
So then do you just hard-map all your drive letters and not script them?

Do you have a logon script on your network? What do you use it for?

When a user's drive mapping gets deleted, do you proxy/remote in to their PC and re-map it? Or, do you have a scripting technique that will fix this script of Marks's that you say "blindly" maps drives and printers?

I don't want to sound rude in any way, but you didn't offer any kind of assistance. I'll be the first to admit that I don't have half the knowledge of scripting that I should, which is evident since I am often looking for help on this site.

I do have users that decide to remap their drives and set their own standard drive mappings. It is very nice and no overhead for the printers to get installed (It would seem). If a user changes departments, I only have to move them to the new security group and all the map drives and printers get installed for the new department.

When I say the script only takes 2-4 seconds, it only takes 2-4 seconds to completely process drive mappings needed as well as the sdat process to appear in task manager, install and quit.

I'm still very curious though:

1. Do you have a way of making the script verify drive and printer mappings and only map/install the ones that aren't there? (This was something that was pondered at one point but I couldn't find the information needed.)

2. Why not use On Error Resume Next on a logon script?

Really man, if you have information that will make it better I'd really like to know about it. However, if you have nothing to add in order to make it better then no further response is necessary.

I really appreciate your time and no disrespect is intended or implied,
MojoZig
 
MojoZig, first thansk for the praise. I am very glad you have found the FAQ to be useful. Mr Movie has created an FAQ faq329-6204 that outlines what he is suggesting.

We have a fundemental difference in opinion on whether or not this is necessary. Out of respect I agree to disagree.

Having supported both small businesses and large enterprises I have formed my own opinions, having seen users remap drives that were reserved by IT for application standardization.

Mr Movies solution is totally viable, though I feel it is a lot of extra code for such a simple task. As your testing has shown the time to remove and remap drives is insignificant. I have not tested it, but I suspect that the time it takes to process the Mr Movies extra code and check the status if a drive exists or not will take about the same amount of time. Furthermore, if the drive DOES exist but maps to the wrong location then what is the sense in having a login script if the end result will not be a unified configuration?

Regarding the On Error Resume Next, this is a necessary evil. When creating collections with WMI such as enumerating printers, you can get errors thrown from the script if you don't use On Error Resume Next. WMI loves to toss errors that an "object is not a collection" for instances like enumerating printers when none are mapped etc. For the purpose of having a customizeable yet generic script, I believe using this is the best course of action. If you wish to really lock down a script tight, remove it and also use the Option Explicit to ensure all objects are properly declared.

I hope this clears things up and I wold suggest that you take a look at Mr Movie's FAQ to so you can better understand what he is suggesting and form your own opinions.

Mr Movie, regarding your FAQ I would like to make a few suggestions. I'd suggest you add some comments to the code so novices can follow it better. I'd also seperate the FAQ text from the actual code using [ignore]
Code:
[/ignore] tags to make the seperation clearer.

I hope you find this post helpful.

Regards,

Mark
 
i didnt say markdmac script blindly maps drives, i said your did.

i am not going to post any code in this thread, the penny needs to drop at your end before you will understand the 'coolness' of the code. i am the undisputed king of logonscripts, my logonscript is the best in the world period :-)

you ask

Mr.M.
So then do you just hard-map all your drive letters and not script them?

no, i dont, you do, look at your code you have hardcoded drive and printer mappings. i was going to mention the benefit of keeping data and computation seperate but that might be alittle out of reach at the moment.

so, getting back to my question

mrmovie "why try and map a drive or printer if it is already connected?"

1. check which drives the user has already connected, get this information into memory (array, dictionary object). wshNetwork.EnumNetworkDrives
2. when come to your wanted drive mappings...check to see if the H: drive is already connected to \\serverA\shareA, if it is then you dont need to bother
3. before you attempt to map the drive check that the share is there FSO.FolderExists

log all of this so you can see if there is a duff share etc etc.
the same logic applies to mapping printers

On Error Resume Next..at the top of a script....If I run your script once i have no doubt it will probably work...however on the second attempt it will fall in a heap. the idea of putting On Error Resume Next in to hide your poor coding is not what it is intended for...i think. you would be better with Option Explicit

I think you will find you are wrong about the time it takes to map a drive. seeing as you have no logging in your logonscript i find it difficult to imagine how you can maitain this script in the future and debug it when a user rings up and says something is wrong with my logonscript. We are in IT (unfortunately i am nerd ;-) i do speak for myself of course) Information Technology. try and grab/log as much info as you can in all your scripts. it will serve you well.

respekt

von don moyla



 
markdmac "Furthermore, if the drive DOES exist but maps to the wrong location then what is the sense in having a login script if the end result will not be a unified configuration"

i am afriad this statement is incorrect, if you look at the code you will find it checks for which server and share the drive is connected to, what would be the point of the check otherwise?

my code is faster than a failed error producing call.
perhaps if you only make one failed call then this might be faster than my method, but as the number of calls increase so does the advantage of my approach.

i have tested this to the death and stated my reputation on it whilst tendering against one of the largest service providers in the world.
 
Mr.M. - You stated, "i didnt say markdmac script blindly maps drives, i said your did."

My ENTIRE script is his creation except the server names, group names, some comments for drive mapping organization and code between the add on code lines. I don't think anything else was changed. Believe it or not, I even have his name in the credit comment still!

I assure you that a log file is the last place I need to look to find out why someone can't log in to the network. With such a solid script, such as Mark's, I know that most problems will now be with transport or the client itself may have a failed nic. The only problem I have had with the script was when I added the wrong path for a server which was recognizable and quickly corrected.

All you had to do was inform me of ways of making it better or just point me to YOUR FAQ just like Mark did his long ago, and to yours just a minute ago to be helpful.

You stated, "i am the undisputed king of logonscripts, my logonscript is the best in the world period." Wow your royal highnass, glad I met you! :)

Just kidding, don't take it personal, I just couldn't resist! :)

Mark,
Thanks again my friend! Your script is serving me well beyond my expectations, and to top that, it's very clean code which is must when you have OCD!

lpb71, I sure hope you now have enough information for your script now. Please look at BOTH FAQs, I'm sure they both will have enough information to help you along the way. Let us know which side of the Logon Force you choose!

Anyway, I have to get back to work on my intranet phone book courtesy of ZColton in the forums as well as connecting my establishment to point and pray's new redirect form. You all have a great day!

Sincerely,
MojoZig

 
i am not going to post any code in this thread, the penny needs to drop at your end before you will understand the 'coolness' of the code. i am the undisputed king of logonscripts, my logonscript is the best in the world period

LOL, I'd love to debate this point but these forums are not the right venue for that. But lets face it, from a geek perspective mapping a drive can't possibly compare to having the computer speak to you at login when evaluating a coolness factor! [wink]

I hope you find this post helpful.

Regards,

Mark
 
Yeah, that part was definately rated high on the coolness factor, unfortunately my boss didn't think we needed that feature as of yet! :o) heheh ...

mojozig
 
It is totally useless, I agree. Just something fun to play with. I like to use that to get someones attention. I'll type a message up for them in a script and put it in the Run Once registry key, so it verbally gives any important message for a user I was trying to fix who may have walked away from their desk. I usually only do that for the people who ignore email and any notes you leave on their desk. It certainly gets their attention (and freaks them out).



I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top