Tom
Tom

Reputation: 151

How to fix Unspecified Error trying to programmatically create printer ports?

I am trying to create 100 printer ports using a csv file that has IPAddress. I am spinning through this file using VB 2008 and then calling a function to create the port. The first port gets created fine but when program gets second address and passes it to function gets an unspecified error.

call to the function
MakePort("IP_" & ln, ln)

Private Function MakePort(ByVal lPrinterName As String, ByVal _      lHostAddress As String)
    Dim res As Boolean = False
    Dim objWMIService As WbemScripting.SWbemServices = GetObject("winmgmts:")
    Dim objNewPort = objWMIService.Get("WIN32_TCPIPPrinterPort").SpawnInstance_
    Try
        objNewPort.Name = lPrinterName
        objNewPort.Protocol = 1
        objNewPort.HostAddress = lHostAddress
        objNewPort.PortNumber = "6101"
        objNewPort.SNMPEnabled = False

        objNewPort.Put_()
        res = True
    Catch ex As Exception
        res = False
        MsgBox(ex.Message)
    End Try
    objNewPort.Name = ""
    objNewPort.Protocol = ""
    objNewPort.HostAddress = ""
    objNewPort.PortNumber = ""

    objNewPort = Nothing
    objWMIService = Nothing


    Return res
End Function

On the Line objNewPort.Name = lPrinterName in second pass is where the Unspecified Error occurs. Looking to save someone time by not having them manually type in the ports

Upvotes: 0

Views: 71

Answers (1)

Tom
Tom

Reputation: 151

This actually worked for me. Powershell script.

$PrinterList=get-content C:\scripts\printers.csv 
FOREACH ($ip in $PrinterList) {   
   Print $I $port=[wmiclass]"Win32_TcpIpPrinterPort" 
   $port.psbase.scope.options.EnablePrivileges=$true 
   $newPort=$port.CreateInstance() 
   $newport.name="$ip" 
   $newport.Protocol=1 
   $newport.HostAddress=$ip 
   $newport.PortNumber="6101" 
   $newport.SnmpEnabled=$false 
   $newport.Put()
}

Upvotes: 1

Related Questions