Map Printer(s) Based on OU location in active directory (VDI)
Using powershell determine the OU location of the "Machine" and map any and all printers based on those locations.
Below I use a switch statement to match based on the OU Name and map those printers. 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 sorted by physical location in OU with unique names (Machines placed in OU by a physical location like a room name)
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 replace with you root dc= clause
#Print Deploy Script
function GetLdapPath ($mymachine) {
$dom = [ADSI]"LDAP://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 GetOU ($mymachine) {
$Machines = [ADSI](""+(GetLdapPath($mymachine))+"")
[string]$Container = $Machines.distinguishedName
#Write-Host $Container
$strOU = $Container.split(',')[1]
#Write-Host "1:" + $strOU
$strOU = $strOU.split('=')[1]
#Write-Host "2:" + $strOU
return $strOU
}
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
$myOU = GetOU ($mymachine)
$thisstr = "VM Machine: " + $mymachine + " Time: " + (Get-Date) + " OU Container: " + $myOU
$thisstr | out-file ($mytemp + "\MapPrinters.log") -append -noclobber
switch ($myOU) {
"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
}