Printer Mapping

Welcome Forums General PowerShell Q&A Printer Mapping

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
3 years, 8 months ago.

  • Author
    Posts
  • #22821

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    Jack,

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

The topic ‘Printer Mapping’ is closed to new replies.