Author Posts

July 6, 2016 at 10:04 pm

Hey guys
im total newbie and got this from the web; struggeling now with put(). It says "Connection refused". WMI installed;DCOM activated..

####################################################
# Change these values to the appropriate values in your environment
$ComputerName = "windows01"
$PrinterIP = “11.10.10.10”
$PrinterPort = “9101”
$PrinterPortName = “IP_” + $PrinterIP
$DriverName = “Sharp MX-3100N – scriptinstalled”
$DriverPath = “\\localhost\share\MX3100\German\PCL6\64bit”
$DriverInf = “\\localhost\share\MX3100\German\PCL6\64bit\sn0emdeu.inf”
$PrinterCaption = “SWLaser2”
####################################################

### ComputerList Option 1 ###
$ComputerList = @(“windows01”)

### ComputerList Option 2 ###
# $ComputerList = @()
# Import-Csv “C:\Temp\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
}
####################################################

Thanks!

edit:

[otherLANG]Ausnahme beim Aufrufen von "Put" mit 0 Argument(en): "Allgemeiner Fehler "[/otherLANG]
In C:\Users\windows\Desktop\addprinter.ps1:57 Zeichen:1
+ $Printer.Put()
+ ~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

  • This topic was modified 2 years, 1 month ago by  Saints Criz. Reason: error

July 7, 2016 at 1:24 am

ensure that you are able to connect to wmi as that is what the error is indicating and there is no error logic to address any failures.
Try this and see if it succeeds or fails

gwmi win32_Printer -ComputerName Server1

July 9, 2016 at 3:20 pm

Thank you for your answer Jonathan! The connection to wmi works, it shows me a list with my printers. Any ideas?

July 11, 2016 at 1:46 am

The next step is to add some basic error handling logic to try and narrow down the issue.
This breaks your functions into two try groups to handle the initial connection to the remote system and then the code that uses the connection. Based on the error returned you will know which function is having the issue and which code block to look at.

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

Function InstallPrinterDriver {
Param ($DriverName, $DriverPath, $DriverInf, $ComputerName)
Try{
    $wmi = [wmiclass]”\\$ComputerName\Root\cimv2:Win32_PrinterDriver”
    $wmi.psbase.scope.options.enablePrivileges = $true
    $wmi.psbase.Scope.Options.Impersonation = [System.Management.ImpersonationLevel]::Impersonate
}Catch{Throw "InstallPrinterDriver Failed to Connect to $ComputerName"}
Try{
    $Driver = $wmi.CreateInstance()
    $Driver.Name = $DriverName
    $Driver.DriverPath = $DriverPath
    $Driver.InfName = $DriverInf
    $wmi.AddPrinterDriver($Driver)
    $wmi.Put()
}Catch{Throw "InstallPrinterDriver Failed to write the changes to wmi"}
}

Function CreatePrinter {
param ($PrinterCaption, $PrinterPortName, $DriverName, $ComputerName)
Try{
    $wmi = ([WMIClass]”\\$ComputerName\Root\cimv2:Win32_Printer”)
}Catch{Throw "CreatePrinter Failed to Connect to $ComputerName"}
Try{
    $Printer = $wmi.CreateInstance()
    $Printer.Caption = $PrinterCaption
    $Printer.DriverName = $DriverName
    $Printer.PortName = $PrinterPortName
    $Printer.DeviceID = $PrinterCaption
    $Printer.Put()
}Catch{Throw "CreatePrinter Failed to write the changes to wmi"}
}