0

I am trying to figure out a way to add a printer to multiple workstations. I found the below script which should be able to accomplish this but I run into an error (Exception calling "Put" with "0" argument(s): "Generic failure ") which I will include below the initial script.

####################################################
# Change these values to the appropriate values in your environment

$PrinterIP = "10.00.00.00"
$PrinterPort = "9100"
$PrinterPortName = "IP_" + $PrinterIP
$DriverName = "Xerox WorkCentre 5955 PS"
$DriverPath = "\\UNC_Path\To\My\Drivers"
$DriverInf = "\\UNC_Path\To\My\Drivers\x2DBRIP.inf"
$PrinterCaption = "Xerox WorkCentre 5955 PS"

$ComputerList = @()
Import-Csv "\\UNC_Path\To\Location\Where\TheCSV\Is\Stored\ComputersThatNeedPrinters.csv" | `
% {$ComputerList += $_.Computer}

Function CreatePrinterPort {
param ($PrinterIP, $PrinterPort, $PrinterPortName, $ComputerName)
$wmi = [wmiclass]"\\$ComputerName\root\cimv2:win32_tcpipPrinterPort"
$wmi.psbase.scope.options.enablePrivileges = $true
$Port = $wmi.createInstance()
$Port.name = $PrinterPortName
$Port.hostAddress = $PrinterIP
$Port.portNumber = $PrinterPort
$Port.SNMPEnabled = $false
$Port.Protocol = 1
$Port.put()
}

Function InstallPrinterDriver {
Param ($DriverName, $DriverPath, $DriverInf, $ComputerName)
$wmi = [wmiclass]"\\$ComputerName\Root\cimv2:Win32_PrinterDriver"
$wmi.psbase.scope.options.enablePrivileges = $true
$wmi.psbase.Scope.Options.Impersonation = `

[System.Management.ImpersonationLevel]::Impersonate
$Driver = $wmi.CreateInstance()
$Driver.Name = $DriverName
$Driver.DriverPath = $DriverPath
$Driver.InfName = $DriverInf
$wmi.AddPrinterDriver($Driver)
$wmi.Put()
}

Function CreatePrinter {
param ($PrinterCaption, $PrinterPortName, $DriverName, $ComputerName)
$wmi = ([WMIClass]"\\$ComputerName\Root\cimv2:Win32_Printer")
$Printer = $wmi.CreateInstance()
$Printer.Caption = $PrinterCaption
$Printer.DriverName = $DriverName
$Printer.PortName = $PrinterPortName
$Printer.DeviceID = $PrinterCaption
$Printer.Put()
}

foreach ($computer in $ComputerList) {
CreatePrinterPort -PrinterIP $PrinterIP -PrinterPort $PrinterPort `
-PrinterPortName $PrinterPortName -ComputerName $computer
InstallPrinterDriver -DriverName $DriverName -DriverPath `
$DriverPath -DriverInf $DriverInf -ComputerName $computer
CreatePrinter -PrinterPortName $PrinterPortName -DriverName `
$DriverName -PrinterCaption $PrinterCaption -ComputerName $computer
}
####################################################

Here are the errors I get for each workstation listed in the .csv file that I use for the $computerlist

Exception calling "Put" with "0" argument(s): "Generic failure "
At I:\PSScriptTools\Rough Network Printer for Multiple Computers Script - NOT WORKING.ps1:53 char:1
    + $Printer.Put()
    + ~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

The script is able to identify the computers listed in the list but I get the above errors and no printer is created on the workstations. Any help would be greatly appreciated.

  • You can use `Invoke-Command` here too. Also, I'd recommend using `rundll32 printui.dll,PrintUIEntry` instead of WMI. It's easier and gives you a nice one liner. If you just execute that code in a PS or CMD promt, you'll get a pop up of your options. – Colyn1337 Jan 21 '15 at 21:17
  • @colyn1337. Thanks. I definitely will look in to using 'code' rundll32 printui.dll, PrintUIEntry 'code'. For now, I would like to get this clumsy script working simply because I'm learning a lot and feel that I'm close. regarding the 'code'Invoke-Command'code', I am not sure how or where to enter that into the current script. I'm a noob and apologize for being so clueless at the moment. I'm learning. – EternalStudent Jan 21 '15 at 23:27
  • @Colyn1337 So, I definitely see how `invoke-command` will work. I am just running into a wall when I try to apply it to the above script properly. I think I need to enter `Invoke-Command -ComputerName $ComputerName -ScriptBlock` but I'm not sure how or where to do it in the above script. Sorry that I'm still learning how to apply code to the comments section. – EternalStudent Jan 22 '15 at 01:47

0 Answers0