ofcourse, i have removed most of the printers but left a few in there so you can see whats going on:
First the script that deletes all installed printers (not local) and then maps printers based on computer location (set in the "environmental variables", eg LOCATION=IT1. We used GP to do this) and also maps printers for individual stray PCs:
Code:
on error resume next
Set shell= WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
dim Counter, PrinterPath
dim strResult, strLocation, strComputer
'Remove All Network Printers.
Const EVENT_SUCCESS = 0
Const EVENT_FAILURE = 1
Const EVENT_WARNING = 2
Const EVENT_INFORMATION = 4
function LogtoEvent (MSG,success)
select case success
case 1 shell.LogEvent EVENT_SUCCESS, MSG ' 1 is a success
case 0 shell.LogEvent EVENT_FAILURE, MSG ' 0 is a error
case 2 shell.LogEvent EVENT_WARNING, MSG ' 2 is a warning
case 3 shell.LogEvent EVENT_INFORMATION, MSG ' do not use
end select
end function
Set oPrinters = WshNetwork.EnumPrinterConnections
FOR Counter = 0 to oPrinters.Count - 1
IF mid(oPrinters.Item(Counter), 1, 2) = "\\" THEN
PrinterPath = oPrinters.Item(Counter)
WshNetwork.RemovePrinterConnection PrinterPath, True, True
End If
Next
'Add Network Printers based on Location set in Environment Variables
strLocation = shell.ExpandEnvironmentStrings("%LOCATION%")
strComputer = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
LogtoEvent "Printer STAFF COMPUTER Script Starting for: " & strComputer,1
select case strComputer
case "S21"
addPrinter "\\printserver\Music - Photocopier",true
case "M12"
addPrinter "\\printserver\M11 Mono",true
addPrinter "\\printserver\M11 - Colour - CLP-600",false
case "EGR-OFFICE"
addPrinter "\\printserver\EGR Office - Colour - HP 1510N",true
addPrinter "\\printserver\Main Admin Office - Photocopier",false
addPrinter "\\printserver\Site Manager - Mono - HP1320",false
end select
select case strLocation
case "A7"
addPrinter "\\printserver\A7 - Mono",true
case "ADMINOFFICE"
addPrinter "\\printserver\Main Admin Office - Mono - HP1320",true
addPrinter "\\printserver\Main Admin Office - Photocopier",false
addPrinter "\\printserver\Main Admin Office - Colour - CLP-600",false
addPrinter "\\printserver\Reception - Mono - HP2015",false
case "ART"
addPrinter "\\printserver\Art - Mono - ML-2855",true
addPrinter "\\printserver\Art - Samsung CLP-510 Series",false
case "BURSAR"
addPrinter "\\printserver\Bursar - Mono",true
addPrinter "\\printserver\Main Admin Office - Photocopier",false
addPrinter "\\printserver\EGR Office - Colour - HP 1510N",false
case "STAFFROOM"
addPrinter "\\printserver\Staff Room - Mono - Samsung ML3310",True
addPrinter "\\printserver\Staff Room - Colour",false
end select
function addPrinter(strConnectString,isDefault)
if strConnectString <> "" then
LogtoEvent "Adding printer: " & strConnectString,1
strResult = WshNetwork.AddWindowsPrinterConnection(strConnectString)
if isDefault then
LogtoEvent "Setting printer: " & strConnectString & " as default",1
WshNetwork.SetDefaultPrinter strConnectString
end if
end if
end function
LogtoEvent "Printer STAFF COMPUTER script finished",1
runotherscript "\\LOCATION OF OTHER SCRIPT\Printers_USER.vbs"
Sub runotherscript(name)
Dim oShell
Set oShell = WScript.CreateObject("WSCript.shell")
oShell.run name, 1, true
Set oShell = Nothing
end sub Then the script that is called by the fist script that maps printers based on user group membership:
Code:
'Printer vbs for logon script
On Error Resume Next
Set shell= WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
dim Counter, PrinterPath
dim strResult, strLocation, strComputer
Const EVENT_SUCCESS = 0
Const EVENT_FAILURE = 1
Const EVENT_WARNING = 2
Const EVENT_INFORMATION = 4
function LogtoEvent (MSG,success)
select case success
case 1 shell.LogEvent EVENT_SUCCESS, MSG ' 1 is a success
case 0 shell.LogEvent EVENT_FAILURE, MSG ' 0 is a error
case 2 shell.LogEvent EVENT_WARNING, MSG ' 2 is a warning
case 3 shell.LogEvent EVENT_INFORMATION, MSG ' do not use
end select
end function
wscript.sleep(1000) '1 second
'determines the user who just logged on
Set objSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("Wscript.Network")
strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)
LogtoEvent "Printer STAFF USER Script Starting for: " & objSysInfo.UserName,1
For Each strGroup in objUser.MemberOf
strGroupPath = "LDAP://" & strGroup
Set objGroup = GetObject(strGroupPath)
strGroupName = objGroup.CN
LogtoEvent "Staff Member is in group: " & strGroupName,1
'Mapping of printers by group and set default printer for that group
Select Case strGroupName
Case "All Staff"
objNetwork.AddWindowsPrinterConnection "\\printserver\Reprographics - Mono"
objNetwork.AddWindowsPrinterConnection "\\printserver\Reprographics - Colour"
Case "Dolphin College"
objNetwork.AddWindowsPrinterConnection "\\printserver\Dolphin - Office Mono"
objNetwork.AddWindowsPrinterConnection "\\printserver\Library - Colour - CLP600"
Case "ICT Teachers"
objNetwork.AddWindowsPrinterConnection "\\printserver\Maths ICT Office - Mono"
'==========Add addtional case sections for each group above================
End Select
next
wscript.sleep(100) '0.1 second
LogtoEvent "Printer USER script finished",1