Display Username Of A Remote Computer

From Wiki

Jump to: navigation, search

Creator: IT Support Staff @ HCHS

Script Language: VBS

Forum topic Link: http://www.edugeek.net/forums/wiki-announcements/18521-display-username-remote-computer.html

This script will Display who is logged onto a remote computer and will also find and display the exact location of the user account in Active Directory.

strComputer = inputbox("Please enter the name of the computer:","Enter Computer Name")
 
' Check that user entered a value
if strComputer = "" then
	wscript.quit
end if
 
set objRoot = getobject("LDAP://RootDSE")
domainname = objRoot.get("defaultNamingContext")
set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & strComputer & "")
set colOS = objWMI.ExecQuery("Select * from Win32_ComputerSystem")
 
For Each objItem In colOS
strUserName = objItem.UserName
 
IF strUserName <> "" then
 
logonname = MID(strUserName,(instr(strUserName,"\")+1),LEN(strUserName))
 
ou = finduser(logonName,domainname)
userou = mid(ou,instr(ou,",")+1,len(ou)-instr(ou,","))
'ou = split(ou, ",")
'userou = ou '(0)
 
 
MSGbox "The following user is logged on to this computer  :        " & strUserName & VBCR & VBCR & "This user's account can be found in the OU" & VBCR & userou,,strComputer
 
ELSE MSGbox "There is no user currently logged on to this computer",,strComputer
 
 
 
END IF
 
next
 
 
 
'end sub
 
Function FindUser(Byval strUserName, Byval Domain) 
	on error resume next
 
	set cn = createobject("ADODB.Connection")
	set cmd = createobject("ADODB.Command")
	set rs = createobject("ADODB.Recordset")
 
	cn.open "Provider=ADsDSOObject;"
 
	cmd.activeconnection=cn
	cmd.commandtext="SELECT ADsPath FROM 'LDAP://" & Domain & _
			 "' WHERE sAMAccountName = '" & strUserName & "'"
 
	set rs = cmd.execute
 
	if err<>0 then
		FindUser="Error connecting to Active Directory Database:" & err.description
	else
		if not rs.BOF and not rs.EOF then
     			rs.MoveFirst
     			FindUser = rs(0)
		else
			FindUser = "Not Found"
		end if
	end if
	cn.close
end function