Hello, I tried using the below code designed by you in my network ("mydomain.com"), but I am not getting the output that says the user is logged onto which machines on network. This script only gives ping result outputs. I would appreciate your help here.... :-)
'Script by MJP 2006
'Reports which users are logged into a computer
'or
'Reports which users are logged into all domain computers
'Run it from command line like such:-
'cscript UsersLoggedIn.vbs PC001 >UsersLoggedIn.csv
'or
'cscript UsersloggedIn.vbs >UsersLoggedIn.csv
'Check if a "Computer Name" cmd line variable was passed to script
On Error Resume next
'Set objRootDSE = GetObject("
LDAP://RootDSE")
'strDomain = objRootDSE.Get("DefaultNamingContext")
strComputer=WScript.Arguments.Item(0)
On Error Goto 0
If strComputer="" Then
' No specific computer was specified, proceed to query all computers in domain
Else
strPingStatus = PingStatus(strComputer)
If strPingStatus = "Success" Then
QPO 'Run Query Process Owner function
Else
WScript.Echo strComputer & ",Failed ping with: " & strPingStatus
End If
WScript.quit
End If
' Enumerate All Computer Accounts in Active Directory
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select Name, Location from 'LDAP://DC=mydomain,DC=COM' " _
& "Where objectClass='computer'"
' "Select Name, Location from 'LDAP://"& StrDomain & " ' where objectClass='printQueue'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strComputer = objRecordSet.Fields("Name").Value
objRecordSet.MoveNext
'Check if computer account is obsolite
If obsoliteness(strComputer) =0 Then
'check computer is on and echo "logged in user" or "ping failure status"
strPingStatus = PingStatus(strComputer)
If strPingStatus = "Success" Then
QPO 'Run Query Process Owner function
Else
WScript.Echo strComputer & ",Failed ping with: " & strPingStatus &","&time
End If
Else
WScript.Echo strComputer & ",Identified as an obsolite machine account,"&time
End If
Loop
WScript.Quit
'---------------------------------------
'My obsoliteness function
function obsoliteness(var)
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Pattern = "(^XC00)|(^RC00)|(^QC00)|(^PC00)|(^OC00)|(^JC00)|(^HC00)|(^FC00)|(^EC00)|(^DC00)|(^CC)|(^C00)"
obsoliteness = myRegExp.test(var)
end function
'---------------------------------------
'Query Process Owner function
Function QPO
On Error Resume next
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'explorer.exe'")
For Each objProcess in colProcessList
objProcess.GetOwner strUserName, strUserDomain
Wscript.Echo strComputer &",Is logged into by "&strUserDomain & "\" & strUserName &","&time
Next
End Function
'---------------------------------------
'-------------------------------------
Function FastPing(strComputer)
Set WshShell = WScript.CreateObject("WScript.Shell")
return = WshShell.Run("ping "&strComputer&" -n 1 -w 500", 0, true)
if return = 0 then
FastPing= "Success"
Else
FastPing= "Failure"
End if
End function
'-------------------------------------
'GET USERDOMAIn
Sub DOMAIn
Set objExecObject = objShell.Exec("cmd /c echo %USERDOMAIN%")
Do While Not objExecObject.StdOut.AtEndOfStream
userdomain = objExecObject.StdOut.ReadLine() 'gets %userdomain%
trim(userdomain)
loop
'Get a List of Servers
Const ForWriting = 2, ForAppending=8
Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
Dim F : Set F = Fso.CreateTextFile(inputfile, TRUE)
'Should Exec echo %USERDOMAIN% get parameter
Set oContainer = GetObject("WinNT://" & userdomain)
oContainer.Filter = Array("computer")
For Each oComputer1 in oContainer
Pingit(oComputer1.name)
if strPingit = "Success" then
F.WriteLine(oComputer1.name) 'ADD TO SERVERLIST
end if
Next
f.close
end sub
'Function Pingit(ip)
Function PingStatus(strComputer)
strTarget = ip 'IP address or hostname
Set objShell = CreateObject("WScript.Shell") 'Create Shell object
Set objExec = objShell.Exec("ping -n 1 -w 200 " & strTarget)
'Execute Ping command with option in shell (dos)
strPingResults = LCase(objExec.StdOut.ReadAll) 'Read shell (dos) output
If InStr(strPingResults, "reply from") Then 'Scan shell (dos) output for "Reply From" which indicates success
strPingit = "Success" 'Return TRUE if ping is successful
Else
strPingit = "False" 'Return FALSE if ping failes
End If
End Function