Jump to content

Accessing WMI using WBEM Odbc


ptrex
 Share

Recommended Posts

Accessing WMI using WBEM Odbc

This method shown below accesses the WMI classes using a normal ODBC connection.

Prerequisite is that you have the WBEM driver available on your machine.

(I think that this is natively installed in all XP Pro machines ?)

Initialize error handler 
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")

Const $adPromptComplete  = 1

Dim $cnn
Dim $rst 

$cnn =  ObjCreate("ADODB.Connection")
$rst = ObjCreate("ADODB.Recordset")
$dtmConvertedDate = ObjCreate("WbemScripting.SWbemDateTime")

With $cnn 
    .Provider = "MSDASQL.1" 
    .Open ('Provider=MSDASQL.1;Extended Properties="DSN=WBEM Source;DBQ=root\CIMV2;SERVER=;UIDPWDDEFINED=;UID=;PWD=;SYSPROPS=FALSE;NAMESPACES={root\CIMV2,deep}"')
    ConsoleWrite($cnn.ConnectionString & @LF) 
EndWith 

With $rst 
    .ActiveConnection = $cnn 
    .Source = ("SELECT * FROM Win32_NTLogEvent Where Logfile = 'Application' and RecordNumber > 20800'") 
    ; ConsoleWrite(.Source & @CRLF)
    .Open 
EndWith 
    
While not $rst.EOF
    For $i = 0 To $rst.Fields.Count - 1
        ConsoleWrite($i & " " & $rst.Fields($i).Name & " " & $rst.Fields($i).Value  & @CR ) 
    Next
        $rst.MoveNext
            ConsoleWrite(@CR)
WEnd


$cnn.Close
$rst.ActiveConnection.Close

;This is custom error handler
Func MyErrFunc()
  $HexNumber=hex($oMyError.number,8)
  Msgbox(0,"COM Test","We intercepted a COM Error !"       & @CRLF  & @CRLF & _
             "err.description is: "    & @TAB & $oMyError.description    & @CRLF & _
             "err.windescription:"     & @TAB & $oMyError.windescription & @CRLF & _
             "err.number is: "         & @TAB & $HexNumber              & @CRLF & _
             "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
             "err.scriptline is: "     & @TAB & $oMyError.scriptline     & @CRLF & _
             "err.source is: "         & @TAB & $oMyError.source         & @CRLF & _
             "err.helpfile is: "       & @TAB & $oMyError.helpfile       & @CRLF & _
             "err.helpcontext is: "    & @TAB & $oMyError.helpcontext _
            )
SetError(1)  ; to check for after this function returns
Endfunc

Important : Change the Query for you spesific needs, when it fails.

In some case this can open opportunities, in other cases it can be a limitation using this method.

Anyhow, here you get an other scripting technique which you might use (or not ?!)

In combination with my prior PivotTable script, this is a nice complementory scripting approach.

To build multidimentions data views of WMI classes.

Regards,

ptrex

Link to comment
Share on other sites

Be careful, the WMI ODBC are not on all OS supportet.

...

The WMI ODBC Adapter is not available in Windows Server 2003. When you upgrade to Windows Server 2003 from Windows 2000 Server, the WMI ODBC Adapter is not removed if already present on a computer. The ODBC Adapter may be unsupported in subsequent versions of Windows.

...

http://msdn.microsoft.com/en-us/library/aa392726(VS.85).aspx

Link to comment
Share on other sites

@Paulchen,

2 ways to overcome this problem.

1. The WBEM Driver can be installed saperately from the W2K CD value added. Even on a W2003 server.

2. Copy the WBEM DLL to a W2003 server and use the ODBC API to access the driver in AU3.

ODBC API

Don't give up so easily :)

PS : I don't know if it is available on a Vista OS ?

Regards,

ptrex

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...