Jump to content

get current username when script run as service


mary
 Share

Recommended Posts

my script run as service.

@username return "SYSTEM"

my question: how to get to current username or access to his HKCU from my service?

MS says in LocalSystem account, the registry key HKEY_CURRENT_USER is associated with the default user, not the current user. To access another user's profile, impersonate the user

any help please ?

Link to comment
Share on other sites

I do not know the solution, but just want to add a possible gap in the solution.

Keep in mind, that maybe multiple users are logged in (not often but possible)

But in the Win32_LogonSession Class you may find something to help you

http://msdn.microsoft.com/en-us/library/aa394189%28VS.85%29.aspx

I will try it with my service.....and it worked:

I used a VBS like this

Start it with: cscript getuser.vbs //nologo

strComputer = "."   ' " use "." for local computer

Set objWMI = GetObject("winmgmts:" _
              & "{impersonationLevel=impersonate}!\\" _
              & strComputer & "\root\cimv2")

Set colSessions = objWMI.ExecQuery _
    ("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10")

If colSessions.Count = 0 Then
   Wscript.Echo "No interactive users found"
Else
   For Each objSession in colSessions
     Set colList = objWMI.ExecQuery("Associators of " _
         & "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _
         & "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )

     For Each objItem in colList
       WScript.Echo "User: " & objItem.Name
       WScript.Echo "FullName: " & objItem.FullName
       WScript.Echo "Domain: " & objItem.Domain
     Next
     Wscript.Echo "Session start time: " & objSession.StartTime
   Next
End If

I tested this script running as a service with Windows XP Pro (Sp2). User was System while the .vbs gave me the logged in user. And if two or more users are logged in at the same pc, you will get all users from the vbs.

Maybe there is a autoit function/udf?

Link to comment
Share on other sites

Holy.....

Even more simple:

There is a .EXE inside XP/Vista/Win7

QWINSTA.EXE

Also for Remote

Gives output like this

SESSIONNAME       USERNAME             ID  STATE  TYP         DEVICE
 services                               0   active wdcon
>console           test                 1   Disc   wdcon
Edited by Tankbuster
Link to comment
Share on other sites

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...