[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

Login script

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
Tog_97
Asked:
Tog_97
  • 2
1 Solution
 
RobSampsonCommented:
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
 
Tog_97Author Commented:
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
 
RobSampsonCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now