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

Matching Username with the domain computer

Status
Not open for further replies.

chxpty

MIS
May 19, 2005
7
US
Hi. I am new to this VBscript. Heres my problem. I have to enter the user name in and match it up with the computer that they are currently log on to. Also i need to find the IP address of that computer. i am not sure were to start i know how to find that information for the local computer but i don't know how to find it on the network. any help would be wonderful.
 
Don't remember where I got this to give credit to the proper source but I didn't write this. When run you enter the computer name and it will return the currently logged in user.

strComputer = InputBox("Computer to Query for Logged In User","Search where?")

On Error Resume Next

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem in colItems
Wscript.Echo "UserName: " & objItem.UserName & " is logged in at computer " & strComputer
Next

Hope it helps,

Aaron
 
This helps but i need to be able to look at the users logged in to the variouse computers on the network.
 
That would be my WhosLoggedInthere script.
Code:
'==========================================================================
'
' NAME: WhoIsLoggedInThere.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 3/29/2005
'
' COMMENT: Prompts for a PC name and returns currently logged in user
'
'==========================================================================

strComputer = InputBox("Computer to Query for Logged In User","Search where?")

On Error Resume Next

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem in colItems
    Wscript.Echo "UserName: " & objItem.UserName & " is logged in at computer " & strComputer
Next

But I think what you are looking for is this:

Code:
'==========================================================================
'
' NAME: ReportLogonLocationsByPC.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 3/11/2004
'
' COMMENT: <comment>
'
'==========================================================================

On Error Resume Next

'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

'open the data file
Set oTextStream = oFSO.OpenTextFile("wslist.txt")
'make an array from the data file
RemotePC = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close

For Each strComputer In RemotePC
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkConnection",,48)
	Report = ""
	For Each objItem in colItems
	   Report = Report &   "UserName: " & objItem.UserName & "  is logged in at " & strComputer & vbCrLf
	   Exit For
	Next
Next
Set ts = oFSO.CreateTextFile ("LogonLocationReport.txt", ForWriting)
ts.write Report
Set oTextStream = nothing
set ts = nothing
set oFSO = nothing

To automatically generate the wslist file needed by the above script you can use this one:

Code:
'==========================================================================
'
' NAME: CreateWSList.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 3/18/2004
'
' COMMENT: <comment>
'
'==========================================================================

Const ADS_SCOPE_SUBTREE = 2
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, Location 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
    report = report & objRecordSet.Fields("Name").Value & vbCrLf
    objRecordSet.MoveNext
Loop


Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.CreateTextFile ("wslist.txt", ForWriting)
ts.write report
ts.close
MsgBox "Done"

I hope you find this post helpful.

Regards,

Mark
 
that is what i am looking for. i have one last question when i put in the code i could not get the file LogonLocationReport.txt to be created. The wslist was created but the logon location report was not. any suggestions?
 
Comment out the On Error Resume Next and see what errors you get and report back. FYI: I'm going to have to sign off soon.

I hope you find this post helpful.

Regards,

Mark
 
One other thought too, did you check running processes to make certain the script is actually done running? If you have a lot of PCs it could take a while. Also make certain that the wslist only contains VALID machine names. if you have removed old machines from the domain but the machine names are still in your AD the results can be strange.

I hope you find this post helpful.

Regards,

Mark
 
'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

'open the data file
Set oTextStream = oFSO.OpenTextFile("wslist.txt")
'make an array from the data file
RemotePC = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close

For Each strComputer In RemotePC
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkConnection",,48)
report = ""
For Each objItem in colItems
Report = Report & "UserName: " & objItem.UserName & " is logged in at " & strComputer & vbCrLf
Exit For
Next
Next
Set ts = oFSO.CreateTextFile ("LogonLocationReport.txt", ForWriting)
ts.write Report
Set oTextStream = nothing
set ts = nothing
set oFSO = nothing

msgbox "done"

Line 12 Char:5
Error 0x80041003
Code 80041003
source: (Null)

 
Are you logged on with Admin rights? If I am counting right that is the line Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

I hope you find this post helpful.

Regards,

Mark
 
You are correct that is the line. So what your saying is that i have to be logged in as an administrator for this code to work. Now is that an adminstrator on the network or the local computer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top