Printer Mapping

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Paul Goffar Paul Goffar 1 year, 9 months ago.

  • Author
    Posts
  • #22821
    Profile photo of Paul Goffar
    Paul Goffar
    Participant

    I'm currently borrowing from one of Bo Prox's scripts to migrate printer shares and it works great! The only thing I'm having trouble with is logic to guarantee that although the print server name changes, the print share remains the default.

    Any ideas?

    Paul

    Function Switch-Printer
    {
    	Param [
    		$oldprintserver,
    		$oldPrintbyIP,
    		$newPrintServer,
    		$PrinterLog = "c:\printers.csv"
    	]
    	
    
    	Try
    	{
    		LogWrite ["{0}: Checking for printers mapped to old print server" -f $Env:USERNAME]
    		$printers = @[Get-WmiObject -Class Win32_Printer | where { $_.Systemname -like "*$oldprintserver*" -or $_.SystemName -like "*$oldPrintbyIP*" }]
    		
    		If [$printers.count -gt 0]
    		{
    			ForEach [$printer in $printers]
    			{
    				LogWrite ["{0}: Replacing with new print server name: {1}" -f $Printer.Name, $newPrintServer]
    				
    				
    				If [$printer.name -like "*$oldprintserver*"] { $newPrinter = $printer.Name -replace $oldprintserver, $newPrintServer }
    				
    				If [$printer.name -like "*$oldPrintbyIP*"] { $newPrinter = $printer.Name -replace $oldPrintbyIP, $newPrintServer }
    				
    				
    				$returnValue = [[wmiclass]"Win32_Printer"].AddPrinterConnection[$newPrinter].ReturnValue
    				If [$returnValue -eq 0]
    				{
    					"{0},{1},{2},{3},{4},{5}" -f $Env:COMPUTERNAME,
    					$env:USERNAME,
    					$newPrinter,
    					$returnValue,
    					[Get-Date],
    					"Added Printer" | Out-File -FilePath $PrinterLog -Append -Encoding ASCII
    					LogWrite ["{0}: Removing" -f $printer.name]
    					$printer.Delete[]
    					"{0},{1},{2},{3},{4},{5}" -f $Env:COMPUTERNAME,
    					$env:USERNAME,
    					$printer.Name,
    					$returnValue,
    					[Get-Date],
    					"Removed Printer" | Out-File -FilePath $PrinterLog -Append -Encoding ASCII
    				}
    				Else
    				{
    					LogWrite ["{0} returned error code: {1}" -f $newPrinter, $returnValue] -Verbose
    					"{0},{1},{2},{3},{4},{5}" -f $Env:COMPUTERNAME,
    					$env:USERNAME,
    					$newPrinter,
    					$returnValue,
    					[Get-Date],
    					"Error Adding Printer" | Out-File -FilePath $PrinterLog -Append -Encoding ASCII
    				}
    			}
    		}
    	}
    	Catch
    	{
    		"{0},{1},{2},{3},{4},{5}" -f $Env:COMPUTERNAME,
    		$env:USERNAME,
    		"WMIERROR",
    		$_.Exception.Message,
    		[Get-Date],
    		"Error Querying Printers" | Out-File -FilePath $PrinterLog -Append -Encoding ASCII
    	}
    	
    	
    }
  • #22823
    Profile photo of Jack Neff
    Jack Neff
    Participant

    Get-Member is your friend! If you do a

    $printer | Get-Member

    you'll notice it has a property called 'Default' (contains true or false) and a method called 'SetDefaultPrinter'. With these two items all you have to do is invoke the SetDefaultPrinter method on the newly created printer if the old printer object's Default parameter was "true".

    if ($returnValue -eq 0){
        if ($printer.default){ Get-WmiObject -Class win32_printer -Filter "Name like '$newprinter'" | Invoke-WmiMethod -Name SetDefaultPrinter }
        ##Write to log##
        $printer.Delete()
    }
    
  • #22825
    Profile photo of Paul Goffar
    Paul Goffar
    Participant

    Jack,

    Thanks a mill, I'm going to try this tonight!

You must be logged in to reply to this topic.