Today I wrote a couple of quick PowerShell Scripts to get shared printers from all computers on the domain, and another one to take the data returned and use it in an OSD Task in SCCM 2012 to create the shares printers when we deploy Windows 7.
The first script is as follows
# By Gary Rice # V1 - 14/05/2013 # Printer information for import needs to be as follows # $Printers = "HP LaserJet 4250 PCL6","Kyocera FS-1030D KX" # $Computers = "MININT-HJ0UIN4","MININT-LR30CVS" # $Sharenames = "MININT-HJ0UIN4-HPLJ4250","MININT-LR30CVS-FS1030D" $OutPut = "" $NL = [Environment]::NewLine $FileName = Get-Date -format "yyyy-MM-dd-HH-mm-ss" $Filename += ".txt" $C = "" $Pstring = "`$Printers = " $Cstring = "`$Computers = " $Sstring = "`$Share = " $OutPut += "Computer,Printer Name,Printer Driver,Printer Share Name" + $NL $OU = "OU=Desktop Computers,DC=test,DC=internal,DC=snowey,DC=com" $DomainComputers = Get-ADComputer -SearchBase $OU -Filter `*` | Select-Object -ExpandProperty Name foreach($Computer in $DomainComputers) { if(!(Test-Connection -Cn $Computer -BufferSize 16 -Count 1 -ea 0 -quiet)) { $OutPut += $Computer + "," + "Error" + $NL } else { $Printers = get-wmiobject -computername $Computer -query "select * from win32_printer" foreach ($Printer in $Printers) { if($Printer.Shared -eq "True") { $OutPut += $Computer + "," + $Printer.name + "," + $Printer.drivername + "," + $Printer.ShareName + $NL $Pstring += $C + "`"" + $Printer.drivername + "`"" $Cstring += $C + "`"" + $Computer + "`"" $Sstring += $C + "`"" + $Printer.ShareName + "`"" $C = "," } } } } $FilenameFN ="Computers-Printers-" + $Filename $Output | Out-File $Env:Temp$FileNameFN Invoke-Expression $Env:Temp$FileNameFN $OutPut = $Pstring + $NL + $Cstring + $NL + $Sstring $FilenameFN ="Computers-printers-OSD-String" + $Filename $Output | Out-File $Env:Temp$FileNameFN Invoke-Expression $Env:Temp$FileNameFN
It’s not very complex, it just goes off and gets all the computers in a specific OU, checks that are active then loops through all the printers and if any are shared it saves the computer name, printer driver name and share name to file. I had to use printer driver name as the Printer name was not always accurate or what it should be due to historical renaming.
Then, the script that I included in the OSD Task Sequence was
# By Gary Rice # V1.1 - 15/05/2013 # Uses `Shared Printers on Domain Computers.PS1` to generate data # To be run as part of SCCM 2012 OSD Task $Count = 0 $Printers = "HP LaserJet 4250 PCL6","Kyocera FS-1030D KX" $Computers = "MININT-HJ0UIN4","MININT-LR30CVS" $Shares = "MININT-HJ0UIN4-HPLJ4250","MININT-LR30CVS-HPLJ4250" foreach ($Computer in $Computers) { if ($Computer -eq $env:computername) { $TargetPrinters = get-wmiobject -computername $Computers[$count] -query "select * from win32_printer" foreach ($Printer in $TargetPrinters) { if ($Printer.name -eq $Printers[$Count]) { $Printer.shared = $true $Printer.sharename = $Shares[$Count] $Printer.put() } } } $Count += 1 }
Again very simple, it takes an array of computers (printer names and share names) and loops through them until it finds a match. It then loops through all the printers on the local machine until it finds one that matches Printer Driver and then it creates the share.
There might be a different / easier way of doing this, but when you need something quick.
Hopefully someone might find them useful (or not).