【问题标题】:Batch file to run a command in Administrator Command Prompt在管理员命令提示符中运行命令的批处理文件
【发布时间】:2015-02-18 08:59:09
【问题描述】:

我已经使用 WiX 安装程序创建了 MyApplication.msi 包,保存在 D 驱动器中并通过管理员命令提示符运行它,如下所示:

  1. 右击cmd.exe - 以管理员身份运行
  2. C:\Windows\system32>"D:\MyApplication.msi"

包启动并按要求运行。

现在,我需要一个能够自动执行上述步骤的batch文件。

如何做到这一点?

【问题讨论】:

    标签: windows batch-file cmd package


    【解决方案1】:

    你可以像这样使用标准的 runas windows 命令

    runas /user:administrator D:\MyApplication.msi
    

    但它可能会要求输入密码。

    另一种选择是使用 sysinternals.com(现在它属于 Microsoft)工具

    psexec -u UserName -p password D:\MyApplication.msi
    

    【讨论】:

      【解决方案2】:

      或者您可以右键单击您的程序 - 属性并在兼容性选项卡上勾选以管理员身份运行。

      VBScript 可以自动化右键单击 - RunAs(在 Vista 和更高版本中,RunAs 提升为管理员令牌 - XP 及之前的版本允许您选择不同的用户)。 RunAs 在菜单上显示为 Run As Administrator(Vista 及更高版本)。

      这是一个对文件执行动词的 vbscript 程序。

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多