Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Request / Who is Logged On Script

Status
Not open for further replies.

x11tech

Programmer
Dec 26, 2007
190
Starting with
Code:
' WhoLogonInput.vbs
' Sample VBScript to discover which user is logged on
' Author Guy Thomas [URL unfurl="true"]http://computerperformance.co.uk/[/URL]
' Version 2.4 - December 2005
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objComputer, colComputer
Dim strLogonUser, strLogonUser1, strComputer

strComputer = "."
strComputer = InputBox("Enter Computer name", "Find Logon User", strComputer)
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" &_
   strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")

For Each objComputer in colComputer
	'Note: if objComputer.UserName is null, this generates a
	'script error.  objComputer.UserName is null when no one is logged in.
	strLogonUser = Split(objComputer.UserName,"\")
	strLogonUser(1) = UCase(Left(strLogonUser(1),1)) & Trim(Mid(strLogonUser(1),2,20))
	Wscript.Echo strLogonUser(1) & " is logged on at " & strComputer
Next

' End of Sample Logged on VBScript

I modified the script as follows:

Code:
Option Explicit
Dim oWMIService, oComputer, colComputer
Dim oFSO, oFileInfo
Dim sLogonUser, sComputer, sTime
Dim sInfoFile ', sHtmlFile
Dim sOldLogonUser, sOldTime
' Dim sLogonUser1
' Dim oFileHtml

const ForReading = 1
const ForWriting = 2
const ForAppending = 8

sComputer = "mycomputer"
'sHtmlFile = "c:\inetpub\[URL unfurl="true"]wwwroot\operations_ts1.asp"[/URL]
sInfoFile = "c:\inetpub\[URL unfurl="true"]wwwroot\mycomputer_status.txt"[/URL]

set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" &_
   sComputer & "\root\cimv2")
Set colComputer = oWMIService.ExecQuery ("Select * from Win32_ComputerSystem")

For Each oComputer in colComputer
	if not isnull(oComputer.UserName) then
		wscript.echo oComputer.UserName
		sLogonUser = Split(oComputer.UserName,"\")
		wscript.echo sLogonUser
		sLogonUser(1) = UCase(Left(sLogonUser(1),1)) & Trim(Mid(sLogonUser(1),2,20))
		'Wscript.Echo sLogonUser(1) & " is logged on at " & sComputer

		if oFSO.FileExists(sInfoFile) then
			set oFileInfo = oFSO.OpenTextFile(sInfoFile, ForReading)
			sOldLogonUser = oFileInfo.ReadLine
			sOldTime = oFileInfo.ReadLine

			set oFileInfo = nothing

		else
			sOldLogonUser = ""
			sOldTime = ""

		end if

		sTime = Now

		if sOldLogonUser <> sLogonUser(1) then
			if oFSO.FileExists(sInfoFile) then
				set oFileInfo = oFSO.GetFile(sInfoFile)
				oFileInfo.Delete

				set oFileInfo = nothing
			end if

			set oFileInfo = oFSO.OpenTextFile(sInfoFile, ForWriting, True)
			oFileInfo.WriteLine(sLogonUser(1))
			oFileInfo.WriteLine(sTime)

			set oFileInfo = nothing

			'Wscript.Echo sOldLogonUser & " <> " & sLogonUser(1)
		end if

	else
		'wscript.echo "No one is logged in at " & sComputer
		sTime = Now

		if oFSO.FileExists(sInfoFile) then
			set oFileInfo = oFSO.OpenTextFile(sInfoFile, ForReading)
			sOldLogonUser = oFileInfo.ReadLine
			sOldTime = oFileInfo.ReadLine

			set oFileInfo = nothing

			set oFileInfo = oFSO.GetFile(sInfoFile)
			oFileInfo.Delete

			set oFileInfo = nothing
		end if

		set oFileInfo = oFSO.OpenTextFile(sInfoFile, ForWriting, True)
		oFileInfo.WriteLine("No one")

		if sOldLogonUser = "No one" then
			oFileInfo.WriteLine(sOldTime)
		else
			oFileInfo.WriteLine(sTime)
		end if

		set oFileInfo = nothing

	end if
Next

' End of Sample Logged on VBScript

I scheduled the second script to run every 2 minutes on a Windows 2003 server. The script successfully retrieves username information when the account running the script is the same as the account logged in to the queried server.

When the user running the script and the user logged in to the queried server differ, the script returns that no one is logged in. (This is in error.)

I experience the same problems regardless of the computer that I run the script from. (I've also tried running it from a Windows XP SP2 workstation.)

The user accounts being used to test this all have Domain Admins membership.


This appears to be some kind of user rights issue. Any assistance would be appreciated.
 
Using the Win32_ComputerSystem WMI class is not a very good way to get the user logged into a computer...as you've noticed. This has been discussed several times now...a better solution would be to use Win32_Process and see who the owners of explorer.exe are...anyone logged in will have an instance of explorer.exe running and the Win32_Process class has a GetOwner method to show you who owns it.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thanks, I appreciate your patience with this old question. I'll look in to Win32_Process.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top