Sign in to follow this  
Followers 0
mary

get current username when script run as service

9 posts in this topic

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 ?

Share this post


Link to post
Share on other sites



;-) Your two Avatars below each other are really giving a funny combination:

- a beatuiful lady

- a bit-byte-keyboard maniac without face

<scnr>

;-)


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Maybe a check on "explorer.exe" with the _ProcessGetOwner() does the trick?

_ProcessGetOwner() gives the pid of "lancher" and not the owner user

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

This works if NOT called from a service, give it a try ;)...

#include<_ProcessFunctions.au3>
$hProcess = _ProcessOpen(ProcessExists("explorer.exe"),0x20000)
MsgBox(0,"",_ProcessGetOwner($hProcess))
_ProcessCloseHandle($hProcess)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Holy.....

Even more simple:

There is a .EXE inside XP/Vista/Win7

QWINSTA.EXE

Good idea !

Thanks for all your helps ( KaFu and Tankbuster)

Edited by mary

Share this post


Link to post
Share on other sites
;) Thx, you're welcome

Share this post


Link to post
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
Sign in to follow this  
Followers 0