It is currently March 28th, 2024, 11:31 am

Detecting the current user account type (admin vs. standard)

Tips and Tricks from the Rainmeter Community
Post Reply
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA
Contact:

Detecting the current user account type (admin vs. standard)

Post by raiguard »

AFAIK there is no direct and straightforward way to tell whether the current user is an administrator or not. After a bit of fiddling around, I was able to find a command that did the trick when combined with a few other measures and values.

Code: Select all

[MeasureUserName]
Measure=Plugin
Plugin=SysInfo
SysInfoType=USER_NAME
IfCondition=1
IfTrueAction=[!CommandMeasure MeasureGetLocalGroup "Run"]

[MeasureGetLocalGroup]
Measure=Plugin
Plugin=RunCommand
Parameter=net localgroup Administrators
OutputType=ANSI
FinishAction=[!EnableMeasure MeasureUserAdmin][!UpdateMeasure MeasureUserAdmin]

[MeasureUserAdmin]
Measure=String
String=[MeasureGetLocalGroup]
IfMatch=(?s).*\b[MeasureUserName:EscapeRegExp]\b.*
IfMatchAction=[!Log "User '[MeasureUserName]' is an administrator"]
IfNotMatchAction=[!Log "User '[MeasureUserName]' is not an administrator"]
DynamicVariables=1
Disabled=1
This code retrieves the current username, then runs a command that lists all users in the "Administrators" user group. The third measure runs that command output through Regex to determine whether the given username is included in that group. The regex ensures a match only if the exact username is by itself (its own word). For example, if the username is 'Ben', who is a standard user, and there is another user 'Benjamin' who is an admin, the regex will not misidentify 'Ben' as an admin.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5380
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA
Contact:

Re: Detecting the current user account type (admin vs. standard)

Post by eclectic-tech »

Thanks for sharing! :rosegift:

I looked at the "net" command's ability to list user groups, but never took the time to put it to use. :uhuh:

So thanks for saving me from figuring the extra steps to parse the string. :thumbup:

Adding this to my toolkit! :17nodding
Post Reply