Author Posts

April 2, 2014 at 1:42 pm

I am new to Powershell. I am trying to map network printers based on room name. One of the rooms requires 4 printers. The script works on Windows 7 just fine. If I run it on Windows 8.1, the first printer is successfully mapped, but an error occurs for the remaining three. Here is the error:

Cannot find an overload for "AddPrinterConnection" and the argument count: "1".
At line:24 char:2
+ $net.AddPrinterConnection($PrinterPath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest

Cannot find an overload for "AddPrinterConnection" and the argument count: "1".
At line:29 char:2
+ $net.AddPrinterConnection($PrinterPath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest

Cannot find an overload for "AddPrinterConnection" and the argument count: "1".
At line:34 char:2
+ $net.AddPrinterConnection($PrinterPath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest

Here is the script:

$localPrintServer = "\\LIBRPrinter"
$paperCutServer = "\\rhino"
$paperCutServer2 = "\\hippo"
$user = gc env:username
$computer = gc env:computername
$room = $computer.substring(0,7)
$room2 = $computer.substring(0,9)

##Remove all the network printers

Get-WMIObject -class Win32_Printer | where Network -EQ 'True' | foreach{$_.delete()}


if ($room -match "LIBR105")  ##Microforms area staff computers
{
	## Connect first printer
	$PrinterPath = $paperCutServer2 + "\LIBR-LIBR007-01"
	$net = new-Object -com WScript.Network
	$net.AddWindowsPrinterConnection($PrinterPath); 
	
	## Connect second printer
	$PrinterPath = $localPrintServer + "\LIBR007"
	$net1 = new-Object -com WScript.Network
	$net1.AddPrinterConnection($PrinterPath1)
	
	## Connect third printer
	$PrinterPath2 = $localPrintServer + "\MFbwCopier"
	$net2 = new-Object -com WScript.Network
	$net2.AddPrinterConnection($PrinterPath2)
	
	## Connect fourth printer
	$PrinterPath3 = $localPrintServer + "\MFColorCopier"
	$net3 = new-Object -com WScript.Network
	$net3.AddPrinterConnection($PrinterPath3)
	


 } 

April 2, 2014 at 5:16 pm

You're mixing up AddWindowsPrinterConnection() and AddPrinterConnection(). The former is what you appear to need; AddPrinterConnection() takes 2-5 arguments and is for mapping local ports (such as LPT3) to UNC paths. On a side note, you don't have to create a new instance of WScript.Network for each printer. You can call AddWindowsPrinterConnection() multiple times on the same object.

Alternatively, you can use WMI to add the printer connection, similar to how you removed the existing mappings:

$win32Printer = [wmiclass]'Win32_Printer'

$result = $win32Printer.AddPrinterConnection($PrinterPath)
if ($result.ReturnValue -ne 0)
{
    Write-Error -Exception ([System.ComponentModel.Win32Exception][int]$result.ReturnValue)
}