Solved

How to Setup a VBS Logon Script by Group Membership

Posted on 2010-09-06
12
357 Views
Last Modified: 2012-06-21
Hi all,

Im really new to the whole VBS scripting thing, but what im trying to do is make a script to map drives to groups.

So
Group Fdrive = \\server\Fdrive.
Group Tdrive = \\server\accounts

That kinda thing,

And then printers,

Group AccountsPrinter = \\server\AccountingPrinterName
Group SkillsPrinter = \\server\SkillsPrinterName

Or am I creating to many groups here… what’s the best way to do this

Would it be better to create a group like, Accounts T Drive and Printing

Thanks guys
0
Comment
Question by:liminal
  • 6
  • 6
12 Comments
 
LVL 8

Expert Comment

by:andoss
ID: 33614662
Are you on a domain? You can use vbscript to map based on domain groups if so which is really handy from and Admin perspective.
I can give you a copy of the VBScript we use if so.

Basically it maps drives based on username (home directories) or groups (department shares etc)
0
 
LVL 8

Accepted Solution

by:
andoss earned 500 total points
ID: 33614676
Have attached the script in code for you to peruse anyways.
There's comments which hopefully explain whats going on.

I've removed majority of our mapings just or simplicity, but you can add as many mappings as you want to the Select statement or even create separate Select statements.
n Error Resume Next

Set WshNetwork = CreateObject("WScript.Network")
 
DomainString = WshNetwork.UserDomain
UserString = WshNetwork.UserName

Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)


'Unmaps some drives which it may map so as to prevent errors
WshNetwork.RemoveNetworkDrive "F:",true,true
WshNetwork.RemoveNetworkDrive "G:",true,true
WshNetwork.RemoveNetworkDrive "L:",true,true


'Simple mapping of drives other than department shared drive
WshNetwork.MapNetworkDrive "F:", "\\servername\Applications"
WshNetwork.MapNetworkDrive "G:", "\\servername\Company Share"
wshNetwork.MapNetworkDrive "M:", "\\servername\Home Directories\" + UserString



'Shared drive mapping based on users AD Group, if user is a member of more than one AD Group and drives are same letter the last drive mapped will be given.
For Each GroupObject In UserObj.Groups
strMSG = GroupObject.Name
        Select Case GroupObject.Name
        	'Case "RHO_finance"
                '	WshNetwork.MapNetworkDrive "U:", "\\wfs01\Home Directories\CATS"
		
		Case "RHO_accounts"
			WshNetwork.MapNetworkDrive "L:", "\\servername\Departments\Accounts"
		Case "RHO_wholesale"
			WshNetwork.MapNetworkDrive "L:", "\\servername\Departments\Wholesale"
		
		
		'Shared drive mappings for exception & Misc groups. ie. Those who need access to another departments shared drive
		Case "RHO_accountsexceptions"
			WshNetwork.MapNetworkDrive "Q:", "\\servername\Departments\Accounts"
		Case "RHO_marketingexceptions"
			WshNetwork.MapNetworkDrive "Q:", "\\servername\Departments\Marketing"
		
	End Select
Next


'Removes the full UNC path from mapped drives under windows explorer and replaces with a shorter name
sDrive = "M:\"
Set oShell = CreateObject("Shell.Application")
oShell.NameSpace(sDrive).Self.Name = "Home Directory"

'sDrive = "U:\"
'Set oShell = CreateObject("Shell.Application")
'oShell.NameSpace(sDrive).Self.Name = "CATS"

sDrive = "L:\"
Set oShell = CreateObject("Shell.Application")
oShell.NameSpace(sDrive).Self.Name = "Department Share"

sDrive = "F:\"
Set oShell = CreateObject("Shell.Application")
oShell.NameSpace(sDrive).Self.Name = "Applications"

sDrive = "G:\"
Set oShell = CreateObject("Shell.Application")
oShell.NameSpace(sDrive).Self.Name = "Company Share"
 
WScript.Quit

Open in new window

0
 

Author Comment

by:liminal
ID: 33614723
Hey thanks so much for the quick response... Yeah its a domain... let me have a look, ill come back to you.

0
 

Author Comment

by:liminal
ID: 33614739
Ok, testing now.

I’m just wondering about the RHO_finance… what’s with the underscore? Is that how your groups are named? Or do you need that if you have a space in the group name.
0
 
LVL 8

Expert Comment

by:andoss
ID: 33614800
That's just how we've named our groups. I believe you should be fine if your groups have a space in the name as the code is surrounded by quotes anyways.

Hope it does what you needed :)
0
 

Author Comment

by:liminal
ID: 33614893
Yeah i figured it out... it's working well.

One thing i have noted is that it wont seem to delete the drive, im just double clicking the file... it maps... but if i remove myself from a group and and run again... the drive is still there.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:liminal
ID: 33614937
LOL, yeah it helps if you actually do remove yourself from the group in order to delete the share :)
0
 
LVL 8

Expert Comment

by:andoss
ID: 33614938
You need a separate line for each drive mapping you want removed.
ie. to remove F: Drive you use the following.

WshNetwork.RemoveNetworkDrive "F:",true,true

The problem isn't as simple as you've mapped the drive to a letter other than the ones listed to remove is it?
Although if you use the script at group policy then you'll never come across that anyway as the drives are only mapped at logon.
0
 

Author Comment

by:liminal
ID: 33614982
I didnt remove myself from the group, all good... thanks

are you using this for printers as well?
0
 
LVL 8

Expert Comment

by:andoss
ID: 33615018
No sorry we don't.
Google found me this though which looks good, not sure if you'd already seen it but doesn't look like it would be difficult to integrate to the script i posted.
http://www.computerperformance.co.uk/Logon/LogonScript_Printer_Method.htm

ie. Just add these two lines to the top of the script
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")

Then customise this line for each of the Select cases in the script i posted
objNetwork.AddWindowsPrinterConnection \\server\SkillsPrinterName


Maybe someone else can give better help with the printers.


0
 

Author Comment

by:liminal
ID: 33622708
Thanks heaps for the help :)
0
 
LVL 8

Expert Comment

by:andoss
ID: 33623111
No worries, hope you got what you needed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now