Solved

Login script

Posted on 2008-06-20
3
265 Views
Last Modified: 2010-04-16
I would like to fix a login script that dose the following:

1- Disconnect the drives: H: , G:, P: , T: , and W:

2- Map the new drive:
    H: = Home directory
    G: shared data
    P: shared programs
    T: special folder that will be mapped on group membership (Example: if user TEST is
        a member of group FINANCE then map the T: folder)
   W: anther shared data folder

In the same script I want to:
3- disconnect room1_printer, room2_printer, and so on
4- Map network printers based on computer location attribute and set a default printer
Some workstations have local printers and in some cases I dont want the local printer to be set as the default printer, instead a network printer will be the default printer.
In other cases the local printer will be set as a default printer.

4- At the end of the script I would like that the user gets a popup msg box greeting the user by his/her first name depending on the time of the day ( lets say; if between 1 am till 11 am say good morning, and if the time is  between 11 am till 7 pm say good afternoon, otherwise say good evening.
The msgbox should also say you are logged in now. Example: good morning John, you are logged in now.  

As you see I have a part of the script (thanks to jared_luker from Experts Exchange) witch helps me to map the printers based on computer location. But I have a problem on how the fix the rest and get it all in one nice and clean login script.


Here are the codes I got from jared_luker:
Dim adsinfo,nw
Set adsinfo = CreateObject("adsysteminfo")
Set objNetwork = CreateObject("WScript.Network")
Set ThisComp = GetObject("LDAP://" & adsinfo.ComputerName)
Set oUser = GetObject("LDAP://" & adsinfo.UserName)
Select Case ThisComp.Location
      Case "Room1"
            objNetwork.AddWindowsPrinterConnection "\\server\room1_printer"
      Case "Room2"
            objNetwork.AddWindowsPrinterConnection "\\server\room2_printer"
      Case "Room3"
            objNetwork.AddWindowsPrinterConnection "\\server\room3_printer"
      Case "Room4"
            objNetwork.AddWindowsPrinterConnection "\\server\room4_printer"
      Case "Room5"
            objNetwork.AddWindowsPrinterConnection "\\server\room5_printer"
End Select
--------------------------------------------------------------------------------------------------------------------
 
Thank you
Regards
0
Comment
Question by:Tog_97
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 21829789
Hi, try this script.

Regards,

Rob.
Set objNetwork = CreateObject("WScript.Network")

Set objWinntUser = GetObject("WinNT://" & objNetwork.UserDomain & "/" & objNetwork.UserName & ",user")
 

arrDrives = Array( _

	"ALL~H:~\\server\user$\" & objNetwork.UserName &, _

	"ALL~G:~\\server\shareddata", _

	"ALL~P:~\\server\sharedprograms", _

	"FINANCE~T:~\\server\finance", _

	"ALL~W:~\\server\shareddata2" _

	)
 

For Each strDriveList In arrDrives

	strGroup = Split(strDriveList, "~")(0)

	strDriveLetter = Split(strDriveList, "~")(1)

	strDrivePath = Split(strDriveList, "~")(2)

	objNetwork.RemoveNetworkDrive strDriveLetter, True, True

	WScript.Sleep 1000

	If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strGroup) = True Then

		objNetwork.MapNetworkDrive strDriveLetter, strDrivePath, True

	End If

Next
 

arrPrintersToDisconnect = Array( _

	"\\server\printer1", _

	"\\server\printer2", _

	"\\server\printer3" _

	)
 

For Each strPrinter In arrPrintersToDisconnect

	objNetwork.RemovePrinterConnection strPrinter, True, True

Next
 

Set objADSysInfo = CreateObject("ADSystemInfo)

Set objComputer = GetObject("LDAP://" & objADSysInfo.ComputerName)

Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)

Select Case objComputer.Location

	Case "Room1"

		objNetwork.AddWindowsPrinterConnection "\\server\room1_printer"

	Case "Room2" 

		objNetwork.AddWindowsPrinterConnection "\\server\room2_printer"

	Case "Room3" 

		objNetwork.AddWindowsPrinterConnection "\\server\room3_printer"

	Case "Room4" 

		objNetwork.AddWindowsPrinterConnection "\\server\room4_printer"

	Case "Room5" 

		objNetwork.AddWindowsPrinterConnection "\\server\room5_printer"

End Select
 

strDefaultPrinter = "\\server\room3_printer"

On Error Resume Next

objNetwork.SetDefaultPrinter strDefaultPrinter

Err.Clear

On Error GoTo 0
 

If Hour(Now) >= 1 And Hour(Now) < 11 Then

	MsgBox "Good morning " & objUser.GivenName & ". You are now logged in."

ElseIf Hour(Now) >= 11 And Hour(Now) < 7 Then

	MsgBox "Good afternoon " & objUser.GivenName & ". You are now logged in."

Else

	MsgBox "Good evening " & objUser.GivenName & ". You are now logged in."

End If
 

Function IsMemberOfGroup(strUserDomain, objUser, strGroup) 'the user is a member of a specified group

	IsMemberOfGroup = False

	Dim objGroup

	If strGroup <> "ALL" Then

		On Error Resume Next

		Set objGroup = GetObject("WinNT://" & strUserDomain & "/" & strGroup & ",group")

		If Err.Number Then

			IsMemberOfGroup = "Error"

		Else

			IsMemberOfGroup = objGroup.IsMember(objUser.ADsPath)

			'MsgBox objUser.ADsPath

		End If

	Else

		IsMemberOfGroup = True

	End If

End Function

Open in new window

0
 

Author Comment

by:Tog_97
ID: 21830740
Hi Rob,
Thanks for the quick answer.
Just a few quastions:
forgive me but, I don´t understand the following:
----------------------------------------------------
strDefaultPrinter = "\\server\room3_printer"
On Error Resume Next
objNetwork.SetDefaultPrinter strDefaultPrinter
Err.Clear
On Error GoTo 0
----------------------------------
I was thinking like this the script will check for the location attribute of the computer and if the location is(lets say) room 3 the printer in room 3 will be installed and then it will set that printer as a default.
i just wonder how the script will set the printer  in room 4 as a default printer if the location is room
Another thing is:
If the script will install 2 or 3 printers. lets say if the location of the computer is room 1 the install the printer room 1 and the printer in room 2 and room 3 and set the printer in room 1 as a default printer.
One more thing:
Where dose the script checks if the user is a member of lets say FINANCE group , and if he/she is a member of this group the drive T: will be mapped?

Is it me who doesnt get the point here :).
As I said before I really not good at this, and thanks the guys like you i am trying to learn.

Thank in advaice
Regards,



0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21835860
Sorry mate, I was in a bit of a hurry, and didn't explain it....

To answer your first question, this bit:
strDefaultPrinter = "\\server\room3_printer"
On Error Resume Next
objNetwork.SetDefaultPrinter strDefaultPrinter
Err.Clear
On Error GoTo 0

will try to set \\server\room3_printer as the default printer, whether it is mapped or not, but now that I think about it, it won't really suit, because as you say, you might be in room 2.

If you want to it set a default printer based on the location, then this section:

Select Case objComputer.Location
      Case "Room1"
            objNetwork.AddWindowsPrinterConnection "\\server\room1_printer"
      Case "Room2"
            objNetwork.AddWindowsPrinterConnection "\\server\room2_printer"
      Case "Room3"
            objNetwork.AddWindowsPrinterConnection "\\server\room3_printer"
      Case "Room4"
            objNetwork.AddWindowsPrinterConnection "\\server\room4_printer"
      Case "Room5"
            objNetwork.AddWindowsPrinterConnection "\\server\room5_printer"
End Select


should just become this:

Select Case objComputer.Location
      Case "Room1"
            objNetwork.AddWindowsPrinterConnection "\\server\room1_printer"
            objNetwork.AddWindowsPrinterConnection "\\server\room1_printer2"
            objNetwork.SetDefaultPrinter "\\server\room1_printer"
      Case "Room2"
            objNetwork.AddWindowsPrinterConnection "\\server\room2_printer"
            objNetwork.AddWindowsPrinterConnection "\\server\room1_printer2"
            objNetwork.SetDefaultPrinter "\\server\room2_printer
      Case "Room3"
            objNetwork.AddWindowsPrinterConnection "\\server\room3_printer"
            objNetwork.AddWindowsPrinterConnection "\\server\room1_printer2"
            objNetwork.SetDefaultPrinter "\\server\room3_printer"
      Case "Room4"
            objNetwork.AddWindowsPrinterConnection "\\server\room4_printer"
            objNetwork.SetDefaultPrinter "\\server\room4_printer"
      Case "Room5"
            objNetwork.AddWindowsPrinterConnection "\\server\room5_printer"
            objNetwork.SetDefaultPrinter "\\server\room5_printer"
End Select


and then you can remove the first block....

Also, the part that checks for the group membership is this bit:

      If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strGroup) = True Then
            objNetwork.MapNetworkDrive strDriveLetter, strDrivePath, True
      End If

strGroup is taken as the first element of each line from the array
arrDrives = Array( _
      "ALL~H:~\\server\user$\" & objNetwork.UserName &, _
      "ALL~G:~\\server\shareddata", _
      "ALL~P:~\\server\sharedprograms", _
      "FINANCE~T:~\\server\finance", _
      "ALL~W:~\\server\shareddata2" _
      )

where if you define ALL it will map the drive for all users.  If you specify anything else, then it must match a security group name from your domain, and if the user matches, it will map the drive.

I hope that helps.

If you have any more questions, feel free to ask.

Regards,

Rob.
Set objNetwork = CreateObject("WScript.Network")

Set objWinntUser = GetObject("WinNT://" & objNetwork.UserDomain & "/" & objNetwork.UserName & ",user")

 

arrDrives = Array( _

	"ALL~H:~\\server\user$\" & objNetwork.UserName &, _

	"ALL~G:~\\server\shareddata", _

	"ALL~P:~\\server\sharedprograms", _

	"FINANCE~T:~\\server\finance", _

	"ALL~W:~\\server\shareddata2" _

	)

 

For Each strDriveList In arrDrives

	strGroup = Split(strDriveList, "~")(0)

	strDriveLetter = Split(strDriveList, "~")(1)

	strDrivePath = Split(strDriveList, "~")(2)

	objNetwork.RemoveNetworkDrive strDriveLetter, True, True

	WScript.Sleep 1000

	If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strGroup) = True Then

		objNetwork.MapNetworkDrive strDriveLetter, strDrivePath, True

	End If

Next

 

arrPrintersToDisconnect = Array( _

	"\\server\printer1", _

	"\\server\printer2", _

	"\\server\printer3" _

	)

 

For Each strPrinter In arrPrintersToDisconnect

	objNetwork.RemovePrinterConnection strPrinter, True, True

Next

 

Set objADSysInfo = CreateObject("ADSystemInfo)

Set objComputer = GetObject("LDAP://" & objADSysInfo.ComputerName)

Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)

Select Case objComputer.Location

	Case "Room1"

		objNetwork.AddWindowsPrinterConnection "\\server\room1_printer"

		objNetwork.AddWindowsPrinterConnection "\\server\room1_printer2"

		objNetwork.SetDefaultPrinter "\\server\room1_printer"

	Case "Room2" 

		objNetwork.AddWindowsPrinterConnection "\\server\room2_printer"

		objNetwork.AddWindowsPrinterConnection "\\server\room1_printer2"

		objNetwork.SetDefaultPrinter "\\server\room2_printer

	Case "Room3" 

		objNetwork.AddWindowsPrinterConnection "\\server\room3_printer"

		objNetwork.AddWindowsPrinterConnection "\\server\room1_printer2"

		objNetwork.SetDefaultPrinter "\\server\room3_printer"

	Case "Room4" 

		objNetwork.AddWindowsPrinterConnection "\\server\room4_printer"

		objNetwork.SetDefaultPrinter "\\server\room4_printer"

	Case "Room5" 

		objNetwork.AddWindowsPrinterConnection "\\server\room5_printer"

		objNetwork.SetDefaultPrinter "\\server\room5_printer"

End Select
 

If Hour(Now) >= 1 And Hour(Now) < 11 Then

	MsgBox "Good morning " & objUser.GivenName & ". You are now logged in."

ElseIf Hour(Now) >= 11 And Hour(Now) < 7 Then

	MsgBox "Good afternoon " & objUser.GivenName & ". You are now logged in."

Else

	MsgBox "Good evening " & objUser.GivenName & ". You are now logged in."

End If

 

Function IsMemberOfGroup(strUserDomain, objUser, strGroup) 'the user is a member of a specified group

	IsMemberOfGroup = False

	Dim objGroup

	If strGroup <> "ALL" Then

		On Error Resume Next

		Set objGroup = GetObject("WinNT://" & strUserDomain & "/" & strGroup & ",group")

		If Err.Number Then

			IsMemberOfGroup = "Error"

		Else

			IsMemberOfGroup = objGroup.IsMember(objUser.ADsPath)

			'MsgBox objUser.ADsPath

		End If

	Else

		IsMemberOfGroup = True

	End If

End Function

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

16 Experts available now in Live!

Get 1:1 Help Now