[英]Batch file to run a command in Administrator Command Prompt
I've created MyApplication.msi package using WiX Installer, kept in D drive and running it via Administrator Command Prompt as follows: 我使用WiX安装程序创建了MyApplication.msi程序包,将其保存在D驱动器中,并通过管理员命令提示符运行它,如下所示:
The package gets started and runs as required. 该程序包将启动并根据需要运行。
Now, I need a batch file which will be able to do above steps automatically. 现在,我需要一个批处理文件,该文件将能够自动执行上述步骤。
How can this be done? 如何才能做到这一点?
You can use standard runas windows command like this 您可以像这样使用标准的runas Windows命令
runas /user:administrator D:\MyApplication.msi
but it may ask for password. 但可能会要求输入密码。
Another option is to use sysinternals.com (now it is belongs to Microsoft) tools 另一个选择是使用sysinternals.com(现在它属于Microsoft)工具
psexec -u UserName -p password D:\MyApplication.msi
Or you can right click you program - Properties and tick Run As Administrator on the Compatability tab. 或者,您可以右键单击您的程序-属性,然后在兼容性选项卡上勾选以管理员身份运行。
VBScript can automate Right Click - RunAs (in Vista and later RunAs elevates to Admin token - XP and before allows you to choose a different user). VBScript可以自动执行右键单击-RunAs(在Vista和更高版本中,RunAs提升为Admin令牌-XP,并且在允许您选择其他用户之前)。 RunAs appears on the menu as Run As Administrator (Vista and later). RunAs以“以管理员身份运行”(Vista和更高版本)出现在菜单上。
Here's a vbscript program that executes verbs on files. 这是一个在文件上执行动词的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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.