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.
call cscript /nologo C:\wrkstin_In_Use.vbs 60 60 > C:\logs\Wrkstin_In_Use
'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