如何申请一个批处理文件中的管理员访问权限 [英] How to request Administrator access inside a batch file

查看:457
本文介绍了如何申请一个批处理文件中的管理员访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写我的用户从他们的Vista产品与UAC运行的批处理文件。该文件重新写自己的主机文件,因此它需要使用管理员权限运行。我需要能够向他们发送一个链接到.bat文件的电子邮件。期望的行为是,当他们在文件上单击鼠标右键,并说开放,他们将获得这些UAC对话框,使屏幕会变黑,并迫使他们回答他们是否想给以管理员身份运行该应用程序的权限之一。相反,他们只是看到拒绝访问命令行窗口中。

I am trying to write a batch file for my users to run from their Vista machines with UAC. The file is re-writing their hosts file, so it needs to be run with Administrator permissions. I need to be able to send them an email with a link to the .bat file. The desired behavior is that when they right-click on the file and say Open, they will get one of those UAC dialogs that makes the screen go dark and forces them to answer whether they want to give the application permission to run as administrator. Instead, they are just seeing "Access denied" on the command line window.

这是可以做到不同?

推荐答案

该脚本的伎俩!只需将其粘贴到您的bat文件的顶部。如果要查看脚本的输出,增加你的批处理文件底部的暂停命令。

This script does the trick! Just paste it into the top of your bat file. If you want to review the output of your script, add a "pause" command at the bottom of your batch file.

本脚本现在略加编辑,支持命令行参数和64位操作系统。

This script is now slightly edited to support command line args and 64 bit OS.

感谢您Eneerge @ <一个href=\"https://sites.google.com/site/eneerge/scripts/batchgotadmin\">https://sites.google.com/site/eneerge/scripts/batchgotadmin

Thank you Eneerge @ https://sites.google.com/site/eneerge/scripts/batchgotadmin

@echo off

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
    IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------    
    <YOUR BATCH SCRIPT HERE>

这篇关于如何申请一个批处理文件中的管理员访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆