Map Printer(s) Based on AD Group(s) membership
Using powershell determine the group member of the "Machine" and map any and all printers based on those groups.
Below I use a foreach and a switch statement to loop thru all groups the machine has and base on a matching
group name map those printers and continue threw the rest of the groups. The first printer Queue sent in the list
to the function "MapPrinters" is always set as the default.
To use this script you will require:
Machines that are member of groups (Machines normally grouped by a physical location like a room)
The Print Server in the function "MapPrinters" is set to the target printer server (I will post the multi server version i use later.. alot of typing)
The Ldap Path to the ROOT container of where the machine object are stored
#Print Deploy Script
function GetLdapPath ($mymachine) {
$dom = [ADSI]"LDAP://OU=Computers,DC=domain,DC=local"
$filter = "(&(cn="+ $mymachine +"))"
$ds = new-object System.DirectoryServices.DirectorySearcher($dom,$filter)
$result = $ds.Findall()
$myreturn = $result.get_Item(0).Path
return $myreturn
}
function GetGroups ($mymachine) {
$mygroup = @()
$Machines = [ADSI](""+(GetLdapPath($mymachine))+"")
$Groups = $Machines.memberOf
foreach($group in $groups){
$strGroup = $group.split(',')[0]
$ThisGroup = $strGroup.split('=')[1]
$mygroup += $ThisGroup
}
return $mygroup
}
function MapPrinters ($printers) {
$first = 0
foreach ($printer in $printers) {
$PrinterPath = "\\PrintSVR001\$printer"
$net = new-Object -com WScript.Network
$net.AddWindowsPrinterConnection($PrinterPath)
if ($first -eq 0) {
$net.SetDefaultPrinter($PrinterPath)
}
$first += 1
}
}
try {
$mymachine = get-content env:computername
$mytemp = get-content env:TEMP
$mygroups = Getgroups ($mymachine)
$thisstr = "Machine: " + $mymachine + " Time: " + (Get-Date) + " Groups: " + $mygroups
$thisstr | out-file ($mytemp + "\MapPrinters.log") -append -noclobber
foreach ($group in $mygroups){
switch ($group) {
"LABROOM1" {MapPrinters (("ROOM-PRINTER-3","ROOM-PRINTER-2","ROOM-PRINTER-1"))}
"LIBRARY1" {MapPrinters (("ROOM-PRINTER-2","ROOM-PRINTER-3","ROOM-PRINTER-1"))}
"MATHROOM" {MapPrinters (("ROOM-PRINTER-1","ROOM-PRINTER-2","ROOM-PRINTER-3"))}
"HISTORY" {MapPrinters (("ROOM-PRINTER-6","ROOM-PRINTER-2","ROOM-PRINTER-3"))}
"GARAGE" {MapPrinters (("ROOM-PRINTER-7","ROOM-PRINTER-2","ROOM-PRINTER-3"))}
"LOADINGDOCK" {MapPrinters (("ROOM-LABPRINTER","ROOM-PRINTER-1","ROOM-PRINTER-2","ROOM-PRINTER-3"))}
"HRDEPT" {MapPrinters (("ROOM-PRINTER-2","ROOM-PRINTER-1","ROOM-PRINTER-3","ROOM-PRINTER-4"))}
"FINANCE1" {MapPrinters (("ROOM-PRINTER-4","ROOM-PRINTER-2","ROOM-PRINTER-3","ROOM-PRINTER-1"))}
"NURSING" {MapPrinters (("ROOM-PRINTER-5","ROOM-PRINTER-2","ROOM-PRINTER-3"))}
default {}
}
}
}
catch {
$mymachine = get-content env:computername
$mytemp = get-content env:TEMP
$thisstr = "Machine: " + $mymachine + " Time: " + (Get-Date) + " Error: " + $_
$thisstr | out-file ($mytemp + "\MapPrinters.log") -append -noclobber
#we'll try and add the groups to the log
getgroups | out-file ($mytemp + "\MapPrinters.log") -append -noclobber
}