windows - 如何在 Perl 中以管理员模式打开命令提示符

标签 windows perl command-prompt perl-module

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

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

我可以右键单击 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");
    }
}

最佳答案

如果您能够以管理员身份运行 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 .

关于windows - 如何在 Perl 中以管理员模式打开命令提示符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22057506/

相关文章:

windows - 确定事件窗口名称或 ID

node.js - 如何从 Windows 卸载 mongodb

java - Eclipse 中的\bin 目录很繁琐

node.js - npm 全局列表不断变化

windows - PhoneGap错误: EPERM: Operation not permitted (windows 10)

windows - Windows 禁止文件和文件夹名称的完整列表

python - 如何在 Python 中调用 Perl 函数

perl - 如何使用 Perl DBI 在 Oracle 中超时 "select for update"

c++ - 无法将参数 2 从 'const BITMAPINFO *' 转换为 'const BITMAPINFO *'

perl - 为什么我可以将类名用作 Moose 类型,而不能用作类型联合的一部分?