Sign in to follow this  
Followers 0
BullGates

Stuck on WMI object creation

17 posts in this topic

#1 ·  Posted (edited)

Hello,

I'm facing a quite strange problem, when I try to query via WMI some hostnames that are connected on my network but they have the port 135 closed. I've tried from adding some error handling routines to check if the port 135 is open, but in every case I get a freeze in the script and after some time it may or not return error, aborting the script when in error. Here's te code, I've tried the vbs way of handling this too but no success:

$s_Machine = "hostname"

Local $LastBootUp, $LocalDateTime, $sec, $uptime, $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20
Dim $objWMIService, $colItems

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $s_Machine & "\root\cimv2"); this is where it get's stucked until an error sometimes

Thanks

Edited by BullGates

[topic="51913"]Restrict USB Storage usage to group membership[/topic] * [topic="48699"]Using nircmd library[/topic] * Some admin notes

Share this post


Link to post
Share on other sites



Hello,

I'm facing a quite strange problem, when I try to query via WMI some hostnames that are connected on my network but they have the port 135 closed. I've tried from adding some error handling routines to check if the port 135 is open, but in every case I get a freeze in the script and after some time it may or not return error, aborting the script when in error. Here's te code, I've tried the vbs way of handling this too but no success:

$s_Machine = "hostname"

Local $LastBootUp, $LocalDateTime, $sec, $uptime, $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20
Dim $objWMIService, $colItems

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $s_Machine & "\root\cimv2"); this is where it get's stucked until an error sometimes

Thanks

For good WMI sample code try the AutoIt Scriptomatic in my signature.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

For good WMI sample code try the AutoIt Scriptomatic in my signature.

Thanks, but it still does not work. It allways gets stucked on the "ObjGet" but only for the computers using VPN where WMI port is not available.


[topic="51913"]Restrict USB Storage usage to group membership[/topic] * [topic="48699"]Using nircmd library[/topic] * Some admin notes

Share this post


Link to post
Share on other sites

Thanks, but it still does not work. It allways gets stucked on the "ObjGet" but only for the computers using VPN where WMI port is not available.

Hi,

you can use TCPNameToIP and TCPConnect to check, wether you can connect on port 135 beforehand. See help.

;-))

Stefan

Share this post


Link to post
Share on other sites

Hello Stefan, that was what I meant with "I've tried from adding some error handling routines to check if the port 135 is open, but in every case I get a freeze in the script and after some time it may or not return error".

Now I've found something to specify a timeout with the object creation but still I have the same problem.

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

"wbemConnectFlagUseMaxWait

128 (0x80)

The ConnectServer call is guaranteed to return in 2 minutes or less. Use this flag to prevent your program from hanging indefinitely if the connection cannot be established."

I may have to check for ip ranges and exclude those hostnames, wich is not a nice solution. :|


[topic="51913"]Restrict USB Storage usage to group membership[/topic] * [topic="48699"]Using nircmd library[/topic] * Some admin notes

Share this post


Link to post
Share on other sites

Hello Stefan, that was what I meant with "I've tried from adding some error handling routines to check if the port 135 is open, but in every case I get a freeze in the script and after some time it may or not return error".

Now I've found something to specify a timeout with the object creation but still I have the same problem.

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

"wbemConnectFlagUseMaxWait

128 (0x80)

The ConnectServer call is guaranteed to return in 2 minutes or less. Use this flag to prevent your program from hanging indefinitely if the connection cannot be established."

I may have to check for ip ranges and exclude those hostnames, wich is not a nice solution. :|

Hi,

so you tried:

Local $LastBootUp, $LocalDateTime, $sec, $uptime, $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20
Dim $objWMIService, $colItems
$s_Machine = "hostname"
$g_IP = TCPNameToIP ($s_Machine)

TCPStartUp()

$socket = TCPConnect( $g_IP, 135 )
If $socket <> -1 Then
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $s_Machine & "\root\cimv2"); this is where it get's stucked 
    ;any other code
EndIf

;-))

Stefan

P.S: The TCPConnect will have a default timeout of 100 ms! See help

Share this post


Link to post
Share on other sites

Hi,

so you tried:

Local $LastBootUp, $LocalDateTime, $sec, $uptime, $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20
Dim $objWMIService, $colItems
$s_Machine = "hostname"
$g_IP = TCPNameToIP ($s_Machine)

TCPStartUp()

$socket = TCPConnect( $g_IP, 135 )
If $socket <> -1 Then
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $s_Machine & "\root\cimv2"); this is where it get's stucked 
    ;any other code
EndIf

;-))

Stefan

P.S: The TCPConnect will have a default timeout of 100 ms! See help

Yes as I told I've tried that but with the TCPStartUP() function before the TCPNameToIP or else you get error everytime. When I use the tcpconnect function it "freezes" my script:

+>14:06:25 AutoIt3Wrapper Finished

>Exit code: 0 Time: 568.807


[topic="51913"]Restrict USB Storage usage to group membership[/topic] * [topic="48699"]Using nircmd library[/topic] * Some admin notes

Share this post


Link to post
Share on other sites

Even with a timeout set it hangs for more than 5 minutes, this is insane.

Opt("TCPTimeout",1000)

[topic="51913"]Restrict USB Storage usage to group membership[/topic] * [topic="48699"]Using nircmd library[/topic] * Some admin notes

Share this post


Link to post
Share on other sites

I don't really get this, now I was trying to exlude that IP to avoid the hangs, but even with a simple TCPNameToIP over that hostname the script hangs.

Might this be a bug in Autoit?


[topic="51913"]Restrict USB Storage usage to group membership[/topic] * [topic="48699"]Using nircmd library[/topic] * Some admin notes

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Hi,

there must be something completely wrong with your code.

Just try this one only for debugging:

Opt ("TCPTimeout", 50)
$s_Machine = "128.0.0.1"

TCPStartUp()
$g_IP = $s_Machine
$socket = TCPConnect( $g_IP, 135 )
If $socket <> -1 Then
    MsgBox (0, $socket, "success")
Else
    MsgBox (0, $socket, "unsuccessfull")
EndIf

I got

>Running:(3.3.0.0):C:\Programme\AutoIt3\autoit3.exe "C:\Programme\AutoIt3\Examples\socket.au3"

+>15:48:51 AutoIT3.exe ended.rc:0

+>15:48:52 AutoIt3Wrapper Finished

>Exit code: 0 Time: 27.061

as result with msgbox unsuccessfull.

>_<

P.S: you might have a dns lookup problem? If you know ip, just try the ip first for debugging!

Edited by 99ojo

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Hi,

there must be something completely wrong with your code.

Just try this one only for debugging:

Opt ("TCPTimeout", 50)
$s_Machine = "128.0.0.1"

TCPStartUp()
$g_IP = $s_Machine
$socket = TCPConnect( $g_IP, 135 )
If $socket <> -1 Then
    MsgBox (0, $socket, "success")
Else
    MsgBox (0, $socket, "unsuccessfull")
EndIf

I got

>Running:(3.3.0.0):C:\Programme\AutoIt3\autoit3.exe "C:\Programme\AutoIt3\Examples\socket.au3"

+>15:48:51 AutoIT3.exe ended.rc:0

+>15:48:52 AutoIt3Wrapper Finished

>Exit code: 0 Time: 27.061

as result with msgbox unsuccessfull.

>_<

P.S: you might have a dns lookup problem? If you know ip, just try the ip first for debugging!

I get unsuccessfull and takes a lot of time:

>Running:(3.3.0.0):C:\Program Files\AutoIt3\autoit3.exe "c:\temp\teste.au3"

+>15:07:37 AutoIT3.exe ended.rc:0

+>15:07:38 AutoIt3Wrapper Finished

>Exit code: 0 Time: 192.704

If I use an ip from another computer on the LAN it works and I get a successfull msg box, just for those I get that!

>Running:(3.3.0.0):C:\Program Files\AutoIt3\autoit3.exe "c:\temp\teste.au3"

+>15:09:56 AutoIT3.exe ended.rc:0

+>15:09:57 AutoIt3Wrapper Finished

>Exit code: 0 Time: 2.718

I'm installing Autoit beta to test if it works.

Edited by BullGates

[topic="51913"]Restrict USB Storage usage to group membership[/topic] * [topic="48699"]Using nircmd library[/topic] * Some admin notes

Share this post


Link to post
Share on other sites

I get unsuccessfull and takes a lot of time:

>Running:(3.3.0.0):C:\Program Files\AutoIt3\autoit3.exe "c:\temp\teste.au3"

+>15:07:37 AutoIT3.exe ended.rc:0

+>15:07:38 AutoIt3Wrapper Finished

>Exit code: 0 Time: 192.704

If I use an ip from another computer on the LAN it works and I get a successfull msg box, just for those I get that!

>Running:(3.3.0.0):C:\Program Files\AutoIt3\autoit3.exe "c:\temp\teste.au3"

+>15:09:56 AutoIT3.exe ended.rc:0

+>15:09:57 AutoIt3Wrapper Finished

>Exit code: 0 Time: 2.718

I'm installing Autoit beta to test if it works.

Hi,

what happens in DOS Box (cmd.exe) with:

ping <hostname>

;-))

Stefan

Share this post


Link to post
Share on other sites

C:\temp>ping hostname

Pinging hostname.dns.pt [10.121.18.3] with 32 bytes of data:

Reply from 10.121.18.3: bytes=32 time=31ms TTL=123

Reply from 10.121.18.3: bytes=32 time=32ms TTL=123

Reply from 10.121.18.3: bytes=32 time=34ms TTL=123

Reply from 10.121.18.3: bytes=32 time=32ms TTL=123


[topic="51913"]Restrict USB Storage usage to group membership[/topic] * [topic="48699"]Using nircmd library[/topic] * Some admin notes

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Hi,

further debugging:

$s_Machine = "hostname"
$begin = TimerInit ()
TCPStartUp()
$g_IP = TCPNameToIP ($s_Machine)
MsgBox (0,"", $g_IP & @CRLF & TimerDiff ($begin)); here you should see a msgbox with ip and time for getting ip
$begin = TimerInit ()
$socket = TCPConnect( $g_IP, 135 )
MsgBox (0,"", TimerDiff ($begin))

In my network, lan with GBit and 100 MBit networkspeed, it took about 6 ms to get ip and round about 11 ms to make tcpconnect on running machines.

Otherwise 5000 for non existent machine and 11 ms for tcpconnect.

;-))

Stefan

Edited by 99ojo

Share this post


Link to post
Share on other sites

thanks for you patience and help!

I get 4ms for a near hostname, if I use the computers with problems I get the same 4ms now (strange!!)

now I could run the previous tests and they just run fine, now I'm again experiencing the same problem - I guess something is wrong on the network.


[topic="51913"]Restrict USB Storage usage to group membership[/topic] * [topic="48699"]Using nircmd library[/topic] * Some admin notes

Share this post


Link to post
Share on other sites

thanks for you patience and help!

I get 4ms for a near hostname, if I use the computers with problems I get the same 4ms now (strange!!)

now I could run the previous tests and they just run fine, now I'm again experiencing the same problem - I guess something is wrong on the network.

Maybe you should create a while loop with a ping to the critical pc's. Maybe the vpn tunnel is closed by the clients after a while and you couldn't open a tunnel to them.

$i = 0
While $i < 5400  ; While loop is running for 15 min
    $file = FileOpen ("c:\temp\test.txt",1)
    FileWriteLine ($file, Ping ("hostname"))
    FileClose ($file)
    sleep (10000) ; wait 10 sec
    $i += 1
WEnd

;-))

Stefan

Share this post


Link to post
Share on other sites

ok, thank you once again Stefan!

Regards,

Paulo


[topic="51913"]Restrict USB Storage usage to group membership[/topic] * [topic="48699"]Using nircmd library[/topic] * Some admin notes

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