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

Need help with W2K3 logon script

Status
Not open for further replies.

mmonti

IS-IT--Management
Sep 18, 2003
30
0
0
US
Hi,
I am hoping someone could help me out with a vbscript. This is my first one and I am having trouble adding printers and mapped drives according to a users windows security group.
When I execute the script it will map the printers and drives for the first security group regardless if the user is a member or not then it will end.
Any advice is greatly appreciated.

'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 3.1
'
' NAME:
'
' AUTHOR:
' DATE : 7/8/2004
'
' COMMENT: Test script
'
'==========================================================================

Option Explicit
Dim objNetwork, sysinfo, CurrentUser
Dim strGroups

Const it = "cn=tim it management"
Const adminstrative = "cn=tim administrative"
Const ops = "cn=tim ops managers"
Const exec = "cn=tim corp exec"
Const finance = "cn=tim finance"

Set objNetwork = CreateObject("WScript.Network")

' Forces script to skip errors (rem below line to see errors)
on error resume Next

Dim WshNetwork
Set WshNetwork = WScript.CreateObject("WScript.Network")

' SET ALL DRIVES TO BE REMOVED HERE
WshNetwork.RemoveNetworkDrive "G:"
WshNetwork.RemoveNetworkDrive "N:"
WshNetwork.RemoveNetworkDrive "O:"
WshNetwork.RemoveNetworkDrive "R:"

' Set ALL NETWORK PRINTERS TO BE REMOVED HERE
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.RemovePrinterConnection "\\dc1a\HP laserjet 8000"
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.RemovePrinterConnection "\\dc1a\HP Color Laserjet 4550C"
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.RemovePrinterConnection "\\dc1a\HP Laserjet 2200 (Ops)"
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.RemovePrinterConnection "\\dc1a\HP laserjet 2200 (Paul)"
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.RemovePrinterConnection "\\dc1a\HP Laserjet 4050"

Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.RemovePrinterConnection "\\chulak\HP8000"
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.RemovePrinterConnection "\\chulak\HP4550"
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.RemovePrinterConnection "\\chulak\HP2200ops"
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.RemovePrinterConnection "\\chulak\HP2200P"
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.RemovePrinterConnection "\\chulak\HP4050"

' THIS LINE WILL RETRIEVE USERS AD INFO
Set sysinfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & sysinfo.UserName & "")
strGroups = LCase(Join(CurrentUser.MemberOf))

' SETTING ALL NETWORKDRIVES THAT ARE ASSIGNED TO ALL USERS
objNetwork.MapNetworkDrive "g:", "\\dc2a\g"
objNetwork.MapNetworkDrive "n:", "\\dc2a\n"
' objNetwork.MapNetworkDrive "z:", "\\servername\Users\" & objNetwork.UserName

' SETTING ALL PRINTERS ASSIGNED TO ALL USERS
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.AddWindowsPrinterConnection "\\atlantis\HP8000"
WshNetwork.AddWindowsPrinterConnection "\\atlantis\HP4550"

' EXTRA PRINTERS FOR SPECIFIC SECURITY GROUP

If InStr(strGroups, administrative) Then

' WScript.Echo("Made it To administrative")
objNetwork.MapNetworkDrive "r:", "\\dc2a\r"
objNetwork.MapNetworkDrive "o:", "\\dc2a\o"
Set WshNetwork = WScript.CreateObject ("WScript.Network")
WshNetwork.SetDefaultPrinter "\\atlantis\HP8000"

Elseif InStr(strGroups, finance) Then

' WScript.Echo("Made it To finance")
objNetwork.MapNetworkDrive "o:", "\\dc2a\o"
Set WshNetwork = WScript.CreateObject ("WScript.Network")
WshNetwork.AddWindowsPrinterConnection "\\atlantis\HP4050"
' WshNetwork.SetDefaultPrinter "\\atlantis\HP4050"

Elseif InStr(strGroups, ops) Then

' WScript.Echo("Made it To OPs")
objNetwork.MapNetworkDrive "o:", "\\dc2a\o"
Set WshNetwork = WScript.CreateObject ("WScript.Network")
WshNetwork.AddWindowsPrinterConnection "\\atlantis\HP2200ops"
WshNetwork.SetDefaultPrinter "\\atlantis\HP2200ops"

Elseif InStr(strGroups, exec) Then

' WScript.Echo("Made it To Corp Exec")
objNetwork.MapNetworkDrive "o:", "\\dc2a\o"
objNetwork.MapNetworkDrive "r:", "\\dc2a\r"
Set WshNetwork = WScript.CreateObject ("WScript.Network")
' WshNetwork.AddWindowsPrinterConnection "\\atlantis\HP2200P"

Elseif InStr(strGroups, it) Then

WScript.Echo("made it To IT")
objNetwork.MapNetworkDrive "r:", "\\dc2a\r"
objNetwork.MapNetworkDrive "o:", "\\dc2a\o"
Set WshNetwork = WScript.CreateObject ("WScript.Network")
WshNetwork.AddWindowsPrinterConnection "\\atlantis\HP2200P"
WshNetwork.AddWindowsPrinterConnection "\\atlantis\HP2200ops"
' WshNetwork.SetDefaultPrinter "\\atlantis\HP8000"

End If

' Wscript.Echo "Script is complete"

WScript.quit

-Monti
 
Hello mmonti,

[1] You do not need to instantiate everything objNetwork every time you use it. One instance acts as an engine to do all work.
[2] To retrieve memberof attribute, you need to do express. It is not retrieved to the cache automatically.
[3] instr() found results position <>0.
[3] Other minor modifications as seen.
Code:
Option Explicit
Dim objNetwork, sysinfo, CurrentUser
Dim strGroups

Const it = "cn=tim it management"
Const adminstrative = "cn=tim administrative"
Const ops = "cn=tim ops managers"
Const exec = "cn=tim corp exec"
Const finance = "cn=tim finance"

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

' Forces script to skip errors (rem below line to see errors)
on error resume Next

Dim WshNetwork
Set WshNetwork = WScript.CreateObject("WScript.Network")

' SET ALL DRIVES TO BE REMOVED HERE
WshNetwork.RemoveNetworkDrive "G:"
WshNetwork.RemoveNetworkDrive "N:"
WshNetwork.RemoveNetworkDrive "O:"
WshNetwork.RemoveNetworkDrive "R:"

' Set ALL NETWORK PRINTERS TO BE REMOVED HERE
WshNetwork.RemovePrinterConnection "\\dc1a\HP laserjet 8000"
WshNetwork.RemovePrinterConnection "\\dc1a\HP Color Laserjet 4550C"
WshNetwork.RemovePrinterConnection "\\dc1a\HP Laserjet 2200 (Ops)"
WshNetwork.RemovePrinterConnection "\\dc1a\HP laserjet 2200 (Paul)"
WshNetwork.RemovePrinterConnection "\\dc1a\HP Laserjet 4050"

WshNetwork.RemovePrinterConnection "\\chulak\HP8000"
WshNetwork.RemovePrinterConnection "\\chulak\HP4550"
WshNetwork.RemovePrinterConnection "\\chulak\HP2200ops"
WshNetwork.RemovePrinterConnection "\\chulak\HP2200P"
WshNetwork.RemovePrinterConnection "\\chulak\HP4050"

' THIS LINE WILL RETRIEVE USERS AD INFO
Set sysinfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & sysinfo.UserName & "")
strGroups = LCase(Join(CurrentUser.GetEx("MemberOf"),";"))

' SETTING ALL NETWORKDRIVES THAT ARE ASSIGNED TO ALL USERS
WshNetwork.MapNetworkDrive "g:", "\\dc2a\g"
WshNetwork.MapNetworkDrive "n:", "\\dc2a\n"  
' WshNetwork.MapNetworkDrive "z:", "\\servername\Users\"  & WshNetwork.UserName

' SETTING ALL PRINTERS ASSIGNED TO ALL USERS
WshNetwork.AddWindowsPrinterConnection "\\atlantis\HP8000"
WshNetwork.AddWindowsPrinterConnection "\\atlantis\HP4550"

' EXTRA PRINTERS FOR SPECIFIC SECURITY GROUP

If InStr(strGroups, administrative)<>0 Then

' WScript.Echo("Made it To administrative")
    WshNetwork.MapNetworkDrive "r:", "\\dc2a\r"
    WshNetwork.MapNetworkDrive "o:", "\\dc2a\o"
    WshNetwork.SetDefaultPrinter "\\atlantis\HP8000"
   
Elseif InStr(strGroups, finance)<>0 Then

' WScript.Echo("Made it To finance")
        WshNetwork.MapNetworkDrive "o:", "\\dc2a\o"
    WshNetwork.AddWindowsPrinterConnection "\\atlantis\HP4050"
'        WshNetwork.SetDefaultPrinter "\\atlantis\HP4050" 
          
Elseif InStr(strGroups, ops)<>0 Then

' WScript.Echo("Made it To OPs")
        WshNetwork.MapNetworkDrive "o:", "\\dc2a\o"
        WshNetwork.AddWindowsPrinterConnection "\\atlantis\HP2200ops"
        WshNetwork.SetDefaultPrinter "\\atlantis\HP2200ops" 

Elseif InStr(strGroups, exec)<>0 Then

' WScript.Echo("Made it To Corp Exec")
        WshNetwork.MapNetworkDrive "o:", "\\dc2a\o"
        WshNetwork.MapNetworkDrive "r:", "\\dc2a\r"
        WshNetwork.AddWindowsPrinterConnection "\\atlantis\HP2200P"   

Elseif InStr(strGroups, it)<>0 Then

 WScript.Echo("made it To IT")
    WshNetwork.MapNetworkDrive "r:", "\\dc2a\r"
    WshNetwork.MapNetworkDrive "o:", "\\dc2a\o"
    WshNetwork.AddWindowsPrinterConnection "\\atlantis\HP2200P"
    WshNetwork.AddWindowsPrinterConnection "\\atlantis\HP2200ops"
'        WshNetwork.SetDefaultPrinter "\\atlantis\HP8000"

 End If
   
' Wscript.Echo "Script is complete"

WScript.quit
regards - tsuji
 
Hi tsuji,
Thanks for the response and the tips, unfortunately the script still does not run properly. I am a member of the it manangement group and the script maps the drives and printers of the administrative group to me. I am not a member of the administrative group. Also the Administrative group is the first group in the If Then Else statement.

Thank you,
Monti
 
mmonti,

Try correct the const where there may be a typo.
Const admin[COLOR=red yellow]i[/color]strative = "cn=tim administrative"

- tsuji
 
tsuji,
Thanks!!! The script now works. I cannot believe I missed a typo.

I have one more question, using the command:

WshNetwork.SetDefaultPrinter "\\server\printer share"

if a user has a local printer attached to their pc is it possible to set that local printer as the default but if they do not have a local printer could the script set a mapped printer as the default?

Once again thank you,
Monti

-Monti
 
mmonti,

At logon time, enumerate the printers and if the printer path points not to a print server (ie not starting with \\), high probability is it a local printer. You can develop a code segment in this direction for setting default printer if exists. (But, the \\ signature is no bullet-proof criteria.)

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top