Subject: Open CMD, NSLookup, enter name, return IP
Posted By: mmcdonal Post Date: 6/10/2004 1:19:03 PM
Hi,

   Thanks in advance for the help, I hope.

   I am writing a script that loops through all the printers on a machine, determines if the printer is an old NDPS printer, and if so, creates an instance of the printer as a Network IP printer.

   I need to open CMD, run NSLookup, enter the printer name value passed by the user in an Input Box, get the resulting IP address, and continue with the new installation using the IP address as the new Port Name.

   I want to use the DNS to look up these values rather than create an array or use an Access database or some other data store since the script would be more dynamic.

   Here is the code so far, if it fits:

'Declare variables
Dim strDefault 'whether this is the default printer
Dim strDeviceID 'what the device name is, and the new name built on
Dim strDriverName 'what driver to use
Dim strPortName 'this will hold the NEW PortName from the InputBox
Dim strComputer 'this computer = "."
Dim strIBoxTitle 'holds the value of the NDPS PortName until the DNS name can be entered for strPortName

'this first part of the script goes out and gets each printer one at a time,
'then loops for the next one.
On Error Resume Next
strComputer = "." '= this computer
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Printer",,48)
For Each objItem in colItems
    Wscript.Echo "Default: " & objItem.Default 'gets the default setting for each printer T/F
    Wscript.Echo "DeviceID: " & objItem.DeviceID 'gets the name of the printer model
    Wscript.Echo "DriverName: " & objItem.DriverName 'gets the Windows Driver name
    Wscript.Echo "PortName: " & objItem.PortName 'gets the port name, i.e. USB, LPT, AOC, IP_ etc.
    
    'pass this info to the variables
    strDefault = objItems.Default
    strDeviceID = objItem.DeviceID
    strDriverName = objItem.DriverName
    'strPortName = objItem.PortName not needed here
    strIBoxTitle = objItem.PortName 'holds this for the input box title
    
If Left(strIBoxTitle, 6) = "\\AOC\" Then
        strPortName = InputBox("Please enter the DNS Name for " & strIBoxTitle)
        MsgBox "The NSLookup Function will look for this Printer:" & strPortName
        'function to look up IP of strPortName on the DNS server
        
I NEED CODE HERE...
        
        'installs printers one at a time IF they are NDPS pritners, otherwise it skips them,
        'installs printers and drivers
        'resets one of the IP printers to the default IF its NDPS predecessor was the default
        'does NOT remove the old NDPS printers
        MsgBox "I will now try to add " & strPortName & " as an IP Printer", vbOkOnly
        
        Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
        objPrinter.Default = strDefault 'this should override the old printer, if not, add line here to
        'the old printer to Default = False
        objPrinter.DeviceID   = strDeviceID & " IP"
        objPrinter.DriverName = strDriverName
        objPrinter.PortName   = 'need to create new port name based on table using "IP_XXX.X.X.X"
        objPrinter.Network = True
        objPrinter.Put_
        
        'installs the proper driver? Not sure if needed.
        'Set objWMIService = GetObject("winmgmts:")
        'Set objDriver = objWMIService.Get("Win32_PrinterDriver")
        'objDriver.Name = strDriverName
        'objDriver.SupportedPlatform = "Windows NT x86"
        'errResult = objDriver.AddPrinterDriver(objDriver)
        
    Else
        MsgBox strDeviceID & " is not an NDPS Printer", vbOkOnly
    End if
    'loops back for the next printer
Next

Thanks Folks!

mmcdonal
Reply By: Hal Levy Reply Date: 6/15/2004 12:05:32 PM
So what you want to do is use VB script to automate the installation of printers... I don't think this can be done.


Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
Reply By: janverge Reply Date: 12/19/2006 3:23:00 AM
is there a way for me to identify the IP address of the network printer(s) using C# application.  

I have tried the ManagementObjectSearcher Win32_NetworkAdapterConfiguration but I can only see my own ip address.. same with Win32_Printer.

My goal is to programmatically detect the Network Printer’s IP Address or hostnames based on the printer drivers installed in my PC.

    Can .Net do this?


thank you.


Reply By: mmcdonal Reply Date: 12/19/2006 7:40:26 AM
Wow, this is an old post. You can do this calling WMI as in my original post. What do you use in C# to call WMI? I am sure it is there. You can use WMI to install printers, contrary to what Hal Levy posted way back then.

mmcdonal

Go to topic 38061

Return to index page 287
Return to index page 286
Return to index page 285
Return to index page 284
Return to index page 283
Return to index page 282
Return to index page 281
Return to index page 280
Return to index page 279
Return to index page 278