Yes Jrv, you are right in a sense that I must install the device driver to the remote computer before adding printer via PS script and the script works if I do so.
There is no need to involve printer's vendor in this as I mentioned, the script works on my local computer return code 0. It also works to the remote computer if the driver is already installed to it returning code 87 i.e. adds the printer successfully to
remote computer but still show return code 87.
I will share the script below, again please note that the script works to
- local computer with no driver installed return code 0.
- Remote computer with driver installed already but return code 87
The script fails to work to the remote computer where the driver is not installed but if I installed the driver manually it follow the above point no.2
## Declearing variables
$PrinterIP = "10.3.x.x"
$PrinterPort = "9100"
$PrinterPortName = "IP_" + $PrinterIP
$DriverName = "HP Color LaserJet 5550 PCL 6"
$DriverPath = "\\MyComp\HP 5550"
$DriverInf = "\\MyComp\HP 5550\hpc5550c.inf"
$PrinterCaption = "HP Color LaserJet 5550 PCL 6 - 3290"
## feeding target computers from txt file
$ComputerList = Get-Content "C:\Test.txt"
## Declearing Functions
Function CreatePrinterPort {
param ($PrinterIP, $PrinterPort, $PrinterPortName, $ComputerName)
$wmi = [wmiclass]"\\$ComputerName\root\cimv2:win32_tcpipPrinterPort"
$wmi.Scope.Options.Authentication = [System.Management.AuthenticationLevel]::PacketPrivacy
$Port = $wmi.createInstance()
$Port.name = $PrinterPortName
$Port.hostAddress = $PrinterIP
$Port.portNumber = $PrinterPort
$Port.SNMPEnabled = $true
$Port.Protocol = 1
$Port.put()
}
Function InstallPrinterDriver {
Param ($DriverName, $DriverPath, $DriverInf, $ComputerName)
$wmi = [wmiclass]"\\$ComputerName\Root\cimv2:Win32_PrinterDriver"
$wmi.Scope.Options.Authentication = [System.Management.AuthenticationLevel]::PacketPrivacy
$wmi.Scope.options.enablePrivileges = $true
$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()
}
##Main Script
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
}
As I mentioned earlier, for AddPrinterDriver method we must use SeLoadDriverPrivilege to
load and unload a device driver. This can be seen from following the link below
https://msdn.microsoft.com/en-us/library/aa384771(v=vs.85).aspx
Finally, I can see the following script to again access to SeLoadDriverPrivilege in VB.
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True
What I fail to find out and need help is to do the above same in PowerShell.
I strongly believe that if we could be able to find out the above line in PowerShell. The Script will work even if the driver is not manually installed to the remote computer because this script will grab the driver and installs it.