Getobject access denied WMI in vbscript

Bonjour,

I have been looking for a while for a way to get my script to work; it's a script that scans the computers on a network and pings as well as retrieves the screen resolution. I am receiving an access denied error on the getobject.

I tried to do the impersonation but it doesn't work, and being a novice in scripting, can you provide me with some information? Thank you

while Not f.AtEndOfStream
sPoste= f.readline
wscript.echo "reading the computer: " & sPoste
'err.clear
set objWMIService = GetObject("winmgmts:\\"& sPoste &"\root\cimv2")
set objWMIService = GetObject("WinMgmts:{impersonationLevel=impersonate}!//"& sPoste &"/root/cimv2")
if err = 0 then
set objPing = objWMIService.ExecQuery("select * from Win32_PingStatus where address = '" & sPoste & "'")
For Each objStatus In objPing
r.Write "ping ok: " & sPoste & vbcrlf
set colItems = GetObject("winmgmts:{impersonationLevel=impersonate}!\\").ExecQuery("Select * From Win32_DesktopMonitor where address = '" & sPoste & "'")
'set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor")
for each objItem in colItems

if objItem.ScreenWidth <> null then
wscript.echo "the value is not null"
'On Error resume next
CurrentRecordSet.open "select Nom_Pos from POSTES where Nom_Pos = '" & sPoste & "' ;" ,oConn
if currentrecordset("Nom_Pos") = null then
wscript.echo "the computer name is: " & currentrecordset("Nom_Pos")
Valeur = currentrecordset("Nom_Pos")
CurrentRecordset.close

Configuration: Windows 7 / Safari 535.11

7 answers

  1. re:

    I'm sorry, I don't understand!

    On my side, it works locally, but I have never tested it remotely.

    Best regards

    Lupin
    0
    1. I wrote in my script, your piece of (Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}\\" & Computer & "\root\cimv2")

      Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where Name='Service'") ) replacing with the right data and still getting the same error...
      0
      1. Always the same mistake, I am scanning a remote host, not locally.
        0
        1. re :

          Observe bien, ta syntaxe est différente :

          La tienne :
          set objWMIService = GetObject("WinMgmts:{impersonationLevel=impersonate}!//" & sPoste & "/root/cimv2")

          La mienne :
          Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & sPoste & "\root\cimv2")

          tu peux aussi essayer de remplacer [ sPoste ] par [ . ] pour localhost

          gère les espaces dans la syntaxe.

          Cdt

          Lupin
          0
          1. I tried and I still have the error on

            set objWMIService = GetObject("WinMgmts:{impersonationLevel=impersonate}!//"& sPoste &"/root/cimv2")

            Access denied
            0
            1. Hello,

              At first glance:

              set colItems = GetObject("winmgmts:{impersonationLevel=impersonate}!\\").ExecQuery("Select * From Win32_DesktopMonitor where address = '" & sPoste & "'")

              should read as follows:

              set colItems = objWMIService.GetObject("winmgmts:{impersonationLevel=impersonate}!\\").ExecQuery("Select * From Win32_DesktopMonitor where address = '" & sPoste & "'")

              here is an example of one of my scripts:

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

              Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where Name='Service'")

              Best regards

              Lupin
              0
              1. while Not f.AtEndOfStream
                sPoste= f.readline
                wscript.echo "reading the station :" & sPoste
                'err.clear
                'set objWMIService = GetObject("winmgmts:\\"& sPoste &"\root\cimv2")
                set objWMIService = GetObject("WinMgmts:{impersonationLevel=impersonate}!//"& sPoste &"/root/cimv2")
                if err = 0 then
                set objPing = objWMIService.ExecQuery("select * from Win32_PingStatus where address = '" & sPoste & "'")
                For Each objStatus In objPing
                r.Write "ping ok : " & sPoste & vbcrlf
                set colItems = GetObject("winmgmts:{impersonationLevel=impersonate}!\\").ExecQuery("Select * From Win32_DesktopMonitor where address = '" & sPoste & "'")
                'set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor")
                for each objItem in colItems
                if objItem.ScreenWidth <> null then
                wscript.echo "the value is not null"
                'On Error resume next
                CurrentRecordSet.open "select Nom_Pos from POSTES where Nom_Pos = '" & sPoste & "' ;" ,oConn
                if currentrecordset("Nom_Pos") = null then
                wscript.echo "the name of the station is :" & currentrecordset("Nom_Pos")
                Valeur = currentrecordset("Nom_Pos")
                CurrentRecordset.close
                0