|
| |||||||||
| Tags: drives, map, printers, vbs |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| map drives and install printers via vbs
I have this script that do following at logon: 1. remove all existing network drives 2. add new drives base on group membership 3. delete all network printers 4. add network printers base on group membership (these groups represent phisical location of users) all works well but it is slightly slowing logon process and I'd like to add statment for both printers and map drives to check if they exist and if they do then do nothing and skip to next part of the script. here is the script Set objNetwork = CreateObject("Wscript.Network") Set colDrives = objNetwork.EnumNetworkDrives For i = 0 to colDrives.Count-1 Step 2 objNetwork.RemoveNetworkDrive colDrives.Item(i), True, True Next wscript.sleep 300 Set objSysInfo = CreateObject("ADSystemInfo") Set objNetwork = CreateObject("Wscript.Network") strUserPath = "LDAP://" & objSysInfo.UserName Set objUser = GetObject(strUserPath) For Each strGroup in objUser.MemberOf strGroupPath = "LDAP://" & strGroup Set objGroup = GetObject(strGroupPath) strGroupName = objGroup.CN Select Case strGroupName Case "UserGroup1" objNetwork.MapNetworkDrive "F:", "\\ServerName\share",True |
|
#2
| |||
| |||
| Re: map drives and install printers via vbs
Some quick comments to speed up the script. First, you enumerate the user groups twice. You could easily combine so you only do this once. Even better, don't bind to each group object. This slows down the script considerably. Instead use the full Distinguished Name's in you Select Case statements. Perhaps similar to: For Each strGroup In objUser.memberOf Select Case LCase(strGroup Case "cn=sales,ou=west,dc=mydomain,dc=com" objNetwork.MapNetworkDrive "F:", "\\ServerName\share",True objNetwork.AddWindowsPrinterConnection strUNCPrinter1 objNetwork.AddWindowsPrinterConnection strUNCPrinter2 objNetwork.AddWindowsPrinterConnection strUNCPrinter3 objNetwork.AddWindowsPrinterConnection strUNCPrinter4 objNetwork.SetDefaultPrinter strUNCPrinter1 Case "cn=engr,ou=west,dc=mydomain,dc=com" ' ... End Select Next -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net -- "RW" <RW@discussions.microsoft.com> wrote in message news:7E013B27-7D25-404C-9C91-33E307AFEC9A@microsoft.com... >I have this script that do following at logon: > 1. remove all existing network drives > 2. add new drives base on group membership > 3. delete all network printers > 4. add network printers base on group membership (these groups represent > phisical location of users) > > all works well but it is slightly slowing logon process and I'd like to > add > statment for both printers and map drives to check if they exist and if > they > do then do nothing and skip to next part of the script. > > here is the script > > Set objNetwork = CreateObject("Wscript.Network") > Set colDrives = objNetwork.EnumNetworkDrives > > For i = 0 to colDrives.Count-1 Step 2 > objNetwork.RemoveNetworkDrive colDrives.Item(i), True, True > Next > > wscript.sleep 300 > > Set objSysInfo = CreateObject("ADSystemInfo") > Set objNetwork = CreateObject("Wscript.Network") > > strUserPath = "LDAP://" & objSysInfo.UserName > Set objUser = GetObject(strUserPath) > > > For Each strGroup in objUser.MemberOf > strGroupPath = "LDAP://" & strGroup > Set objGroup = GetObject(strGroupPath) > strGroupName = objGroup.CN > > Select Case strGroupName > > Case "UserGroup1" > objNetwork.MapNetworkDrive "F:", "\\ServerName\share",True > . > . > . > > Case "UserGroup2" > objNetwork.MapNetworkDrive "G:", "\\ServerName\share",True > . > . > . > > ' There is about 20 of this "case" statments for 20 different groups > > > End Select > Next > > Dim objNet, strUserName > Set objNet = CreateObject("Wscript.Network") > strUserName =objNet.UserName > objNet.MapNetworkDrive "H:", "\\ServerName\share\home\" & strUserName, > True > > strComputer = "." > Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") > > Set colInstalledPrinters = objWMIService.ExecQuery _ > ("Select * from Win32_Printer Where Network = TRUE") > > For Each objPrinter in colInstalledPrinters > objPrinter.Delete_ > Next > > Dim objNetwork, strUNCPrinter1, strUNCPrinter2, strUNCPrinter3, ..... > strUNCPrinter107 'there is close to 100 printers > > strUNCPrinter1 = "\\ServerName\PrinterName001" > strUNCPrinter2 = "\\ServerName\PrinterName002" > strUNCPrinter3 = "\\ServerName\PrinterName003" > . > . > . > strUNCPrinter3 = "\\ServerName\PrinterName00107" > > Set objSysInfo = CreateObject("ADSystemInfo") > Set objNetwork = CreateObject("Wscript.Network") > > strUserPath = "LDAP://" & objSysInfo.UserName > Set objUser = GetObject(strUserPath) > > For Each strGroup in objUser.MemberOf > strGroupPath = "LDAP://" & strGroup > Set objGroup = GetObject(strGroupPath) > strGroupName = objGroup.CN > > Select Case strGroupName > Case "PrinterLocationGroup1" > objNetwork.AddWindowsPrinterConnection strUNCPrinter1 > objNetwork.AddWindowsPrinterConnection strUNCPrinter2 > objNetwork.AddWindowsPrinterConnection strUNCPrinter3 > objNetwork.AddWindowsPrinterConnection strUNCPrinter4 > objNetwork.SetDefaultPrinter strUNCPrinter1 > > Case "PrinterLocationGroup2" > objNetwork.AddWindowsPrinterConnection strUNCPrinter2 > objNetwork.AddWindowsPrinterConnection strUNCPrinter3 > objNetwork.AddWindowsPrinterConnection strUNCPrinter4 > objNetwork.AddWindowsPrinterConnection strUNCPrinter5 > objNetwork.SetDefaultPrinter strUNCPrinter2 > > ' Again more Printer groups here > > > End Select > Next > > |
|
#3
| |||
| |||
| Re: map drives and install printers via vbs
Thnaks Richard for feedback on this I'll look into this but my quick observation is that this won't work because in your example you assuming same group membership is used for printers assigments and network drives mapping which in my case is not true. We map network drives base on different sec groups then printers, net drives are base on role/position of the user and groups used for printers assigments are strictly used to defind phisical location of particular users in the building, and 2 users doing same job will belong to same group i.e. engineering but at the same time they might be located not only on different floor in the building but even in different AD site, different city. so i have to do this either this way or split into 2 scripts, one for net drives and one for printers. In any case at this point my goal is to add something to this script without major rewrite that will: 1. check if nework drive exist and if so skip to printers part of the script 2. check if printers exist and if so end script "Richard Mueller [MVP]" wrote: > Some quick comments to speed up the script. First, you enumerate the user > groups twice. You could easily combine so you only do this once. Even > better, don't bind to each group object. This slows down the script > considerably. Instead use the full Distinguished Name's in you Select Case > statements. Perhaps similar to: > > For Each strGroup In objUser.memberOf > Select Case LCase(strGroup > Case "cn=sales,ou=west,dc=mydomain,dc=com" > objNetwork.MapNetworkDrive "F:", "\\ServerName\share",True > objNetwork.AddWindowsPrinterConnection strUNCPrinter1 > objNetwork.AddWindowsPrinterConnection strUNCPrinter2 > objNetwork.AddWindowsPrinterConnection strUNCPrinter3 > objNetwork.AddWindowsPrinterConnection strUNCPrinter4 > objNetwork.SetDefaultPrinter strUNCPrinter1 > Case "cn=engr,ou=west,dc=mydomain,dc=com" > ' ... > End Select > Next > > -- > Richard Mueller > MVP Directory Services > Hilltop Lab - http://www.rlmueller.net > -- > "RW" <RW@discussions.microsoft.com> wrote in message > news:7E013B27-7D25-404C-9C91-33E307AFEC9A@microsoft.com... > >I have this script that do following at logon: > > 1. remove all existing network drives > > 2. add new drives base on group membership > > 3. delete all network printers > > 4. add network printers base on group membership (these groups represent > > phisical location of users) > > > > all works well but it is slightly slowing logon process and I'd like to > > add > > statment for both printers and map drives to check if they exist and if > > they > > do then do nothing and skip to next part of the script. > > > > here is the script > > > > Set objNetwork = CreateObject("Wscript.Network") > > Set colDrives = objNetwork.EnumNetworkDrives > > > > For i = 0 to colDrives.Count-1 Step 2 > > objNetwork.RemoveNetworkDrive colDrives.Item(i), True, True > > Next > > > > wscript.sleep 300 > > > > Set objSysInfo = CreateObject("ADSystemInfo") > > Set objNetwork = CreateObject("Wscript.Network") > > > > strUserPath = "LDAP://" & objSysInfo.UserName > > Set objUser = GetObject(strUserPath) > > > > > > For Each strGroup in objUser.MemberOf > > strGroupPath = "LDAP://" & strGroup > > Set objGroup = GetObject(strGroupPath) > > strGroupName = objGroup.CN > > > > Select Case strGroupName > > > > Case "UserGroup1" > > objNetwork.MapNetworkDrive "F:", "\\ServerName\share",True > > . > > . > > . > > > > Case "UserGroup2" > > objNetwork.MapNetworkDrive "G:", "\\ServerName\share",True > > . > > . > > . > > > > ' There is about 20 of this "case" statments for 20 different groups > > > > > > End Select > > Next > > > > Dim objNet, strUserName > > Set objNet = CreateObject("Wscript.Network") > > strUserName =objNet.UserName > > objNet.MapNetworkDrive "H:", "\\ServerName\share\home\" & strUserName, > > True > > > > strComputer = "." > > Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") > > > > Set colInstalledPrinters = objWMIService.ExecQuery _ > > ("Select * from Win32_Printer Where Network = TRUE") > > > > For Each objPrinter in colInstalledPrinters > > objPrinter.Delete_ > > Next > > > > Dim objNetwork, strUNCPrinter1, strUNCPrinter2, strUNCPrinter3, ..... > > strUNCPrinter107 'there is close to 100 printers > > > > strUNCPrinter1 = "\\ServerName\PrinterName001" > > strUNCPrinter2 = "\\ServerName\PrinterName002" > > strUNCPrinter3 = "\\ServerName\PrinterName003" > > . > > . > > . > > strUNCPrinter3 = "\\ServerName\PrinterName00107" > > > > Set objSysInfo = CreateObject("ADSystemInfo") > > Set objNetwork = CreateObject("Wscript.Network") > > > > strUserPath = "LDAP://" & objSysInfo.UserName > > Set objUser = GetObject(strUserPath) > > > > For Each strGroup in objUser.MemberOf > > strGroupPath = "LDAP://" & strGroup > > Set objGroup = GetObject(strGroupPath) > > strGroupName = objGroup.CN > > > > Select Case strGroupName > > Case "PrinterLocationGroup1" > > objNetwork.AddWindowsPrinterConnection strUNCPrinter1 > > objNetwork.AddWindowsPrinterConnection strUNCPrinter2 > > objNetwork.AddWindowsPrinterConnection strUNCPrinter3 > > objNetwork.AddWindowsPrinterConnection strUNCPrinter4 > > objNetwork.SetDefaultPrinter strUNCPrinter1 > > > > Case "PrinterLocationGroup2" > > objNetwork.AddWindowsPrinterConnection strUNCPrinter2 > > objNetwork.AddWindowsPrinterConnection strUNCPrinter3 > > objNetwork.AddWindowsPrinterConnection strUNCPrinter4 > > objNetwork.AddWindowsPrinterConnection strUNCPrinter5 > > objNetwork.SetDefaultPrinter strUNCPrinter2 > > > > ' Again more Printer groups here > > > > > > End Select > > Next > > > > > > > |
|
#4
| |||
| |||
| Re: map drives and install printers via vbs
No problem, but you should still need to enumerate the groups only once. Some Case clauses will map drives, other printers. I often map printers according to computer group membership, and then I would need a separate loop to enumerate the computer group memberships. The Case statements are longer and you may need to look up the Distinguished Names when you deal with the memberOf values, but the code will be much faster. The slowest process in any logon script is generally binding to any objects in Active Directory. The fewer such binds the better. -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net -- "RW" <RW@discussions.microsoft.com> wrote in message news:AFD44B05-0102-49FE-8FA8-DE20A54F98FB@microsoft.com... > Thnaks Richard for feedback on this I'll look into this but my quick > observation is that this won't work because in your example you assuming > same > group membership is used for printers assigments and network drives > mapping > which in my case is not true. We map network drives base on different sec > groups then printers, net drives are base on role/position of the user and > groups used for printers assigments are strictly used to defind phisical > location of particular users in the building, and 2 users doing same job > will > belong to same group i.e. engineering but at the same time they might be > located not only on different floor in the building but even in different > AD > site, different city. > > so i have to do this either this way or split into 2 scripts, one for net > drives and one for printers. In any case at this point my goal is to add > something to this script without major rewrite that will: > 1. check if nework drive exist and if so skip to printers part of the > script > 2. check if printers exist and if so end script > > > > "Richard Mueller [MVP]" wrote: > >> Some quick comments to speed up the script. First, you enumerate the user >> groups twice. You could easily combine so you only do this once. Even >> better, don't bind to each group object. This slows down the script >> considerably. Instead use the full Distinguished Name's in you Select >> Case >> statements. Perhaps similar to: >> >> For Each strGroup In objUser.memberOf >> Select Case LCase(strGroup >> Case "cn=sales,ou=west,dc=mydomain,dc=com" >> objNetwork.MapNetworkDrive "F:", "\\ServerName\share",True >> objNetwork.AddWindowsPrinterConnection strUNCPrinter1 >> objNetwork.AddWindowsPrinterConnection strUNCPrinter2 >> objNetwork.AddWindowsPrinterConnection strUNCPrinter3 >> objNetwork.AddWindowsPrinterConnection strUNCPrinter4 >> objNetwork.SetDefaultPrinter strUNCPrinter1 >> Case "cn=engr,ou=west,dc=mydomain,dc=com" >> ' ... >> End Select >> Next >> >> -- >> Richard Mueller >> MVP Directory Services >> Hilltop Lab - http://www.rlmueller.net >> -- >> "RW" <RW@discussions.microsoft.com> wrote in message >> news:7E013B27-7D25-404C-9C91-33E307AFEC9A@microsoft.com... >> >I have this script that do following at logon: >> > 1. remove all existing network drives >> > 2. add new drives base on group membership >> > 3. delete all network printers >> > 4. add network printers base on group membership (these groups >> > represent >> > phisical location of users) >> > >> > all works well but it is slightly slowing logon process and I'd like to >> > add >> > statment for both printers and map drives to check if they exist and if >> > they >> > do then do nothing and skip to next part of the script. >> > >> > here is the script >> > >> > Set objNetwork = CreateObject("Wscript.Network") >> > Set colDrives = objNetwork.EnumNetworkDrives >> > >> > For i = 0 to colDrives.Count-1 Step 2 >> > objNetwork.RemoveNetworkDrive colDrives.Item(i), True, True >> > Next >> > >> > wscript.sleep 300 >> > >> > Set objSysInfo = CreateObject("ADSystemInfo") >> > Set objNetwork = CreateObject("Wscript.Network") >> > >> > strUserPath = "LDAP://" & objSysInfo.UserName >> > Set objUser = GetObject(strUserPath) >> > >> > >> > For Each strGroup in objUser.MemberOf >> > strGroupPath = "LDAP://" & strGroup >> > Set objGroup = GetObject(strGroupPath) >> > strGroupName = objGroup.CN >> > >> > Select Case strGroupName >> > >> > Case "UserGroup1" >> > objNetwork.MapNetworkDrive "F:", "\\ServerName\share",True >> > . >> > . >> > . >> > >> > Case "UserGroup2" >> > objNetwork.MapNetworkDrive "G:", "\\ServerName\share",True >> > . >> > . >> > . >> > >> > ' There is about 20 of this "case" statments for 20 different groups >> > >> > >> > End Select >> > Next >> > >> > Dim objNet, strUserName >> > Set objNet = CreateObject("Wscript.Network") >> > strUserName =objNet.UserName >> > objNet.MapNetworkDrive "H:", "\\ServerName\share\home\" & strUserName, >> > True >> > >> > strComputer = "." >> > Set objWMIService = GetObject("winmgmts:\\" & strComputer & >> > "\root\cimv2") >> > >> > Set colInstalledPrinters = objWMIService.ExecQuery _ >> > ("Select * from Win32_Printer Where Network = TRUE") >> > >> > For Each objPrinter in colInstalledPrinters >> > objPrinter.Delete_ >> > Next >> > >> > Dim objNetwork, strUNCPrinter1, strUNCPrinter2, strUNCPrinter3, ..... >> > strUNCPrinter107 'there is close to 100 printers >> > >> > strUNCPrinter1 = "\\ServerName\PrinterName001" >> > strUNCPrinter2 = "\\ServerName\PrinterName002" >> > strUNCPrinter3 = "\\ServerName\PrinterName003" >> > . >> > . >> > . >> > strUNCPrinter3 = "\\ServerName\PrinterName00107" >> > >> > Set objSysInfo = CreateObject("ADSystemInfo") >> > Set objNetwork = CreateObject("Wscript.Network") >> > >> > strUserPath = "LDAP://" & objSysInfo.UserName >> > Set objUser = GetObject(strUserPath) >> > >> > For Each strGroup in objUser.MemberOf >> > strGroupPath = "LDAP://" & strGroup >> > Set objGroup = GetObject(strGroupPath) >> > strGroupName = objGroup.CN >> > >> > Select Case strGroupName >> > Case "PrinterLocationGroup1" >> > objNetwork.AddWindowsPrinterConnection strUNCPrinter1 >> > objNetwork.AddWindowsPrinterConnection strUNCPrinter2 >> > objNetwork.AddWindowsPrinterConnection strUNCPrinter3 >> > objNetwork.AddWindowsPrinterConnection strUNCPrinter4 >> > objNetwork.SetDefaultPrinter strUNCPrinter1 >> > >> > Case "PrinterLocationGroup2" >> > objNetwork.AddWindowsPrinterConnection strUNCPrinter2 >> > objNetwork.AddWindowsPrinterConnection strUNCPrinter3 >> > objNetwork.AddWindowsPrinterConnection strUNCPrinter4 >> > objNetwork.AddWindowsPrinterConnection strUNCPrinter5 >> > objNetwork.SetDefaultPrinter strUNCPrinter2 >> > >> > ' Again more Printer groups here >> > >> > >> > End Select >> > Next >> > >> > >> >> >> |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "map drives and install printers via vbs" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Printers disappear under Printers & Faxes | flupsie | Hardware Peripherals | 4 | 01-06-2011 10:40 AM |
| How to install printers automatically in Windows Server 2003 using Print Management console | Ander K | Windows Server Help | 6 | 05-12-2010 01:47 AM |
| x64 server - x32 xp clients - users can't install printers | George R | Windows x64 Edition | 3 | 28-05-2009 12:15 PM |
| Drives for HP 940C and J5700 Printers | Wileyuno | Vista Help | 3 | 06-02-2009 07:09 AM |
| Unable to connect to any network drives and printers. | botakz8888 | Windows Vista Network | 5 | 07-12-2008 09:58 AM |