PeterLundin
Tue Apr 05 01:57:04 CDT 2005
Ok. Thanks for pointing that out Torgeir!
//Peter
"Torgeir Bakken (MVP)" wrote:
> Peter Lundin wrote:
>
> > You could use WMI to determine logged on user on a specific computer:
> > -------------
> > strComputer = "computerName"
> > Set objWMIService = GetObject("winmgmts:" _
> > & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
> >
> > Set colComputer = objWMIService.ExecQuery _
> > ("Select * from Win32_ComputerSystem")
> >
> > For Each objComputer in colComputer
> > Wscript.Echo "Logged-on user: " & objComputer.UserName
> > Next
> > (snip)
> Hi
>
> Many have reported that Win32_ComputerSystem.UserName only works if the
> user on the remote computer has administrator rights on that computer.
>
> Obtaining the owner of the explorer.exe process should work better:
>
> '--------------------8<----------------------
>
> sUser = ConsoleUser(".") ' use "." for local computer
>
> MsgBox "Console user: " & sUser, _
> vbInformation + vbSystemModal, "Get user name"
>
>
> Function ConsoleUser(sHost)
> ' Returns name of user logged on to console
> ' If no users are logged on, returns ""
> Dim oWMI, colProc, oProcess, sUser, sDomain
> Set oWmi = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate,(debug)}!\\" _
> & sHost & "\root\cimv2")
>
> Set colProc = oWmi.ExecQuery("Select Name from Win32_Process" _
> & " Where Name='explorer.exe' and SessionID=0")
>
> ConsoleUser = ""
> For Each oProcess In colProc
> lRet = oProcess.GetOwner(sUser, sDomain)
> If lRet = 0 Then
> ConsoleUser = sUser
> End If
> Next
> End Function
> '--------------------8<----------------------
>
>
> --
> torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
> Administration scripting examples and an ONLINE version of
> the 1328 page Scripting Guide:
>
http://www.microsoft.com/technet/scriptcenter/default.mspx
>