How to elevate Perl process using UAC

前端 未结 3 1010
野趣味
野趣味 2021-01-20 17:24

Suppose I have a script that needs to read information from places that normal user is not permitted to read (e.g. other users\' folders).

Currently all I can do is

相关标签:
3条回答
  • 2021-01-20 17:37

    If you're ok with launching through a shortcut, you can use the following:

    1. Create a shortcut to perl.exe
    2. Edit the shortcut.

      1. On the Shortcut tab, change "Target" to

        "c:...\bin\perl.exe" "c:...\script.pl"

      2. [Optional] On the Shortcut tab, change "Start in" to the path of the directory in which your script resides.

      3. On the Shortcut tab, click "Advanced", then check "Run as Administrator".

    There's a tool called "runas", but I can't seem to get it to work without asking you for Administrator's password.

    0 讨论(0)
  • 2021-01-20 17:41

    According to MSDN and PerlMonks, you can try:

    Win32::FileOp::ShellExecute( runas => 'yourprogram.exe' )
    

    or

    Win32::FileOp::ShellExecute( runasuser => 'yourprogram.exe' )
    

    These should (not tested) ask you for elevation when it is needed. (Works on Windows 7 only).

    Related: Requesting Administrator privileges during runtime

    0 讨论(0)
  • 2021-01-20 17:56

    Tired of having no good answer to this question in my own work, I wrote Win32::RunAsAdmin. All it does is call the Windows Shell via OLE with "runas" as the verb, but it packages it conveniently so all you have to do is stick the following at the beginning of your code:

    use Win32::RunAsAdmin qw(force);
    

    During the import step, it will check for elevated privileges, and return silently if you're already running in elevated mode. Otherwise, it restarts the script in elevated mode with a UAC popup.

    0 讨论(0)
提交回复
热议问题