How to elevate Perl process using UAC

怎甘沉沦 提交于 2019-12-31 02:30:50

问题


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 complain about not being run as a "superuser" and quit, but I would rather like the script to ask for elevation itself.


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/11019571/how-to-elevate-perl-process-using-uac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!