How to elevate Perl process using UAC

♀尐吖头ヾ 提交于 2019-12-01 22:29:50
jm666

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

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.

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.

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