7 Replies - 5912 Views - Last Post: 06 December 2012 - 10:55 PM Rate Topic: -----

#1 spc21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 04-February 09

Using VBScript to get information for current logged in user

Posted 30 November 2012 - 10:59 PM

ALL,

I am trying to gather some specific information about whether there is currently a user logged onto a workstation/machine/server, whether that users session is ACTIVE or INACTIVE, and if they're INACTIVE--how long have they been INACTIVE. Essentially, my cohorts have a "tough time" remembering to log off of their machines/Virtual Desktops; so I am attempting to write a script that will report to me, and will tell me if they've been in active for an extended amount of time. I have written my code to the best of my knowledge, as I am not well versed in VBS--I only have a cursory knowledge of VB.net--and I've ran into a little bit of a snag.

What I'm utilizing for my code:

* Using a batch file as a wrapper to send arguments to the VBS, and output to a logfile
* The VBS is used to perform all coding, computations, and returns to the logfile.

Here are my goals:

* Find out if a user is logged on and if they are ACTIVE or INACTIVE
- If there is no one logged on, report to the log file as this workstation is available for use
- If there is a user logged on, and they are currently ACTIVE report that the workstation is currently in use.
- Otherwise, if they have been INACTIVE for over a defined amount of time--return as they need kicked off.

Problems I'm currently having:

* I am attempting to run this (.bat file -> .vbs) under a scheduled task, and instead of grabbing the user who is logged in. It is grabbing the owner of the scheduled task.
* I am using when the NTUSER.DAT file was last modified as my time reference, and I don't think its pulling the proper information--working properly. It isn't pulling the CRITICAL case when it should be, or the OK case(when there is no user logged in) because its finding the scheduled task owner as being logged in.
* Since the code continues to show that there is a user logged in, I cannot properly use it for my purpose with out fixing the problems.

What I've done to trouble shoot
* I have attempted to "mess with" who the owner of the scheduled task is (admin, NT Authority, etc.) still sees that user as logged in. (This code should be able to determine if no one is logged in currently)
* I have tried changing my case/switch statements to account for time issues--again, I think this has to do with the task/job owner.


BATCH FILE
call cscript /nologo C:\wrkstin_In_Use.vbs 60 60 > C:\logs\Wrkstin_In_Use


VBS FILE

'Read the User defined Arguments from the batch file (timetolook, idle time, etc.)
Set CommandLineArgs = WScript.Arguments

'Get the user defined acceptable Idle Time from the batch file. 
'If the users Idle time is higher than this number go critical.
intIdleTime = CommandLineArgs(0)

'Get the user defined acceptable time from the batch file.
intTimetoLook = CommandLineArgs(1)

'Create the object so you can access information about this machine.
Set objNetwork = CreateObject("Wscript.Network")

'Get the name of the server/machine.
strComputer = objNetwork.ComputerName

'Get what user is currently logged into the server/machine.
strUser = objNetwork.UserName

'Lets find out how long they've been idle. Creates the object so we can pull the last time the user did something. 
'Can be interchanged with ("\\" & strComputer & "\Documents and Settings\" & strUser & "\NTUSER.DAT").DateLastModified
set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
strModified = objFSO.GetFile("C:\Documents and Settings\" & strUser & "\NTUSER.DAT").DateLastModified

'If user has been INACTIVE for over the specified time, go critical.
If (datediff("n", strModified, now) > intIdleTime) AND (strUser <>"") then
	ReturnMessage("CRITICAL")
ElseIf (datediff("n", strModified, now) < intIdleTime) AND (strUser <>"") then
	ReturnMessage("WARNING")
Else
	ReturnMessage("OK")
End If

'This function/Case/Switch will report the proper info to the log file
Function ReturnMessage(Message)
    Select Case UCase(Message)
        Case "OK"
            WScript.Echo intTimetoLook & "m OK: This VDI is available for use"
            WScript.Quit(0)
        Case "CRITICAL"    
	    WScript.Echo intTimetoLook & "m Critical: " & strUser & " has been inactive for over " & intIdleTime & " on " & strComputer & " they may have forgotten to log off!"
            WScript.Quit(2)
        Case "WARNING"    
	    WScript.Echo intTimetoLook & "m Warning: " & strUser & " is currently active on " & strComputer & "." 
            WScript.Quit(2)
    End Select
End Function

This post has been edited by spc21: 30 November 2012 - 11:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using VBScript to get information for current logged in user

#2 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 572
  • View blog
  • Posts: 2,985
  • Joined: 19-May 09

Re: Using VBScript to get information for current logged in user

Posted 01 December 2012 - 12:22 PM

Have a look at WMI. It's a set of objects that run as a service, and will give you all kinds of information about the environment. including what users are logged on, if my memory serves me correctly.
Was This Post Helpful? 0
  • +
  • -

#3 spc21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 04-February 09

Re: Using VBScript to get information for current logged in user

Posted 01 December 2012 - 03:24 PM

Bob,

Thank you for the reply. I was able to find a few different WMI calls that perform the actions that I needed, and I'm able to gather information properly now. However, the issue I'm having now is with the NTUSER.dat file. From what I was able to find, I thought this file was updated whenever any actions were performed on the system--however, the file has updated on my test box since about 3:17pm EST. Would you be able to shed any light on this? I've tried to search google/forums for a better explanation of what this database does, and when it gets updated, but I couldn't find anything. is there a better system file that I could reference, which would get a more accurate time frame for when the user was last active? I was trying to find info in WMI for this, but I don't know if I was looking for the right parameters.
Was This Post Helpful? 0
  • +
  • -

#4 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 572
  • View blog
  • Posts: 2,985
  • Joined: 19-May 09

Re: Using VBScript to get information for current logged in user

Posted 01 December 2012 - 07:20 PM

I did a bit of research on this, and ntuser.dat indeed seems pretty unclear, even to Microsoft support. I also took a look at the "NET USER" command prompt command. NET USER [username] has a "Last Logon" value, that accurately gives the time of the last logon. (I logged in and out and checked a couple of times.) Are you able to access this information programmatically? I can think of one way to do it (redirect the output to a file and read the file) but a cleaner way would be to redirect to a text stream and look at the stream. I don't know how to do that offhand.
Was This Post Helpful? 0
  • +
  • -

#5 spc21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 04-February 09

Re: Using VBScript to get information for current logged in user

Posted 01 December 2012 - 08:02 PM

Hmm, I think I may have worked with something like Net User before; however, I'm drawing a blank on what I did/how I read it. I believe I did the same thing you stated above, and parsed it out looking for keywords/times. What I'm trying to determine, is whether they are active vs inactive; unfortunately, it doesn't look like NET USER queries for this information. I know that task manager has the USERS tab which shows inactivity as well as for how long (like in terminal services), but I can't seem to find anything that I can use to access this--or what it's using to determine that a user is inactive. From what I was able to find on the NTUSER.dat, there was a suggestion made that the file was updated whenever there was any user activity (why I chose it), which I'm finding to be false--it seems to update randomly or at logon/logoff. I'm going to keep peeling through, and see if there is a WMI procedure that can pull this information for me (maybe from terminal services or task manager?)--no luck as of yet though. Thank you for your help so far!
Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 572
  • View blog
  • Posts: 2,985
  • Joined: 19-May 09

Re: Using VBScript to get information for current logged in user

Posted 02 December 2012 - 02:09 PM

I don't think that NTUSER.dat is up to the minute. You should be able to get logged on users from the Win32_LogonSesson class, and then you can use NET USER to evaluate when they logged on. Now, perhaps you need to explain further what you mean by "inactive". What constitutes an inactive user exactly?
Was This Post Helpful? 0
  • +
  • -

#7 spc21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 04-February 09

Re: Using VBScript to get information for current logged in user

Posted 06 December 2012 - 07:12 AM

Hi bob,

Sorry it's been so long between my posts. I almost forgot about this, because I figured out that the NTUSER.DAT.LOG file gets updated every minute while the user is actively doing something on the machine. Essentially what I meant by INACTIVE, was that the user has not performed any actions--or have any programs currently running (something that is actively writing/performing changes on something). I have found major problems with people not logging off of single signon environments, and leaving them locked for days at a time--all the while they're using other machines daily and doing the same thing over and over consuming unnecessary resources. This script was meant to find users that have been INACTIVE (nothing being done on the machine, just that the user is logged on) for over a specified time, that gets passed through by the batch file. I did some digging, and I found that the log file for the NTUSER database gets updated when the user is actively working on the machine, and does not update unless the user clicks in the environment, enters a keystroke, or a file is querying the registry keys. For now, it appears to work properly without issue. Thank you for your assistance! =]
Was This Post Helpful? 0
  • +
  • -

#8 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 572
  • View blog
  • Posts: 2,985
  • Joined: 19-May 09

Re: Using VBScript to get information for current logged in user

Posted 06 December 2012 - 10:55 PM

You're very welcome, glad you got it worked out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1