Link to home
Start Free TrialLog in
Avatar of Gitcho
Gitcho

asked on

How to get a computer name from a user name in VBscript/ASP

I work on the helpdesk for a large organization, and am looking for a way to get a host name from a username using VBscript in an ASP page.  I'd like for other helpdesk staff to submit a username on the page, and have it return the name(s) of the computer(s) their currently logged into. Sorry, I'd offer more points on this one, but this is my first question on EE, and 125 is all i've got, but you can have 'em all.

I currently have 2 methods to pull computer name from user id's :

METHOD 1 :
-------------------------------
[Use DOS to net send the user, and pull the info from the NBT cache]
> NET SEND jsmith2
> NBTSTAT -c

METHOD 2 :
-------------------------------
[use vbscript to pull home directory of user, open computer manager & connect to home directory computer & look in the sessions folder for users name & info]
[error checking & other non-essential code stripped]

Set oFs = CreateObject("Scripting.FileSystemObject")

Set oArgs = WScript.Arguments
UserName = oArgs(0)

Set myUser= GetObject("WinNT://WORKDOMAIN/" & UserName & ",user")
Set userClass = GetObject(myUser.Schema)

HomeDir = myUser.HomeDirectory
pos1 = InStrRev(myUser.HomeDirectory, "\")
HomeDir = Mid(myUser.HomeDirectory,3,pos1-3)

RunShellCmd("fsmgmt.msc -s /computer:\\"& HomeDir)
WScript.quit()



The problem with method 1, is that it isn't always current/reliable, and alerts the user on their PC when you net send them.
The problem with method 2, is that it isn't automated.  

There's got to be a way to enumerate/export the info in the sessions folder on a logon server (computer mgr), search for the username in the list, and pull the ip/hostname(s).

Thanks for any help u can offer.

Avatar of Gary Dewrell
Gary Dewrell
Flag of United States of America image

Hi Gitcho,

I can tell you a very low tech way to do it that I know works. If you are on a domain, add a line to the login script that writes the %computername% to a file in the users directory. Overwrite that file each time. Read that file from the known users directory.



God Bless
Avatar of Gitcho
Gitcho

ASKER

Thanks dgewrell.  Not a bad idea - hadn't thought of that one, but not sure that I could make that work.

That write-once approach wouldn't tell you if a user was still logged on to any of the hosts - unless there was a way to write back at logoff.
If the PC bluescreened, you'd also be out of luck.

It might help a bit, but really need a VBscript solution that works for the Helpdesk intranet page.
Keep in mind I am not a programmer so this may no make sence but it seams logical to me.

What about a comobination of the two. To remove the file just add that to the logoff script that can be specified via GPO.
That will solve the issue of them logging off.

Then you have a vbscript that looks in the user directory for the specified file and read it. If the file does not exist you know the user is no longer logged on. If the file exist you read it to get the name of the PC that the user last logged onto. The write the code into you VB script to determine if that user is still logged on.  If he is do what ever it is that you wanted to do. If not have the vbscript delete the file from the users directory.  I have no programmed since the vb3 days but Im sure this can all be done in vbscript. If nobody with more experiance gives you a good answer, let me know and Im sure I can write this in vbscript. AFter all you know what they say, once you've learned to ride a bike, you never forget. :)

God Bless
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gitcho

ASKER

gdewrell :
It's a bit of a work around, and may work, but if you can view the list of sessions in the computer manager snap for any remote logon server, you should be able to query the logon server for the same session list in VB - just like you can pull many other remote server attributes.

bluedevilfan :
I'm looking for a way to get the name(s) of the computer(s) that a user is logged onto, by passing only their network username to the program.   Let me check into that winnetmag.com link & get back to u ... thanks ...
Avatar of Gitcho

ASKER

bluedevilfan :

you're the man !  I've been looking everywhere for that ... works awesome. thanks.
Cool!  Glad I could help.