Important Notice: On February 29th, this community was put into read-only mode. All existing posts will remain but customers are unable to add new posts or comment on existing. Please feel to join our Community Discord for any questions and discussions.

Add Printers with PowerShell

Dear 

I run this PowerShell command after change the values which is required but I got this error 

Exception calling "Put" with "0" argument(s): "Generic failure "
At C:\Users\11120\Desktop\Install Network Printer.ps1:55 char:9
+ $Printer.Put()
+ ~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

 

Can you help me please . 

 

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

    $PrinterIP = "10.10.10.10"
    $PrinterPort = "9100"
    $PrinterPortName = "IP_" + $PrinterIP
    $DriverName = "KONICA MINOLTA bizhub C35P PS"
    $DriverPath = "\\UNC_Path\To\My\Drivers"
    $DriverInf = "\\UNC_Path\To\My\Drivers\KOBJQA__.inf"
    $PrinterCaption = "Konica Minolta C35P"
    ####################################################

    ### ComputerList Option 1 ###
    # $ComputerList = @("lana", "lisaburger")

    ### 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
    }
    ####################################################
1

Comments

1 comment
Date Votes