Open CMD, NSLookup, enter name, return IP
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
__________________
mmcdonal
Look it up at: http://wrox.books24x7.com
|