Detecting Local Admin for Win32

A

AgnosticPope

I need to check and see if the current user has admin rights on a
system (different from rights on the domain. It's possible for a user
to be a local admin with no admin rights on the domain). I'm
attempting to write a function to do this, but I'm having trouble.
Here's what I have so far.
##################

sub IsAdmin {

use Win32::NetAdmin qw(UserGetAttributes);
$domain = Win32::DomainName();
$machine = Win32::NodeName;
$_ = Win32::LoginName; # grab the name of the current user
UserGetAttributes($machine, $_, $pwd, $passwordAge, $privilege,
$homeDir, $comment, $flags, $scriptPath)
or die "UserGetAttributes() failed: $^E";
return ($privilege==USER_PRIV_ADMIN);
}

#################
This obviously doesn't work. Changing $machine to $domain works, but
doesn't give me what I'm look for. Anyone out there help?
 
M

MrReallyVeryNice

Hi AgnosticPope,

A few months back I had the same needs and I did a bit of research on
Google. No need to reinvent the wheel if someone has already done the work.
I came up with the following results:
http://www.faqchest.com/prgm/perlu-l/perl-03/perl-0305/perl-030500/perl03050
716_23494.html

I have been running a variant of the code provided in the above URL
successfully on W2K since then. Mileage might vary depending on your
operating system. I make no guarantee. :) Just because I'm nice and
honest, I'm pasting the original code here instead of giving you my script.
Give a try to:

use Win32::API;
$isadmin = new Win32::API('advpack.dll', 'IsNTAdmin', 'NN', 'N');
$status=$isadmin->Call(0,0);
if($status != 0) { print "You are an administrator."; }
else { print "You are not an administrator."; }

Let us know if that solves your problem.
MrReallyVeryNice (who gives credit where it is due!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top