Sign in to follow this  
Followers 0
gcue

wmi error?

25 posts in this topic

hello i have this script that works for mostly any pcs in our environment but freezes on one pc in particular. the script seems to freeze for at least like 10 minutes then outputs an error:

here's the script (which again works for mostly all other pcs in our environment)

Local $col2Items, $Net_Model, $Net_Adapter, $Net_IP, $Net_MAC, $Net_DHCP_Server

        $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $asset & "\root\cimv2")

        $colItems = $objWMIService.ExecQuery("SELECT DHCPServer, Description, MACAddress, IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x30)
        $col2Items = $objWMIService.ExecQuery("SELECT MACAddress, NetConnectionID FROM Win32_NetworkAdapter")

        If IsObj($colItems) Then
            For $objItem In $colItems
                $Net_Adapter = ""
                $Net_Model = ""
                $Net_DHCP_Server = ""
                $Net_IP = ""
                $Net_MAC = ""

                $Net_Model = $objItem.Description

                If IsObj($col2Items) Then
                    For $obj2Item In $col2Items
                        If $objItem.MACAddress = $obj2Item.MACAddress And $obj2Item.NetConnectionID <> "" Then
                            $Net_Adapter = $obj2Item.NetConnectionID
                        EndIf
                    Next
                EndIf

                $Net_MAC = $objItem.MACAddress

                For $x = 0 To UBound($objItem.IPAddress) - 1
                    $Net_IP = $objItem.IPAddress($x) & ", "
                Next
                $Net_IP = Remove_Last_Comma($Net_IP)

                If $objItem.DHCPServer <> "255.255.255.255" Then $Net_DHCP_Server = $objItem.DHCPServer

                If $Net_DHCP_Server = "" Then $Net_DHCP_Server = "DISABLED"

                FileWrite($results, '' & @CRLF & _
                        $asset_label & "," & $asset & "," & $Net_Adapter & "," & $Net_Model & "," & $Net_IP & "," & $Net_MAC & "," & $Net_DHCP_Server)

                Dashboard_Results_AddRow($hListView, $asset_label & "|" & $asset & "|" & $Net_Adapter & "|" & $Net_Model & "|" & $Net_IP & "|" & $Net_MAC & "|" & $Net_DHCP_Server, $m)
            Next
        EndIf

here's the error (caught by com error handling):

variable must be of type 'object'

thanks in advance

Share this post


Link to post
Share on other sites



That's actually very common.

Usually paused (non running state) Winmgmt service is the cause.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

the service was "started".. i tried restarting it and it took forever to try the restart and eventually failed. so i restarted the pc and it seems to be working ok now.

how can i check for this in the future and not have to wait 10 min for a comm error?

thanks for your help!

Share this post


Link to post
Share on other sites

Hi gcue,

you could check the service state before running the Script.

There's a Service UDF somewhere in the forum. Or you could use "sc.exe query winmgmt" and look for "running".

Regards,

Hannes

:)


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

looks like the service query fails when its in that state, is that the service used to access the machine remotely (ie: reg, etc)

Edited by gcue

Share this post


Link to post
Share on other sites

Local $rtn = _ServiceRunning('winmgmt')
MsgBox(0, '', $rtn)

;===============================================================================
;
; Description:      Checks to see if a service is running
; Syntax:           _ServiceRunning($sServiceName)
; Parameter(s):     $sServiceName - Name of service to check
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
; Author(s):        SumTingWong
; Documented by:    noone
;
;===============================================================================
Func _ServiceRunning($sServiceName)
    Local $arRet, $hSC, $hService, $bRunning = 0
    $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", "str", "", "str", "ServicesActive", "long", 0x0001)
    If $arRet[0] <> 0 Then
        $hSC = $arRet[0]
        $arRet = DllCall("advapi32.dll", "long", "OpenService", "long", $hSC, "str", $sServiceName, "long", 0x0080)
        If $arRet[0] <> 0 Then
            $hService = $arRet[0]
            $arRet = DllCall("advapi32.dll", "int", "ControlService", "long", $hService, "long", 0x00000004, "str", "")
            $bRunning = $arRet[0]
            DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)
        EndIf
        DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    Return $bRunning
EndFunc


Make yourself sheep and the wolves will eat you - Benjamin Franklin

Share this post


Link to post
Share on other sites

thanks ripdad, that works when you're checking locally.. but what about checking a remote pc? wouldnt i use wmi to check a remote pc?

Local $sWMIService, $objWMIService, $colServiceList
    Local $wbemFlagReturnImmediately = 0x10
    Local $wbemFlagForwardOnly = 0x20
    Local $wbemFlags = $wbemFlagReturnImmediately + $wbemFlagForwardOnly

    Dim $service_info[2]

    $service_found = False

    $sWMIService = "winmgmts:\\" & $asset & "\root\cimv2"
    $objWMIService = ObjGet($sWMIService)
    If IsObj($objWMIService) Then
        $colServiceList = $objWMIService.ExecQuery('SELECT * FROM Win32_Service WHERE Name = "' & $service_name & '"', 'WQL', $wbemFlags)
        For $oSvc In $colServiceList
            $service_found = True

            $service_info[0] = $oSvc.State
            $service_info[1] = $oSvc.StartMode
        Next
        If $service_found = True Then
            Return $service_info
        EndIf
        SetError(1)
    Else
        SetError(1)
    EndIf

Local $rtn = _ServiceRunning('winmgmt')
MsgBox(0, '', $rtn)

;===============================================================================
;
; Description:      Checks to see if a service is running
; Syntax:           _ServiceRunning($sServiceName)
; Parameter(s):     $sServiceName - Name of service to check
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
; Author(s):        SumTingWong
; Documented by:    noone
;
;===============================================================================
Func _ServiceRunning($sServiceName)
    Local $arRet, $hSC, $hService, $bRunning = 0
    $arRet = DllCall("advapi32.dll", "long", "OpenSCManager", "str", "", "str", "ServicesActive", "long", 0x0001)
    If $arRet[0] <> 0 Then
        $hSC = $arRet[0]
        $arRet = DllCall("advapi32.dll", "long", "OpenService", "long", $hSC, "str", $sServiceName, "long", 0x0080)
        If $arRet[0] <> 0 Then
            $hService = $arRet[0]
            $arRet = DllCall("advapi32.dll", "int", "ControlService", "long", $hService, "long", 0x00000004, "str", "")
            $bRunning = $arRet[0]
            DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hService)
        EndIf
        DllCall("advapi32.dll", "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    Return $bRunning
EndFunc

Share this post


Link to post
Share on other sites

Your code looks familiar to me. :)

As the other already said, when the WMI service on remote system is not working properly, the ExecQuery will fail and thus you cannot access the methods/properties of the object.

It makes no sense to test remote wmi service with wmi!

Restart the remote WMI service may help.

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

heheh yep! sorry i ahve my comments that give credit at the top of my script forgot to look up there =)

so it wont return an error, so no way of finding out eh?

Share this post


Link to post
Share on other sites

First parameter for OpenSCManager is MachineName. Doesn't have to be local.

Restarting services requires admin privileges. Forget WMI altogether, that would be my advice.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

is there an easy way to convert from WMI? i have so much im doing in WMI. where can i look up what dll to call and what the parameters are?

Share this post


Link to post
Share on other sites

gcue,

I think I would find out what the issue is with that PC.

Perhaps format/reinstall to get RID of the gremlins. <g>

Lack of WMI Service when called upon, in my opinion, is a red flag.

You could also run a program on that problem PC.

If Not _ServiceRunning('winmgmt') Then _StartService('winmgmt')


Make yourself sheep and the wolves will eat you - Benjamin Franklin

Share this post


Link to post
Share on other sites

yea ive tried restarting the service.. even manually - so i think trancexx made the best suggestion (too much to depend on the existence of a service)- now im just trying to find the dlls to use and look up the syntax *sigh*

Share this post


Link to post
Share on other sites

had to dig up this old post - still having the issue.

the service is in a status where i cannot even restart it. there's a tool to reinstall the service which i've been using to fix the issue on a one to one basis. but i'd like to detect when the service is in this state.

if i do a sc.exe /query "winmgmt" - the service shows like its running normally (compared results with a machine that is working ok). however, i cannot restart the service - it fails. i also tried psservice (pstools) - same results

ive tried trying to capture it this way but neither work

$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootcimv2")
If Not IsObj($objWMIService) Then ;capture attempt 1
If @error then ;capture attempt 2

the hang up happens at the objget because line 2 in the script below never happens

$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootcimv2")
MsgBox(0,"","pass1")

any ideas?

Share this post


Link to post
Share on other sites

My thoughts are.....

Is there a firewall blocking the query?

Does the user account running the script have WMI rights on the computer in question?

Does the hostname have anything other than alphanumeric characters?

Also, see if you can browse the namespace using microsofts cim studio.

Share this post


Link to post
Share on other sites

no firewall.

yes user account has admin rights

hostname doesnt have anything aside from alphanumeric

i jsut tried WMI CIM Studio from WMI Tools (cool tool by the way - connected to other machines) but also hangs and fails to connect on machines in question.

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

Then those machines have deeper problems... What they are is beyond my ability to assit with over a message board.

But yea, the CIM studio is awesome, you can use it to search easy for wmi objects. Then use the "browser" to see what information that object has, and the methods you can call on it. I use it almost daily.

Edited by Colyn1337

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Remote PC already rebooted?

Or:

psexec <reomtepc> net stop Winmgmt
psexec <remotepc> net start Winmgmt

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

im trying to detect the bad state its in - not trying to fix it =)

reason is my script is crashing everytime it comes across a machine like this - i want to capture the bad state and give a soft error like "reinstall wmi service"

thanks for the reply.

Share this post


Link to post
Share on other sites

Ahh ok, then right after your objcreate() statement add an expression like....

If @error Then

Consolewrite("Error found with " & $strcomputer & @lf)

Endif

Share this post


Link to post
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
Sign in to follow this  
Followers 0