Windows Vista:如何在cmd“以管理员身份”中执行.exe程序


11

对,就是那样。我需要在dos cmd行中运行一个程序.exe,它需要像右键单击并选择“以管理员身份运行”一样运行


我也想知道。我的解决方案是使用命令创建批处理文件,并使其始终以admin身份运行,或者以cmd身份打开cmd。但是感觉应该有一些优雅的解决方案。
OregonGhost

Answers:



7

如果您以管理员身份运行cmd本身,那么从那里运行的所有其他操作也将同时运行。我只是设置了以管理员身份打开的命令提示符的快捷方式。那里的一切都很好。


5

我使用了.cmd和.vbs脚本的二重奏,称为elevate,它可以很好地满足我的需求。您只需要输入

提升[命令]
从“开始”>“运行”或从命令行,它将以管理员权限运行命令。希望能帮助到你!

这是elevate.cmd:

:: //***************************************************************************
:: // ***** Script Header *****
:: //
:: // File:      Elevate.cmd
:: //
:: // Additional files required:  Elevate.vbs
:: //
:: // Purpose:   To provide a command line method of launching applications that
:: //            prompt for elevation (Run as Administrator) on Windows Vista.
:: //
:: // Usage:     elevate.cmd application <application arguments>
:: //
:: // Version:   1.0.0
:: // Date :     01/02/2007
:: //
:: // History:
:: // 1.0.0   01/02/2007  Created initial version.
:: //
:: // ***** End Header *****
:: //***************************************************************************


@echo off

:: Pass raw command line agruments and first argument to Elevate.vbs
:: through environment variables.
set ELEVATE_CMDLINE=%*
set ELEVATE_APP=%1

start wscript //nologo "%~dpn0.vbs" %*

和elevate.vbs:

' //***************************************************************************
' // ***** Script Header *****
' //
' // File:      Elevate.vbs
' //
' // Additional files required:  Elevate.cmd
' //
' // Purpose:   To provide a command line method of launching applications that
' //            prompt for elevation (Run as Administrator) on Windows Vista.
' //
' // Usage:     (Not used directly.  Launched from Elevate.cmd.)
' //
' // Version:   1.0.1
' // Date :     01/03/2007
' //
' // History:
' // 1.0.0   01/02/2007  Created initial version.
' // 1.0.1   01/03/2007  Added detailed usage output.
' //
' // ***** End Header *****
' //***************************************************************************


Set objShell = CreateObject("Shell.Application")
Set objWshShell = WScript.CreateObject("WScript.Shell")
Set objWshProcessEnv = objWshShell.Environment("PROCESS")

' Get raw command line agruments and first argument from Elevate.cmd passed
' in through environment variables.
strCommandLine = objWshProcessEnv("ELEVATE_CMDLINE")
strApplication = objWshProcessEnv("ELEVATE_APP")
strArguments = Right(strCommandLine, (Len(strCommandLine) - Len(strApplication)))

If (WScript.Arguments.Count >= 1) Then
    strFlag = WScript.Arguments(0)
    If (strFlag = "") OR (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
        OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "-?") OR (strFlag="h") _
        OR (strFlag = "?") Then
        DisplayUsage
        WScript.Quit
    Else
        objShell.ShellExecute strApplication, strArguments, "", "runas"
    End If
Else
    DisplayUsage
    WScript.Quit
End If


Sub DisplayUsage

    WScript.Echo "Elevate - Elevation Command Line Tool for Windows Vista" & vbCrLf & _
                 "" & vbCrLf & _
                 "Purpose:" & vbCrLf & _
                 "--------" & vbCrLf & _
                 "To launch applications that prompt for elevation (i.e. Run as Administrator)" & vbCrLf & _
                 "from the command line, a script, or the Run box." & vbCrLf & _
                 "" & vbCrLf & _
                 "Usage:   " & vbCrLf & _
                 "" & vbCrLf & _
                 "    elevate application <arguments>" & vbCrLf & _
                 "" & vbCrLf & _
                 "" & vbCrLf & _
                 "Sample usage:" & vbCrLf & _
                 "" & vbCrLf & _
                 "    elevate notepad ""C:\Windows\win.ini""" & vbCrLf & _
                 "" & vbCrLf & _
                 "    elevate cmd /k cd ""C:\Program Files""" & vbCrLf & _
                 "" & vbCrLf & _
                 "    elevate powershell -NoExit -Command Set-Location 'C:\Windows'" & vbCrLf & _
                 "" & vbCrLf & _
                 "" & vbCrLf & _
                 "Usage with scripts: When using the elevate command with scripts such as" & vbCrLf & _
                 "Windows Script Host or Windows PowerShell scripts, you should specify" & vbCrLf & _
                 "the script host executable (i.e., wscript, cscript, powershell) as the " & vbCrLf & _
                 "application." & vbCrLf & _
                 "" & vbCrLf & _
                 "Sample usage with scripts:" & vbCrLf & _
                 "" & vbCrLf & _
                 "    elevate wscript ""C:\windows\system32\slmgr.vbs"" –dli" & vbCrLf & _
                 "" & vbCrLf & _
                 "    elevate powershell -NoExit -Command & 'C:\Temp\Test.ps1'" & vbCrLf & _
                 "" & vbCrLf & _
                 "" & vbCrLf & _
                 "The elevate command consists of the following files:" & vbCrLf & _
                 "" & vbCrLf & _
                 "    elevate.cmd" & vbCrLf & _
                 "    elevate.vbs" & vbCrLf

End Sub

只是好奇,相对于符文来说,这有什么好处?
tfinniga's

h!你懂我!:)我不知道与runas相比的优势,但是当您不知道runas时,这是唯一的选择。我绝对不好意思:)

我认为,一旦我们阅读了用作脚本的内容,那将会有所帮助!


2
elevate脚本的明显优势是,它们使您能够在UAC下将用户提升为管理员,而无需提供admin用户和密码(前提是您以admin用户身份登录)。可以运行runas.exe吗?


2

您也可以按Windows键,键入CMD,这将在程序中列出cmd,右键单击它->属性->兼容性->以管理员身份运行此程序

这将始终以管理员身份运行。


您也可以使用Ctrl + Shift + Enter。
乔伊(Joey)

1

由于有人发布了等效的VBS,因此这是PowerShell的Invoke-Admin函数

function Invoke-Admin() {
param ( [string]$program = $(throw "Please specify a program" ),
        [string]$argumentString = "",
        [switch]$waitForExit )

$psi = new-object "Diagnostics.ProcessStartInfo"
$psi.FileName = $program 
$psi.Arguments = $argumentString
$psi.Verb = "runas"
$proc = [Diagnostics.Process]::Start($psi)
if ( $waitForExit ) {
    $proc.WaitForExit();
}

}


0

简而言之:

  • 单击/按开始,然后按CTRL + SHIFT和ENTER。确认。

详细地:

  1. 按下“开始”按钮立即键入“ cmd”(不带引号)(1)
  2. 按住CTRL和SHIFT键
  3. 按ENTER
  4. 确认Windows Vista UAC对话框

(1)立即意味着在“搜索”框中而不是在“运行”框中键入它。


0

您可以这样使用VBScript。创建一个.vbs文件,例如ambika.vbs

Set objShell = CreateObject(“Shell.Application”)
Set objWshShell = WScript.CreateObject(“WScript.Shell”)
Set objWshProcessEnv = objWshShell.Environment(“PROCESS”)

objShell.ShellExecute “C:\Windows\system32\cmd.exe”, “/k”, “”, “runas”
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.