Forum Search:
Forum.Brain-Cluster.com: Brain Cluster Technical Forum
Ultimate forum for Technical Discussions

Home » Microsoft » Windows Server » Active Directory » map drives and install printers via vbs
map drives and install printers via vbs [message #161850] Thu, 08 October 2009 08:03 Go to next message
RW  is currently offline RW
Messages: 52
Registered: July 2009
Member
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
Re: map drives and install printers via vbs [message #161865 is a reply to message #161850] Thu, 08 October 2009 11:44 Go to previous messageGo to next message
rlmueller-nospam  is currently offline rlmueller-nospam  United States
Messages: 292
Registered: July 2009
Senior Member
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
>
>
Re: map drives and install printers via vbs [message #161866 is a reply to message #161865] Thu, 08 October 2009 12:41 Go to previous messageGo to next message
RW  is currently offline RW
Messages: 52
Registered: July 2009
Member
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
> >
> >
>
>
>
Re: map drives and install printers via vbs [message #161880 is a reply to message #161866] Thu, 08 October 2009 14:45 Go to previous message
rlmueller-nospam  is currently offline rlmueller-nospam  United States
Messages: 292
Registered: July 2009
Senior Member
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
>> >
>> >
>>
>>
>>
Previous Topic:Admin tools for Windows 7
Next Topic:System Security Group
Goto Forum:
  


Current Time: Sat Jan 20 08:29:27 MST 2018

Total time taken to generate the page: 0.03758 seconds
.:: Contact :: Home ::Sitemap::.

Powered by: FUDforum 3.0.0RC2.
Copyright ©2001-2009 FUDforum Bulletin Board Software