JSI Tip 8277. How can my script determine who is logged onto the console of a remote computer?


I have scripted WhoConsole.bat and WhoConsole.vbs to determine the user that is logged onto the console of a remote computer.

The syntax for using WhoConsole.bat is:

call WhoConsole ComputerName UserName

Where ComputerName is the NetBIOS name of the computer, and UserName is a call directed environment variable that will contain the logged on user, or null, if nobody is logged on, or if the ComputerName is NOT found.

NOTE: WhoConsole.bat and WhoConsole.vbs must be located in the same folder.

WhoConsole.bat contains:

@echo off
setlocal
if \{%2\}==\{\} goto err
set comp=%1
set user=null
set comp=%comp:\=%
set wc="%~DP0WhoConsole.vbs"
if not exist %wc% goto nowc
call :quiet>nul 2>&1
if "%r2%" NEQ "" goto sr2 
if "%r1%" EQU "" goto finish
:sr2
set user=%r2%
:finish
endlocal&set %2=%user%
goto :EOF
:quiet
for /f "Tokens=1* Delims=\" %%t in ('cscript //NOLOGO %wc% %comp%') do (
 set r1=%%t
 set r2=%%u
)
goto :EOF
:err
@echo syntax WhoConsole Computer User
endlocal
goto :EOF
:nowc
@echo WhoConsole.bat - Cannot locate WhoConsole.vbs
endlocal
WhoConsole.vbs contains:
dim ComputerName, Users
Dim objArgument
Set objArgument = Wscript.Arguments
ComputerName=objArgument(0)
who = "winmgmts:\{impersonationLevel=impersonate\}!//"& ComputerName &""
Set Users = GetObject( who ).InstancesOf ("Win32_ComputerSystem")
for each User in Users
	Wscript.echo User.UserName
Next



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish