We help IT Professionals succeed at work.
Get Started
Troubleshooting Question

WMI call to add TCP/IP Printer port

Last Modified: 2021-01-11

I have a vbscript that adds a printer.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True 

Install "" 

sub Install(strIP)
    InstallPrinterPort strIP
end Sub 

strBasePrinter = "Copier"
strPrinterName = "RICOH IM C6000 PCL 6"
strINFPath = "C:\Printers\IMC6000\disk1\OEMSETUP.INF"
strIPPort = "IP_192.168.5.150"
Set objShell = CreateObject("WScript.Shell")
strCommand = "cmd /c rundll32 printui.dll,PrintUIEntry /if /b """ & strBasePrinter & """ /f " & strINFPath & " /r """ & strIPPort & """ /m """ & strPrinterName & """ & /Z"
objShell.Run strCommand, 1, True

Sub InstallPrinterPort(strIP)

    Set colInstalledPorts =  objWMIService.ExecQuery _
        ("Select Name from Win32_TCPIPPrinterPort")
    For each objPort in colInstalledPorts
        If objPort.Name="IP_" & strIP then exit sub 

    Set objNewPort = objWMIService.Get _
    objNewPort.Name = "IP_" & strIP
    objNewPort.Protocol = 1
    objNewPort.HostAddress = strIP
    objNewPort.PortNumber = "9100"
    objNewPort.SNMPEnabled = False
end Sub
I'm trying to do the same now in vb.NET, with these two functions:

Private Function CreatePrinterIP(ByVal IPPort As String, ByVal IPPortName As String)
            Dim searcher As New ManagementObjectSearcher(
                    "SELECT * FROM Win32_TCPIPPrinterPort")

            For Each queryObj As ManagementObject In searcher.Get()

                If queryObj("Name") = IPPortName Then
                    Return 1
                    Exit Function
                End If

            Dim objNewPort As ManagementObject = searcher.Get("Win32_TCPIPPrinterPort").SpawnInstance()

            objNewPort("Name") = IPPortName

            objNewPort("Protocol") = 1

            objNewPort("HostAddress") = IPPort

            objNewPort("PortNumber") = "9100"

            objNewPort("SNMPEnabled") = False

            Return 1
        Catch err As ManagementException
            MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            Return 0
        End Try
    End Function

    Private Sub CreatePrinter(ByVal IPPortName As String, ByVal PrinterName As String, ByVal Location As String, ByVal Drivername As String)
        Dim cmdToSend As String = "rundll32 printui.dll,PrintUIEntry /if /b """ & Printername & """ /f " & Location & " /r """ & IPPortName & """ /m """ & Drivername & """ & /Z"
    End Sub
The CreatePrinterIP function worked once.
I then deleted the IP adress manually. And tried again. Now I receive an error that
The conversion of string Win32_TCPIPPrinterPort to type Integer is invalid.
on line
Dim objNewPort As ManagementObject = searcher.Get("Win32_TCPIPPrinterPort").SpawnInstance()
Does anyone have an idea how to solve this?

Watch Question
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE