Jump to content

Querying WMI


Recommended Posts

I am trying to make a little program that does a bit of loadbalancing between terminalservers.

It does this by querying the active sessions of an TS throug WMI.

When I ran it on an server it went well! But on my XP client it gives errors :D

What can I do?

This is the code:

#include <GUIConstants.au3>
$Form1 = GUICreate("Centraal", 290, 113)
$Label1 = GUICtrlCreateLabel("Test", 8, 8, 266, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Maak verbinding met centraal", 50, 40, 179, 25, 0)
$Label2 = GUICtrlCreateLabel("Client status:", 8, 88, 64, 17)
GUICtrlSetFont(-1, 6, 400, 0, "MS Sans Serif")
$Label3 = GUICtrlCreateLabel("Wachtend op gebruiker...", 80, 88, 122, 17)
GUICtrlSetFont(-1, 6, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)

Dim $Result = ""

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button1
            balance()
    EndSelect
WEnd

;Roep WMI query aan en bereken laagste load
Func balance()
    GUICtrlSetData($Label3, "Querying WMI TS2")
    WMI("10.1.1.2")
    $ts2_s = $Result
    GUICtrlSetData($Label3, "Querying WMI TS3")
    WMI("10.1.1.3")
    $ts3_s = $Result
    GUICtrlSetData($Label3, "Querying WMI TS4")
    WMI("10.1.1.4")
    $ts4_s = $Result
    GUICtrlSetData($Label3, "Querying WMI TS5")
    WMI("10.1.1.1")
    $ts5_s = $Result

    GUICtrlSetData($Label3, "Berekenen laagste load...")
    
Select
    Case $ts2_s < $ts3_s AND $ts4_s And $ts5_s
        $vrije_ts = "10.1.1.2"
    Case $ts3_s < $ts2_s And $ts4_s And $ts5_s
        $vrije_ts = "10.1.1.3"
    Case $ts4_s < $ts2_s And $ts3_s And $ts5_s
        $vrije_ts = "10.1.1.4"
    Case $ts5_s < $ts2_s And $ts3_s And $ts4_s
        $vrije_ts = "10.1.1.5"
    Case $ts2_s = $ts3_s And $ts4_s And $ts5_s
        $vrije_ts = "10.1.1.2"
EndSelect
    MsgBox(0,"",$vrije_ts)
    GUICtrlSetData($Label3, "MSTC word gestart...")
EndFunc

;WMI query
Func WMI($strComputer)
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""
    $Output = ""
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_TermService_TerminalServices", "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    For $objItem In $colItems
        $Output = $objItem.ActiveSessions
    Next
    Global $Result = $Output
EndFunc ;==>WMI

Thanks in advance!

Regards,

Sander (Dutch)

Edited by Sander Timmerman
Link to comment
Share on other sites

I'll take a wild stab at it

could it be because you are on a workgroup on your xp machine,

whereas the server you ran it on has a domain server?

I had to use either the @ComputerName macro or enter my computer name for $strComputer

it would crash if IP used instead of computername

you should add an error handler function and use 'Return' instead of Global for WMI()

$ts2_s = WMI("ComputerName")

;....

Return $Output

EndFunc ;==>WMI

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")
Func MyErrFunc()
  ;Return
  $HexNumber=hex($oMyError.number,8)
  Msgbox(0,"COM Error 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 returns1
  Return
Endfunc

I see fascists...

Link to comment
Share on other sites

@Sander Timmerman

The error is the following you WMI cannot connect remotely to the server to get the details from the Objects.

Therefore you need to make a REMOTE WMI call to the server

Some WMI Secrets

This will get you started.

Groeten / Regards

ptrex

Link to comment
Share on other sites

Well, i've added the users to the WMI ACL list and gave them full control, but still it wont work :D

I've tested in on a machine that is in the domain an can ping the server.

I've attached a screenshot with the error.

Can you help me with fixing the code?! I can do a little autoit, but this WMI stuff get's me crazy :P

Thanks in advance

Regards,

Sander (Dutch)

post-13837-1201794350_thumb.jpg

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...