Skip to content

Instantly share code, notes, and snippets.

@iyre
Last active February 20, 2020 22:25
Show Gist options
  • Save iyre/9e8d5fb3966d7ad6068fa9cfc7365d26 to your computer and use it in GitHub Desktop.
Save iyre/9e8d5fb3966d7ad6068fa9cfc7365d26 to your computer and use it in GitHub Desktop.
Consolidate shared printers from various servers
$NewPrintServer = 'PRINT'
$ComObject = New-Object -ComObject WScript.Network
$Map = @{
"4071" = "4071"
"4111" = "4111N"
"5001" = "4111N"
"2640" = "2640N"
}
$Search = @{
Class = "Win32_Printer"
Namespace = "root\CIMV2"
Property = "ServerName, Default, ShareName, Name, Network"
ErrorAction = "Stop"
}
try {
$Printers = Get-WMIObject @Search
}
catch {
throw "Unable to connect to the Win32_Printer Class."
}
if ( $null -ne $Printers ) {
$SharedPrinters = $Printers | Where-Object { $null -ne $_.ShareName }
$DefaultPrinter = $SharedPrinters | Where-Object { $True -eq $_.Default } |
% { Select-String -InputObject $_.Name -Pattern '\d{4}'} |
% { $_.Matches } | % { $Map.Item($_.Value) }
foreach ( $Printer in $SharedPrinters ) {
$Model = Select-String -InputObject $Printer.Name -Pattern '\d{4}' |
% { $_.Matches } | % { $Map.Item($_.Value) }
if ($null -eq $Model) {continue}
$NewName = "\\$NewPrintServer\SHARP MX-$Model"
$ComObject.RemovePrinterConnection($Printer.Name)
$ComObject.AddWindowsPrinterConnection($NewName)
if ($DefaultPrinter -eq $Model) { $ComObject.SetDefaultPrinter($NewName) }
}
}
[Void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($ComObject)
Remove-Variable ComObject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment