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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Free Code: ASP/VBScript List Users Logged into PCs 1

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
US
Hi Everyone,

The following code is one of the latest being added to my Admin Script Pack. I thought others on Tek-Tips would benefit from this as well, so I am sharing it.

This code will be useful to Administrators and Help Desk personnel. What it does is enumerate all the computers from AD and displays a list of who is logged in at that computer.

Place this code as an ASP file on an IIS server.
Right click the file in IIS and choose properties.
Click the File Security tab.
Click the top Edit button for Authentication and Access Control.

If only Administrators will be using this page, clear the Enable Anonymous Access checkbox and check Integrated Windows Authentication.

If you wish to have non-administrators use the page then leave Enable Anonymous Access checked but set the Anonymous User Name and Password to be that of a resource account that has admin rights to the local workstations and AD.

Use Ctrl+F to quickly locate a machine name or User ID.

Code:
[green]
<!--
'==========================================================================
'
' NAME: MatchUserComputer.asp
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 3/24/2007
' COPYRIGHT (c) 2007 All Rights Reserved
'
' COMMENT: Users accessing this web page need admin rights or you must
'          set the an ID in IIS security that can read the information 
'          from AD and use remote WMI calls to workstations.
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'
'    THIS CODE AND MANY MORE VALUABLE SCRIPTS ARE PART OF 
'    THE SPIDER'S PARLOR ADMIN SCRIPT PACK
'    [URL unfurl="true"]http://www.thespidersparlor.com/vbscript[/URL]
'     
'==========================================================================
-->[/green]

<%@ LANGUAGE="VBSCRIPT" %>
<%
On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2
Dim NewComputerArray

[green]'Query AD for computer names.[/green]
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objDomain = getObject("LDAP://rootDse")
Domain = objDomain.Get("defaultNamingContext")
LDPATH = Chr(39) & "LDAP://" & Domain & Chr(39)


Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
    "Select Name from " & LDPATH _
        & "where objectClass='computer'"  
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30 
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
objCommand.Properties("Cache Results") = False 

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
 	cList = cList & UCase(objRecordSet.Fields("Name").Value) & vbCrLf
 	objRecordSet.MoveNext
Loop

[green]'Make an array out of the collection[/green]
NewComputerArray = Split(cList ,vbCrLf)

[green]'Now sort the array
       '[URL unfurl="true"]http://www.tek-tips.com/faqs.cfm?fid=4836[/URL][/green]
for i = UBound(NewComputerArray) - 1 To 0 Step -1
    for j= 0 to i
        if NewComputerArray(j)>NewComputerArray(j+1) then
            temp=NewComputerArray(j+1)
            NewComputerArray(j+1)=NewComputerArray(j)
            NewComputerArray(j)=temp
        end if
    next
next 

[green]'Build our report text[/green]
For Each sComputer In NewComputerArray
	If Len(sComputer) > 0 Then
		[green]'Get the logged on user[/green]
		User = GetUserLogged(sComputer)
		If AltColor = 0 Then
			Report = Report & "<tr><td>" & sComputer & "</td><td align='left'>" & User &"</td></tr>"
			AltColor = 1
		Else
			Report = Report & "<tr bgcolor='#00A3F0'><td>" & sComputer & "</td><td align='left'>" & User &"</td></tr>"
			AltColor = 0
		End If
	End If
Next

[green]'Now display the web page[/green]

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>List Logged On Users</title>
</head>

<body bgcolor="blue"><center>
<table bgcolor="#66CCFF" border="1" width="75%">
	<tr bgcolor="white"><th>Computer</th><th>Logged in User</th></tr>
<%
Response.Write Report

%>
</table></center>
This web page brought to you by <a href="[URL unfurl="true"]http://www.thespidersparlor.com/vbscript">The[/URL] Spider's Parlor.</a>
</body>

</html>

<%
Function GetUserLogged(strComputer)
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
	For Each objItem in colItems
	    ThisUser = objItem.UserName
	Next
	GetUserLogged = ThisUser
End Function
%>

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Wow.. thank you very much for the useful post Mark. Your generosity within these forums is very much appreciated. Cheers!
 
Thank you for the post mark! [2thumbsup]

Just a suggestion...you could add the following line to get the computer names returned in alphabetical order and avoid sorting the array.

objCommand.Properties("Sort On") = "Name"

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thanks for the suggestion DM4Ever. The problem I found was that machine names could be entered into AD with mixed case and the lower case and upper case names will not sort as expected. That is why I force all upper case machine names and then sort the array.

Without doing it this way, uppercase names will sort followed by lower case.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Ahh...makes sense!!!

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top