Reputation: 3088
I created a service based on this code Is it possible to run a Python script as a service in Windows? If possible, how?
But when i run the code I can only run it as a service and not a normal process.
Is it possible to create a script that when it is run by an admin it runs as a service, but when its activated without admin privileges it runs as a normal process?
Upvotes: 2
Views: 193
Reputation: 5587
With win32service
(some docs here) you can also get the user info by calling the method GetUserObjectInformation
, with differente type
parameters to get one of UOI_FLAGS,UOI_NAME, UOI_TYPE, or UOI_USER_SID
. The complete list of user info types can be found here.
To have a look just print the info somewhere on your program, like the snippet below. If the user name is all that you need win32api
will get you there easier.
print win32api.GetUserName()
print win32service.GetUserObjectInformation( win32service.OpenInputDesktop(0,0,1), 4)
# or simply:
print win32service.GetUserObjectInformation( win32service.GetProcessWindowStation(), 4)
Finally, in order to get the privileges directly you can use win32net
like so:
win32net.NetUserGetInfo(None, win32api.GetUserName() , 11 )['priv']
This will return an int which maps to:
In your program you can check if win32net.NetUserGetInfo(None, win32api.GetUserName() , 11 )['priv'] == win32netcon.USER_PRIV_ADMIN
to launch it as a service or not.
For more information on the extensions you can find a brief reference here.
Upvotes: 1