【问题标题】:How to open command prompt in Administrator mode in Perl如何在 Perl 中以管理员模式打开命令提示符
【发布时间】:2014-02-27 01:53:30
【问题描述】:

我需要以管理员身份在命令提示符下运行命令。我平时使用system()命令发送命令在命令提示符下运行时,我没有Administrator级别的提升。

如何获得管理员级别的权限来运行我的指令?

我可以右键单击 cmd.exe 并选择“以管理员身份运行”以在管理员模式下手动打开命令提示符。

谢谢!

编辑

如果未安装 7-Zip,我正在尝试安装 7-Zip。 (您需要安装“wget”命令(*来自 Cygwin 或 GnuWin32)才能运行下面的代码片段)

use Cwd;
use File::Spec;
my $cwd = getcwd();
my $winpath = File::Spec->catdir($cwd);


if (!(-e "C:\\Program Files\\7-Zip")){
    print "\n 7-Zip is not installed. Downloading ... \n";
    system("wget http://downloads.sourceforge.net/project/sevenzip/7-Zip/9.20/7z920.exe?r=&ts=1392082197&use_mirror=softlayer-dal");   # SourceForge Mirror
    print "\n Installing.. \n\n";
    #print "\n Press \"install\" to install the 7-Zip installer on your pC\n\n";
    if(-e $winpath."\\7z920.exe"){
        system($winpath."\\7z920.exe");
    }
}

【问题讨论】:

标签: windows perl command-prompt perl-module


【解决方案1】:

如果您能够以管理员身份运行 cmd,那么您可以从该 cmd 实例运行您的 Perl 脚本,它将继承管理员权限。

您可以通过例如在c:\windows 下创建一个目录来进行测试:

use strict;

if (mkdir 'c:/windows/perltest/') {
    print "We have admin permissions";
    rmdir 'c:/windows/perltest/'
} else {
    print "No admin permission"
}

如果您在没有管理员权限的情况下运行并想要提升它们,请查看How to elevate Perl process using UAC

旁注:您可以跳过下载 7-Zip。 Perl 有many libraries that can handle zipping and unzipping

【讨论】:

    【解决方案2】:

    我发现了一个名为 elevate.exe 的小巧实用程序。

    它非常棒。我现在可以使用管理员权限发送/执行命令。

    http://code.kliu.org/misc/elevate/

    还是谢谢。

    (使用时请小心,它可能会损坏机器,并可能破坏您的系统。)

    【讨论】:

      猜你喜欢
      • 2018-08-10
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 2015-12-16
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多