Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Printer Port script not working.

Status
Not open for further replies.

sparkbyte

Technical User
Sep 20, 2002
879
US
I have gone through several versions of this and cannot seem to figure out why this does not work.

I originaly had this all done in Sub's called from a main body and got rid of the sub's in hopes of trapping any errors but I cannot seem to capture any errors.

Code:
On Error Resume Next

Dim WshNetwork
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputer = WshNetwork.ComputerName

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

strPathtoTextFile = "F:\-- AddPrinter Scripts\"

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & strPathtoTextFile & ";" & _
          "Extended Properties=""text;HDR=YES;FMT=Delimited"""

objRecordset.Open "SELECT * FROM BankPrinters.csv", _
          objConnection, adOpenStatic, adLockOptimistic, adCmdText

  	Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
			If Err.Number <> 0 Then
				WScript.Echo "Bind to Computer Object."
			    WScript.Echo "Error: " & Err.Number
			    WScript.Echo "Error (Hex): " & Hex(Err.Number)
			    WScript.Echo "Source: " &  Err.Source
			    WScript.Echo "Description: " &  Err.Description
		    Err.Clear
			End If

objRecordset.MoveFirst
Do Until objRecordset.EOF
	WScript.Echo "Printer Name: " & vbTab & vbTab & objRecordset.Fields.Item("PrinterName")
	WScript.Echo "Printer IP: " & vbTab & vbTab & objRecordset.Fields.Item("PrinterIP")
	WScript.Echo "Printer Driver: " & vbTab & objRecordset.Fields.Item("PrinterDriver")
	
	Set objNewPort = objWMIService.Get _
	    ("Win32_TCPIPPrinterPort").SpawnInstance_
	objNewPort.Name = "IP_" & objRecordset.Fields.Item("PrinterIP")
	objNewPort.Protocol = 1
	objNewPort.HostAddress = objRecordset.Fields.Item("PrinterIP")
	objNewPort.PortNumber = "9999"
	objNewPort.SNMPEnabled = False
	objNewPort.Put_
		
    objRecordset.MoveNext
Loop

Code:
"PrinterIP","PrinterName","PrinterDriver"
"10.6.102.128","202 BR1_OPS2","HP LaserJet 5"

Thanks

John Fuhrman
Titan Global Services
faq329-6766
 
Are you getting any errors? If you remove the On Error Resume Next what line generates the error?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Here is the output of the script with the On Err. commented out.

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Printer Name: 202 BR1_OPS2
Printer IP: 10.6.102.128
Printer Driver: HP LaserJet 5
F:\-- AddPrinter Scripts\In Progress\InstallPrinters.vbs(47, 2) SWbemObjectEx: Access denied


***** script completed *****


Thanks

John Fuhrman
Titan Global Services
faq329-6766
 
Oh, and the section where it installs the printer was also commented out, so it is failing on the port creation.

Thanks

John Fuhrman
Titan Global Services
faq329-6766
 
Have you tried another WMI connection to the workstation/server that you try to connect to when this error is generated? Could it be WMI is corrupt on that machine or if DCOM is disabled?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Well, you are using WshNetwork.ComputerName to specify the computer to run against which means this is being run locally.

I would assume in that case then that the ID being used does not have the rights needed.

The fact that you are using: "{impersonationLevel=impersonate}!
doesn't really matter, you can't specify security when running locally, so this is ignored.

Try running this code from a remote system and specifying the remote machine name instead of using WshNetwork.ComputerName. Run it while logged on as an ID with admin rights on the remote system.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top