RE: I got Access Denied (SWbemObjectEx) when I use Put_ Method.

Tech-Archive recommends: Fix windows errors by optimizing your registry



Mr. Guarischi,
It's important to know if the variable strComputer on your script is ALWAYS
valued as "." (local machine) or is intended to let the script work on remote
machines.

It is possible to bind WMI with different credentials (avoiding the use of
runas or similar), but NOT on local machine!

It is ALWAYS possible to create and COMPILE a program tha run what you want
(as runas does) but with the Administrative credentials HARD CODED.
So you don't need to pass them to the program creating an enormous security
hole...

Let us know...

SubnetJO, Italy




"a.guarischi" ha scritto:

Hello,

I can't get out of this problem.
My goal is to automate installation of printers to Windows XP Client. Do do
this i made up a .vbs script and
I know, to access and use WMI in write mode, the user, or the process should
have administrative rights.

So, when i Log on the client i want the printers to be installed to, with
Administrator or other administrator, the .vbs works fine.

The main stuff is make this working although the user who logs on is "just"
a Power User.

So, I use a runas statement that launches a batch file which starts the .vbs
file.
What I get is a .cmd process (in Task Manager) bound with "Administrator",
and a cscript process bound with administrator too.

I thought "Wow, I got it!", but unfortunately what I got is an "Access
Denied" Error (80041003) from SWbemObjectEx when I use the Put_ Method to
create a IP Printer Port.

I tried also to make a fully functional executable instead of a .vbs script,
but the results don't change: still "access denied" error.

How can I make it works?

I post my script (comments are in Italian):

*****

Dim Printer
' (0) = nomeprinter
' (1) = IP
' (2) = Driver
' (3) = Path
' (4) = inf file
' (5) = Action

Dim OggFile, origine, OggTextStream, stringa

Dim OK_IPport, OK_Driver

origine=Wscript.Arguments(0)

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set fso = CreateObject("Scripting.FileSystemObject")
Set OggFile = fso.GetFile (origine)
Set OggTextStream = OggFile.OpenAsTextStream(1)
Set shell = WScript.CreateObject("WScript.Shell")

Do While Not OggTextStream.AtEndOfStream

OK_IPPort = 0
OK_Driver = 0
stringa = OggTextStream.ReadLine
Printer = split(stringa,";")
winfolder = shell.ExpandEnvironmentStrings("%WINDIR%")

'WScript.Echo Printer(0)
'WScript.Echo Printer(1)
'Wscript.Echo Printer(2)
'Wscript.Echo Printer(3)
'WScript.Echo Printer(4)

If Printer(5) = "OK" Then 'non fare nulla

Else

If printer(5) = "Install" Or Left(printer(5),5) = "Wrong" Or
Right(printer(5),11)= "WrongDriver" Or printer(5) = "Delete" Then

'se è sbagliato l'IP, prima tolgo la stampante e poi la ricreo
'oppure se è sbagliato il driver, allo stesso modo cancello la
stampante e poi la ricreo.

If Left(printer(5),5) = "Wrong" Or Right(printer(5),11)=
"WrongDriver" Or Printer(5)= "Delete" Then

'elimino la stampante
Set da_eliminare = objWMIService.ExecQuery _
("Select * from Win32_Printer Where DeviceID = '" & printer(0) & "'")

For Each objPrinter in da_eliminare

objPrinter.Delete_

Next

End If

If Not Printer(5) = "Delete" Then

'controllo se esiste la porta IP

Set colPorts = objWMIService.ExecQuery _
("Select * from Win32_TCPIPPrinterPort where HostAddress = '" &
Printer(1) &"'")

For Each objPort in colPorts

OK_IPport = 1

Next

'Wscript.echo "Porta IP Presente: " & OK_IPport

If OK_IPport = 0 Then 'se non esiste la porta IP la creo

Set objWMIServicePort = GetObject("winmgmts:")
Set objNewPort = objWMIServicePort.Get _
("Win32_TCPIPPrinterPort").SpawnInstance_
objNewPort.Name = "IP_" & Printer(1)
objNewPort.Protocol = 1
objNewPort.HostAddress = Printer(1)
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = True
objNewPort.SNMPCommunity = "public"
objNewPort.Put_

End If


'controllo se esiste già il driver

Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_PrinterDriver Where Name Like '" & Printer(2) &
"%'")

For each objPrinter in colInstalledPrinters

OK_Driver = 1

Next

If OK_Driver = 0 Then ' se non esiste il driver, lo installo
usando path e inf indicati nel file di testo.

Set objDriver = objWMIService.Get("Win32_PrinterDriver")
objDriver.Name = Printer(2)
objDriver.SupportedPlatform = "Windows NT x86"
objDriver.Version = "3"

If Not printer(3) = "Builtin" Then

objDriver.FilePath = Printer(3)
objDriver.InfName = Printer(3) & "\" & Printer(4)

Else

objDriver.FilePath = winfolder & "\INF"
objDriver.InfName = winfolder & "\INF\ntprint.inf"

End If

intResult = objDriver.AddPrinterDriver(objDriver)

WScript.Echo "Nome Stampante: " & Printer(0)
Wscript.Echo "Nome Driver: " & ObjDriver.Name
WScript.Echo "Percorso Inf: " & objDriver.FilePath
WScript.Echo "Inf file: " & objDriver.InfName
WScript.Echo "Codice errore installazione Driver: " & intResult

End If

'aggiungo la stampante

Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_

objPrinter.DriverName = Printer(2)
objPrinter.PortName = "IP_" & Printer(1)
objPrinter.DeviceID = Printer(0)
objPrinter.Shared = False
objPrinter.Put_

End If

End If

End If

Loop

' cancello le porte IP non utilizzate

Set objDictionary = CreateObject("Scripting.Dictionary")

Set all_printers = objWMIService.ExecQuery _
("Select * from Win32_Printer")

For Each objPrinter in all_printers

If Not objDictionary.Exists(objPrinter.PortName) Then

objDictionary.Add objPrinter.PortName, objPrinter.PortName

End If

Next

Set colPorts = objWMIService.ExecQuery _
("Select * from Win32_TCPIPPrinterPort")

For Each objPort in colPorts

If objDictionary.Exists(objPort.Name) Then

Else

ObjPort.Delete_

End If

Next

OggTextStream.close


****end script


The argument passed to the .vbs file is a plain text file that contains what
to do with printers. I post here an example:

P00046_006;172.16.46.235;SHARP AR-160 PCL5e;Drivers\sharp_ar160;ar852enu.inf
P00046_004;144.144.144.144;HP LaserJet 1100;Drivers\hp1100;hp201ip5.inf
P00046_100;172.16.100.100;HP LaserJet III;Builtin;Builtin

****end file

Any comment or suggests is highly appreciated.
Thanks in advance.

A.


.



Relevant Pages

  • Re: Re: Running an OU policy only after a previous OU policy com
    ... > Printers with .vbs logon scripts because regular users don't ... In the Computer Configuration ... > Script and in the User Configuration I put the Same as a Logon ...
    (microsoft.public.win2000.group_policy)
  • Re: printuientry
    ... you could put it into a logon script. ... The error did not occur as a network administrator. ... I've tried several variations on the command syntax and can not get it ... >> printer is already defined on a "local port", ...
    (microsoft.public.win2000.printing)
  • Re: Domain Logon Script Advice
    ... users printers and network shares based upon which Active Directory OU ... I happy with deploying the script via GPO etc, ... A very basic VBScript logon script to map the same printer and shares to all ...
    (microsoft.public.scripting.wsh)
  • Re: Questions about GetRef() and ASP, etc.
    ... > stack does NOT extend into a called procedure's scope. ... > script requires catch-all error handling, however, it is often best to set ... > unexposed functions, mix VBS and JS, etc. WSCs are fairly well documented ... > The Err object appears to be misdocumented in the MS VBS documentation. ...
    (microsoft.public.scripting.vbscript)
  • Re: Domain Users to have Local Admin rights
    ... make sure that your script works. ... this computer as local administrator and insert USB drive. ... startup script (e.g. OU policy or Default Domain Group Policy). ... Now close this windows and click on ...
    (microsoft.public.windows.server.security)