PDA

View Full Version : logged in Username of remote machine



doodleman99
08-19-2011, 06:14 AM
Hi there,
I have a little problem!
I have an asset DB that i use to administrate users/machine on our domain.
as SysAdmin i sometimes need to force a reboot on machines which is all fine and dandy, but the other day, i accidentally forced the reboot of a machine which was being used by a user and she REALLY wasnt too impressed (lol).
to prevent this, i need a little msgbox yes/no to confirm the user of the machine before it actually reboots.
something like
"Are you sure you want to reboot " & MachineName & " while the user " & UserName & " is logged in??"

there is a nice little feature to get the username of your local machine:

UserNameWindows = Environ("USERNAME")
MsgBox (UserNameWindows)

But i need to be able to specify a remote machine Name/IP instead?!?!

Any ideas?!!

Charrrs,
JV

hansup
08-19-2011, 11:33 AM
First let me warn you about Environ("USERNAME"), although it's not your immediate concern. That method is insecure because a user can open a command window, set USERNAME to anything, then start up MSACCESS.EXE from that environment. If you ever do need the current user name, use the Windows API instead instead of Environ().

Regarding your actual question, I ran across a WMI approach which you may be able to use. This is a VBA adaptation of VBScript code I found somewhere, but forgot where. I played with it briefly, and hope it either gives you what you need or points you to something useful.

Function GetUser(strComputer)
Dim colCompSys As Object
Dim objComputer As Object
Dim objWMI As Object

Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colCompSys = objWMI.ExecQuery("SELECT UserName FROM Win32_ComputerSystem")

For Each objComputer In colCompSys
GetUser = objComputer.UserName
Next
End Function