如何从批处理脚本调用管理员命令提示符而无需输入密码

时间:2014-04-17 12:08:53

标签: batch-file cmd

我正在尝试使用嵌入在.bat文件中的installUtil命令来自动执行Windows服务安装任务。我的操作系统是Windows 2012 Server标准版。

每当我手动执行此操作时,我都需要调用管理员命令提示符来运行InstallUtil命令。即使我的登录帐户具有管理权限,当我调用命令提示符时,将路径更改为“C:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319”,然后运行命令:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil "Path of my executables"

我收到一条错误消息“安装失败,并且已执行回滚”

要解决上述错误,我右键单击“Windows系统”下的“命令提示符”,然后选择“以管理员身份运行”,这将调用管理员命令提示符,即“管理员:命令提示符”标题”。当我在此提示符上运行上述InstallUtil命令时,Windows服务安装正常。 我不需要输入管理员密码以便以这种方式调用管理员命令提示并在其上运行命令。

现在我尝试使用.bat文件自动完成与上面相同的过程。

在.bat文件中,我尝试输入以下命令:

InstallUtil /runas /user:MyMachine\Administrator

但是当我运行批处理文件时(通过双击并使用“以管理员身份运行”选项),系统会提示我输入管理员密码。我尝试为InstallUtil命令提供各种选项,例如/ nouac,/ noprofile,/ env等,但它每次都提示我输入管理员密码,这是我没有的。

任何想法如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

这是不可能的,你不能自动授予文件管理员权限。如果这是可能的,那么它将击败UAC的目标。这将允许各种漏洞开放。

答案 1 :(得分:0)

这是使用shell高程动词RUNAS提升自身的vbscript。

Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "wscript.exe", chr(34) & WScript.ScriptFullName & Chr(34) & " " & sParms, , "runas", 1
Msgbox "error is " & err.number & " " & err.description

这是一个脚本,允许您列出文件的动词或执行文件的动词。

HelpMsg = vbcrlf & "  ShVerb" & vbcrlf & vbcrlf & "  David Candy 2014" & vbcrlf & vbcrlf & "  Lists or runs an explorer verb (right click menu) on a file or folder" & vbcrlf  & vbcrlf & "    ShVerb <filename> [verb]" & vbcrlf & vbcrlf & "  Used without a verb it lists the verbs available for the file or folder" & vbcrlf & vbcrlf
HelpMsg = HelpMsg & "  The program lists most verbs but only ones above the first separator" & vbcrlf & "  of the menu work when used this way" & vbcrlf & vbcrlf 
HelpMsg = HelpMsg & "  The Properties verb can be used. However the program has to keep running" & vbcrlf & "  to hold the properties dialog open. It keeps running by displaying" & vbcrlf & "  a message box." 
Set objShell = CreateObject("Shell.Application")
Set Ag = WScript.Arguments 
set WshShell = WScript.CreateObject("WScript.Shell") 
Set fso = CreateObject("Scripting.FileSystemObject")

    If Ag.count = 0 then 
        wscript.echo "  ShVerb - No file specified"
        wscript.echo HelpMsg 
        wscript.quit
    Else If Ag.count = 1 then 
        If LCase(Replace(Ag(0),"-", "/")) = "/h" or Replace(Ag(0),"-", "/") = "/?" then 
            wscript.echo HelpMsg 
            wscript.quit
        End If
    ElseIf Ag.count > 2 then 
        wscript.echo vbcrlf & "  ShVerb - To many parameters" & vbcrlf & "  Use quotes around filenames and verbs containing spaces"  & vbcrlf
        wscript.echo HelpMsg 
        wscript.quit
    End If

    If fso.DriveExists(Ag(0)) = True then
        Set objFolder = objShell.Namespace(fso.GetFileName(Ag(0)))
'       Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
        Set objFolderItem = objFolder.self
        msgbox ag(0)
    ElseIf fso.FolderExists(Ag(0)) = True then
        Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
        Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
    ElseIf fso.fileExists(Ag(0)) = True then
        Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
        Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
    Else
        wscript.echo "  ShVerb - " & Ag(0) & " not found"
        wscript.echo HelpMsg 
        wscript.quit
    End If

    Set objVerbs = objFolderItem.Verbs

    'If only one argument list verbs for that item

    If Ag.count = 1 then
        For Each cmd in objFolderItem.Verbs
            If len(cmd) <> 0 then CmdList = CmdList & vbcrlf & replace(cmd.name, "&", "") 
        Next
        wscript.echo mid(CmdList, 2)

    'If two arguments do verbs for that item

    ElseIf Ag.count = 2 then
        For Each cmd in objFolderItem.Verbs
            If lcase(replace(cmd, "&", "")) = LCase(Ag(1)) then 
                wscript.echo Cmd.doit 
                Exit For
            End If
        Next
    'Properties is special cased. Script has to stay running for Properties dialog to show.
        If Lcase(Ag(1)) = "properties" then
            WSHShell.AppActivate(ObjFolderItem.Name & " Properties")
            msgbox "This message box has to stay open to keep the " & ObjFolderItem.Name & " Properties dialog open."
        End If  
    End If
End If