Hi all,
I am a big time newbie with scripting in general and looking for some help.
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?
This script is run from the logon script and run on all users.
Thank you very much for all help!
Michael
This is the script:
Param (
$newPrintServer = “svrprint1”,
$PrinterLog = “\file\share\PrintMigration.csv”
)
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)