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

Hostname to username 3

Status
Not open for further replies.

rubbersoul

IS-IT--Management
Mar 20, 2003
88
CA
I need to find a way to match all my PC hostnames within the Active Directory to actual users who are using them....anotherwords my PC is say 66yykrii.mydomain.local....I need to know its 'Joe Somebodys' PC....any dynamic way of doing this?

 
Do you mean what AD user is using them? Or who's permanent desk it is at. You have text "OWNER" properties of all computer objects, that would be a case of manually entering them though. You can get hold of many scripts that let you query a machine(s) and find out which user is / last user was, logged on. What do you want to do with this information?

Hope this Helps.

Neil J Cotton
njc Information Systems
Systems Consultant
 
I want to take the information and populate the AD with it. So I need to mach PC Hostname to user....yes! I'm talking about the user who physically sits and owns that PC...not who is currently using it....just who sits there and uses it day-to-day. Do you have any examples of scripts or where I can find them?

 
Anyone know of a 3rd party application that may do this?

 
Mark would be your best bet for a script like this, I have seen scripts to return users, but not sure about populating the Owenership property of the machines through a script. If it can be done, and Im sure it will. Mark will know.

Hope this Helps.

Neil J Cotton
njc Information Systems
Systems Consultant
 
OK....thanks....whomever Mark is....hope u can help!

 
Thanks tfg. You guys make me feel so famous. [blush]

This can be done rather easily with vbscript.

What you want to do is bind to the machine object with WMI and return the information for the currently logged on user. Grabbing any static value such as Owner really won't help if PCs have been shuffled around as is usually the case.


That all sounds so nice and technical doesn't it?

Here is the code you need. This requires you put a list of workstation names to query in a text file in the same location as the script. It will then generate a text report for you. Place one machine name on each line of the text file. Refer to my faq faq329-4871 for a script to dump all machine names to a text file. Save the below code to a text file in notepad and give it a name WhoIsLoggedInThereListVer.vbs.


Code:
'==========================================================================
'
' NAME: WhoIsLoggedInThereListVer.vbs 
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.TheSpidersParlor.com[/URL]
' COPYWRITE (c) 2005 All Rights Reserved
' DATE  : 12/14/2005
'
' COMMENT: This script and many more are available in the Admin Script Pack
'          by The Spider's Parlor.  [URL unfurl="true"]http://www.TheSpidersParlor.com/vbscript[/URL]
'==========================================================================

On Error Resume Next

'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
'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:" _
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
	For Each objItem in colItems
	    Report = Report &  "UserName: " & objItem.UserName & " is logged in at computer " & strComputer & vbCrLf
	Next
Next

Set ts = oFSO.CreateTextFile ("logreport.txt", ForWriting)
ts.write report


I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Just giving credit where credit is due! Star is on it's way!

 
Seems the star giving is broke right now. I'll try to remember to give you one in a little while....
 
Hey Mark thanks for the code....just got to this today! When I run the first script I got all the PC dumped into thw 'wslist.txt' file....perfect! Then I ran the 'WhoIsLoggedInThereListVer.vbs' which resides in the same folder as the 'wslist.txt' file and it generates an error...





 
As the message says, there is a ) missing, and its on line 26, line 27 it relises that the ( was not closed.

fixed

Code:
'==========================================================================
'
' NAME: WhoIsLoggedInThereListVer.vbs 
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.TheSpidersParlor.com[/URL]
' COPYWRITE (c) 2005 All Rights Reserved
' DATE  : 12/14/2005
'
' COMMENT: This script and many more are available in the Admin Script Pack
'          by The Spider's Parlor.  [URL unfurl="true"]http://www.TheSpidersParlor.com/vbscript[/URL]
'==========================================================================

On Error Resume Next

'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
'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:")
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
    For Each objItem in colItems
        Report = Report &  "UserName: " & objItem.UserName & " is logged in at computer " & strComputer & vbCrLf
    Next
Next

Set ts = oFSO.CreateTextFile ("logreport.txt", ForWriting)
ts.write report

Hope this Helps.

Neil J Cotton
njc Information Systems
Systems Consultant
 
excuse the missing /code statement lol

Code:
'==========================================================================
'
' NAME: WhoIsLoggedInThereListVer.vbs 
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.TheSpidersParlor.com[/URL]
' COPYWRITE (c) 2005 All Rights Reserved
' DATE  : 12/14/2005
'
' COMMENT: This script and many more are available in the Admin Script Pack
'          by The Spider's Parlor.  [URL unfurl="true"]http://www.TheSpidersParlor.com/vbscript[/URL]
'==========================================================================

On Error Resume Next

'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
'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:")
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
    For Each objItem in colItems
        Report = Report &  "UserName: " & objItem.UserName & " is logged in at computer " & strComputer & vbCrLf
    Next
Next

Set ts = oFSO.CreateTextFile ("logreport.txt", ForWriting)
ts.write report

Hope this Helps.

Neil J Cotton
njc Information Systems
Systems Consultant
 
Wow, seems I had some wierd copy paste issue.

In my post the line
Set objWMIService = GetObject("winmgmts:" _
should be removed, it is a duplicate start of the next line and I really don't know how the heck I got the underscore which would be for a line wrap. It's not in my copy of the script on my side. Very strange. I must have been typing to quick or something and not noticed the focus changed or something like that. Sorry for the confusion.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
LOL. Mark, I didn't even read the code, just went back and put the ")" in.

:)

Hope this Helps.

Neil J Cotton
njc Information Systems
Systems Consultant
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top