It is currently November 13th, 2019, 6:03 am

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

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

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

raiguard » June 22nd, 2018, 2:54 am

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.
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
eclectic-tech
Rainmeter Sage
Posts: 3592
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

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

eclectic-tech » July 4th, 2018, 2:51 am

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