Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Help figuring out Whats wrong with Script

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,58459
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Help figuring out Whats wrong with Script
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Help figuring out Whats wrong with Script - 3/27/2008 5:08:41 AM   
  chiltz

 

Posts: 76
Score: 0
Joined: 6/13/2007
Status: offline
Hi All,

Below script should show me the people who are using a session on a terminal server.  It was working at one point, but now I get an error on line 7:

Error: 0x80041010
Code: 80041010
Source: (null)

Reach has shown me that this is incicates a spelling error or that WMI class doesn't exist.  I don't see any spelling issues....and "\root\CIMV2" definatly exists.  Side note...I have confirmed there are active sessions on the server i am testing...and have tested multiple.



      
 
 
Post #: 1
 
 RE: Help figuring out Whats wrong with Script - 3/27/2008 5:49:11 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
You need to verify that you have the Win32_LogonSession class on the machine that you are running the script against (can't think of any way that you would not have it but just make sure). Next you need to make sure that at the time that you run the script there are some instances of that class where the LogonType is 2 or the LogonType is 10. Basically your query is return ing now instances. You just need to figure out why.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chiltz)
 
 
Post #: 2
 
 RE: Help figuring out Whats wrong with Script - 3/27/2008 6:11:52 AM   
  chiltz

 

Posts: 76
Score: 0
Joined: 6/13/2007
Status: offline
Thats freakin weird...the machines ive been testing on don't have Win32_LogonSession...but they all have TSM???  So I revised my script...now now I have a new error.  Again on line 7.  Error is that "class does not exist".  It doesn't appear to like "colSessons.Count".  I thought ".Count" was a built in thing to count the items in any array or collecton?


      

(in reply to ebgreen)
 
 
Post #: 3
 
 RE: Help figuring out Whats wrong with Script - 3/27/2008 6:13:14 AM   
  chiltz

 

Posts: 76
Score: 0
Joined: 6/13/2007
Status: offline
Side note...anybody know of a more accurate way to check the people logged in either via RDP or Console?

Edit...I only get the error about invalid class if the If statement if the count is 0.  If the Count is more than 0....the script runs fine

< Message edited by chiltz -- 3/27/2008 6:19:08 AM >

(in reply to chiltz)
 
 
Post #: 4
 
 RE: Help figuring out Whats wrong with Script - 3/27/2008 6:26:21 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Your query only returns a collection if their are sessions I believe. This means that if their aren't any sessions no collection is ever created. Try using IsObject instead of .Count.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chiltz)
 
 
Post #: 5
 
 RE: Help figuring out Whats wrong with Script - 3/27/2008 6:41:54 AM   
  chiltz

 

Posts: 76
Score: 0
Joined: 6/13/2007
Status: offline
made below adjustment:

Set colSessions = objWMIService.ExecQuery ("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10")
MsgBox IsObject(colSessions)

Machine1 = returns value "true"  (this is machine that does not have a session and causes colSessions.Count to fail)
Machine2 = returns value "true" (this is the machine that does have a session and works fine with colSessions.Count)

According to IsObject, both of these machines have have a set variable, yet colSessions.Count does not recognize the variable.  I would have thought a Count of 0 would mean no sessions :shrug:

(in reply to ebgreen)
 
 
Post #: 6
 
 RE: Help figuring out Whats wrong with Script - 3/28/2008 6:00:46 AM   
  chiltz

 

Posts: 76
Score: 0
Joined: 6/13/2007
Status: offline
Things I have discovered though testing about script where test machine does not work with .count:

IsNull(colsessions) - returns "False"
IsEmpty(colSessions) - returned "False
IsObject(colSessions) - recturns "True"

So I know that colSessions has a value....but I have no way of figuring out what that value is.  Any ideas?

(in reply to chiltz)
 
 
Post #: 7
 
 RE: Help figuring out Whats wrong with Script - 3/31/2008 12:46:58 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
So without actually figuring out the root issue since I don't have time to right now, wrap the count test in an On Error Resume Next block:

On Error Resume Next
bDoThings = False
if colSessions.Count > 0 Then bDoThings =True
On Error Goto 0

If bDoThings Then
   'Process the recordset here
End If

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chiltz)
 
 
Post #: 8
 
 RE: Help figuring out Whats wrong with Script - 3/31/2008 1:05:41 AM   
  chiltz

 

Posts: 76
Score: 0
Joined: 6/13/2007
Status: offline
updated script is below.  Still gives the same null error.  This is because after processing the first "IF" statement, "bDoThings" has a value of "True", Which in turn should mean that colSessions.Count has a value above 0.


      

(in reply to ebgreen)
 
 
Post #: 9
 
 RE: Help figuring out Whats wrong with Script - 3/31/2008 1:22:57 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
So colSessions has a count of > 0? which line gives you the error then?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chiltz)
 
 
Post #: 10
 
 RE: Help figuring out Whats wrong with Script - 3/31/2008 1:43:53 AM   
  chiltz

 

Posts: 76
Score: 0
Joined: 6/13/2007
Status: offline
Line 16:  For Each objSession in colSessions

Because there is no objSession

(in reply to ebgreen)
 
 
Post #: 11
 
 RE: Help figuring out Whats wrong with Script - 3/31/2008 3:16:31 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
That's really goofy. I must be missing something. What do you see with this change?

bDoThings = False
MsgBox colSessions.Count
if colSessions.Count > 0 Then
bDoThings =True
Else
MsgBox ("No Sessions Exist, either no users or Win32_LogonSession does not exist")
End If
On Error Goto 0

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chiltz)
 
 
Post #: 12
 
 RE: Help figuring out Whats wrong with Script - 3/31/2008 3:32:45 AM   
  chiltz

 

Posts: 76
Score: 0
Joined: 6/13/2007
Status: offline
Error generated from the line added:

Error: Invalid Class
Code: 80041010

The reason there is no session is not because nobody is logged in, but rather because class Win32_logonSession does not exist on this machine.  Which accounts for the class error.  What I don't understand is (and I guess my thinking is wrong on this) I thought .count had a default value of 0, so if there is no dectected data in the collection/array, then the value would stay at 0....and if there is detected data, then it would increase by 1 for each new instance in the Array.

Trouble is likely that Win32_LogonSession is not on this machine, so it would seem purdent to do a check for Win32_logonSession before creating colSessions.

(in reply to ebgreen)
 
 
Post #: 13
 
 RE: Help figuring out Whats wrong with Script - 3/31/2008 3:45:28 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
I think the issue is that there is no colSessions ever created if the class does not exist, so there is no .Count property of the non-existent collection.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chiltz)
 
 
Post #: 14
 
 RE: Help figuring out Whats wrong with Script - 3/31/2008 4:29:27 AM   
  chiltz

 

Posts: 76
Score: 0
Joined: 6/13/2007
Status: offline
IsNull(colsessions) - returns "False"
IsEmpty(colSessions) - returned "False
IsObject(colSessions) - returns "True"

Wouldn't these responses from the script indicate that colSessions does exist?

(in reply to ebgreen)
 
 
Post #: 15
 
 RE: Help figuring out Whats wrong with Script - 3/31/2008 4:35:37 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
I will admit that those responses are curious. Gimme a few and I'll do a little research.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chiltz)
 
 
Post #: 16
 
 RE: Help figuring out Whats wrong with Script - 3/31/2008 5:06:06 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Ok, so the ExecQuery  returns a collection regardless. It's just that without a valid class the collection does not have all of it's properties populated. So it looks like there isn't a really good method for doing this. I suppose you could do something like this:

Option Explicit
Dim strComputer , bDoThings
Dim objWMIService , colSessions , objSession , colList , objItem
strComputer = InputBox("Please Enter Server To Check for Logged In Users")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
On Error Resume Next
Set colSessions = objWMIService.ExecQuery ("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10")
nCount = colSessions.Count
bDoThings = False
if VarType(nCount) > 0 Then
bDoThings =True
Else
MsgBox ("No Sessions Exist, either no users or Win32_LogonSession does not exist")
End If
On Error Goto 0
If bDoThings = True Then
For Each objSession in colSessions
Set colList = objWMIService.ExecQuery("Associators of "& "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " & "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )
      For Each objItem in colList
         WScript.Echo "Username: " & objItem.Name
      Next
Next
Wscript.Echo "Script is Complete"
End If  


I think that would do what you want.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ebgreen)
 
 
Post #: 17
 
 RE: Help figuring out Whats wrong with Script - 3/31/2008 5:30:01 AM   
  chiltz

 

Posts: 76
Score: 0
Joined: 6/13/2007
Status: offline
still comes back as null error

Im thinkin the work around is something to the effect of the below...but not sure the correct way to write it.  I know that Ifexists statement is not right.

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
IfExists objWMIService.win32_logonsession Then
  Set colSessions = objWMIService.ExecQuery ("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10")
  For Each objSession in colSessions
     Set colList = objWMIService.ExecQuery("Associators of "& "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " & "Where AssocClass=Win32_LoggedOnUser_ &   
                          Role=Dependent" ) 
      For Each objItem in colList 
          MsgBox "Username: " & objItem.Name
       Next
  Next
  MsgBox "Script is Complete"
Else
MsgBox "Win32_LogonSession does not exist on machine: " & strComputer " Script is complete"


(in reply to ebgreen)
 
 
Post #: 18
 
 RE: Help figuring out Whats wrong with Script - 3/31/2008 5:33:16 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
So even with the On Error Resume Next in there you still get an error? When I run that code looking for a completely non-existent class it runs without an error.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chiltz)
 
 
Post #: 19
 
 RE: Help figuring out Whats wrong with Script - 3/31/2008 5:52:27 AM   
  chiltz

 

Posts: 76
Score: 0
Joined: 6/13/2007
Status: offline
I thought the below script would work....turns out it still gives me a class error on line 8 :(.  EB...you are right...if I add the On Error Resume Next, it does not display an error...but that is more a bandaid than a fix....and I don't know if the script didn't work because Win32_LogonSession did not exist or because nobody was logged into the machine.  Reason I am trying to build this code is so if I am required to work on a server, I don't have to use TSM to remotely check who is logged in or login to the machine and check.


      

(in reply to ebgreen)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Help figuring out Whats wrong with Script Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts