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
- CategoryInfo : NotSpecified: (
Cannot find an overload for “AddPrinterConnection” and the argument count: “1”.
At line:29 char:2
-
$net.AddPrinterConnection($PrinterPath) -
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~- CategoryInfo : NotSpecified: (
, MethodException - FullyQualifiedErrorId : MethodCountCouldNotFindBest
- CategoryInfo : NotSpecified: (
Cannot find an overload for “AddPrinterConnection” and the argument count: “1”.
At line:34 char:2
-
$net.AddPrinterConnection($PrinterPath) -
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~- CategoryInfo : NotSpecified: (
, MethodException - FullyQualifiedErrorId : MethodCountCouldNotFindBest
- CategoryInfo : NotSpecified: (
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)
}