PDA

Click to See Complete Forum and Search --> : [RESOLVED] specific user logged on?


BenjaminMJ
Dec 17th, 2007, 03:42 AM
Hello everyone. Please help me on this one! I'm trying to make a VBScript that checks whether or not a specific user is loggen on. If not, send smtp warning email. This should be fairly simple, but I can't get anything to work.

The code is:

'VBScript: Check to see, whether or not UserName is loggen on
'If logged on, do nothing. If not logged on, send warning email

Dim oShell
Dim callingUser
Dim userName
Set userName = "example.user"

Set oShell = Wscript.CreateObject("Wscript.Shell")
callingUser = oShell.ExpandEnvironmentStrings("%USERNAME%")
'WScript.Echo callingUser

'HELP me write a boolean function that checks to see whether or not userName is currently loggen on
'Returns true if userName is logged on, false if not.

if(loggedOn = true) then
Set oMsg = CreateObject("CDO.Message")
set iBodyPart = oMsg.BodyPart
iBodyPart.CharSet = "iso-8859-1"
oMsg.To = "to@example.com"
oMsg.From = "from@example.com"
oMsg.Subject = "WARNING from " & callingUser & ": " & userName & " is not logged on."
oMsg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "HIDDEN"
oMsg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oMsg.Configuration.Fields.Update
oMsg.Send
Set oMsg = Nothing
end if

Hack
Dec 17th, 2007, 10:35 AM
Welcome to the forums. :wave:

I don't see where you are comparing anything to anything to see if there is a match.

BenjaminMJ
Dec 18th, 2007, 02:14 AM
That's the thing. I didn't know how to access a list of logged on users on the computer... I got it work anyway, because at last, I magically found an example of doing it:

userName = "benjaminmj"
serverName = "MYCOMPUTER"
message0 = "WARNING from " & callingUser & ": " & userName & " is not logged on"
message1 = "INFORMATION from " & callingUser & ": " & userName & " is logged on."
recipiant = "rec@example.com"
sender = "sen@example.com"
smtpserver = "[SMPT OUT-server address]"
sendusing = 2

'***********************************
'***********************************
'***********************************

Dim oShell
Dim callingUser
found = 0
set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & serverName & "\root\cimv2")
set objEnum = objWMIService.execQuery("select __relpath from win32_process where caption = 'explorer.exe'")
Set oShell = Wscript.CreateObject("Wscript.Shell")
callingUser = oShell.ExpandEnvironmentStrings("%USERNAME%")
Set oMsg = CreateObject("CDO.Message")
set iBodyPart = oMsg.BodyPart
iBodyPart.CharSet = "iso-8859-1"
oMsg.To = recipiant
oMsg.From = sender

for each obj in objEnum
set outParams = obj.ExecMethod_("GetOwner")
if(outParams.User = userName) then
found = 1
end if
next

if(found = 0) then
oMsg.Subject = message0
else
oMsg.Subject = message1
end if
oMsg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpserver
oMsg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = sendusing
oMsg.Configuration.Fields.Update
oMsg.Send
Set oMsg = Nothing

Hack
Dec 18th, 2007, 11:39 AM
Well, I'm glad you go it going, and thank you for posting your results.

If you consider this resolved, you could help us out by pulling down the Thread Tools menu and clicking the Mark Thread Resolved menu item. That will let everyone know that you have your answer.

Thank you. :)

BenjaminMJ
Dec 19th, 2007, 12:52 AM
No problem... And thank you for your interest!