[RESOLVED] Getting UserName from Computer Name
Hello to all.
I was wondering if you can get the username of the person logged into a computer if I only have the computer name.
Meaning I want to have some VB code to pass the computer name to and get the person presently logged in.
Can this be done?
Dane
Re: Getting UserName from Computer Name
Try msome thing like this:
Code:
Private Sub Command1_Click()
Dim strComputer As String
Dim oWMIService As Object
Dim oComputers As Object
Dim oComputer As Object
On Error Resume Next
strComputer = "abc..."
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\xxxx\xxxxx")
Set oComputers = oWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each oComputer In oComputers
List1.AddItem oComputer.UserName
Next
End Sub
Re: Getting UserName from Computer Name
Thanks Rhino this seems to get me going but I still have two questions..
Code:
Public Function getUsrFromCompName(strComputer As String) As String
Dim oWMIService As Object
Dim oComputers As Object
Dim oComputer As Object
Dim strComp As Variant
On Error Resume Next
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\xxxx\xxxxx")
Set oComputers = oWMIService.ExecQuery("Select UserName from Win32_ComputerSystem")
For Each oComputer In oComputers
'Debug.Print oComputer.UserName
getUsrFromCompName = oComputer.UserName
Next
End Function
Question:
1. The function call only seems to work when called from inside the loop. Why is that?
2. It seems like it only works on my computer when I am logged on. I belive it is because of the permisions the company has setup. They are not allowing me to see the username on any other computer. Is is assumption correct? And if so what permissions i need to tell the system adminsitrator to give me, if you know.
Again thanks for your help thus far.
Dallr
Re: Getting UserName from Computer Name
Of course you'd have to have permissions to access remote computer.
Re: Getting UserName from Computer Name
Thanks I did not know that!
Cheers!