Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources: Active Threads
Viewing all articles
Browse latest Browse all 8411

Update Printer Mappings And Keep The Default Printer.

$
0
0

Hi all,

 

I have found the following script on another website to update printers and there is a part in that script that it is supposed to keep the default printer.  The only error I get is the following:

Exception calling "SetDefaultPrinter" with "1" argument(s): "There is no printer called "Lex-Finance"."

At \\server1\SYSVOL\domain.org\scripts\remap-printers-printserver.ps1:70 char:67

+                                  (New-Object -ComObject WScript.Network).SetDefaultPrinter <<<< ($default.S

hareName)

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : ComMethodTargetInvocation

 

Another question I had is how can I move multiple print servers in this script? I currently have 2 print servers and I am consolidating to 1 server.  Can I add two old print server names?

 

Thank you very much for all help!

 

Michael

 

This is the script:

-----------------------------------------------------------------------------------------

<#
    .SYNOPSIS
        Logon Script to migrate printer mapping
    .DESCRIPTION
        Logon Script to migrate printer mappings
    .NOTES
        Author: Boe Prox
        Create: 09 NOV 2012
        Modified:
        Version 1.0 - Initial Script Creation
                1.1 Added Header Text for CSV file
                1.2 Added ability to keep default printer - nullpayload
#>
Param (
    $newPrintServer = "svrprint1",
    $PrinterLog = "\\file\share\PrintMigration.csv"
)<#
    #Header for CSV log file:"COMPUTERNAME,USERNAME,PRINTERNAME,RETURNCODE-ERRORMESSAGE,DATETIME,STATUS" | 
        Export-Csv -FilePath $PrinterLog -Encoding ASCII
#>
Try {
    Write-Verbose ("{0}: Checking for printers mapped to old print server" -f $Env:USERNAME)
	$default = Get-WmiObject -Class Win32_Printer -Filter "Default='True'" -ErrorAction Stop
    $printers = @(Get-WmiObject -Class Win32_Printer -Filter "SystemName='\\\\svrprint'" -ErrorAction Stop)
    If ($printers.count -gt 0) {        
        ForEach ($printer in $printers) {
            Write-Verbose ("{0}: Replacing with new print server name: {1}" -f $Printer.Name,$newPrintServer)
            $newPrinter = $printer.Name -replace "svrprint",$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            
                Write-Verbose ("{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 {
                Write-Verbose ("{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
								 }
								 (New-Object -ComObject WScript.Network).SetDefaultPrinter($default.ShareName)
-----------------------------------------------------------------------------------------

Viewing all articles
Browse latest Browse all 8411

Trending Articles