waseemkotwal has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Is there any method by which one can determine in Perl if a user has administrator privileges? I need this in a Windows environment.

In case of Unix, I use /bin/id to see if UID is zero. But how about in Windows?

Thanks in advance for your help.
Waseem
  • Comment on How to determine if a user has administrator privileges in Windows environment

Replies are listed 'Best First'.
Re: How to determine if a user has administrator privileges in Windows environment
by Corion (Patriarch) on Feb 20, 2006 at 12:44 UTC

    The Win32::NetAdmin module has the function GroupIsMember, which tells you if a username is a member of the group. I think you can use it as follows:

    if GroupIsMember("", "Administrators", "") { print "The script is running with administrator privileges\n" } else { print "The script is running without administrator privileges\n" };
Re: How to determine if a user has administrator privileges in Windows environment
by planetscape (Chancellor) on Feb 20, 2006 at 14:10 UTC
Re: How to determine if a user has administrator privileges in Windows environment
by ikegami (Patriarch) on Feb 20, 2006 at 15:12 UTC

    In unix, I thought you also had to check if the GID is zero?

    In Windows, you ight be better off just trying your operation and check if it works, since it's actually possible to restrict the operations Administrator can do. (However, they can give themselves permissions to do something.)

Re: How to determine if a user has administrator privileges in Windows environment
by Anonymous Monk on Feb 20, 2006 at 15:30 UTC
    There is Win32::IsAdminUser() but this does not exist in older perls I believe...
Re: How to determine if a user has administrator privileges in Windows environment
by dragonchild (Archbishop) on Feb 20, 2006 at 15:59 UTC
    sub is_windows_admin { eval { open my $fh, "> C:/windows/system32/perl_check_if_windows_a +dmin" or die; 1; } && 1; } if ( is_windows_admin() ) { # Strut your stuff }
    Don't get fancy. Also, /bin/id isn't the best solution. Again, it's better to verify if you have the privileges you need vs. seeing if you're root. Your solution won't work in a sudo environment.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?