I'm trying to write a batch file that creates a new directory in %programfiles%
. It needs to be run as admin. I've tried:
runas /user:admin-pc\admin "mkdir C:\Program Files\MyDir"
runas /user:admin-pc\admin "mkdir \"C:\Program Files\MyDir\""
runas /user:admin-pc\admin "cmd /c mkdir \"C:\Program Files\MyDir\""
runas /user:admin-pc\admin "cmd /c mkdir %programfiles%\MyDir"
runas /user:admin-pc\admin "cmd /c mkdir \"C:/Program Files/MyDir\""
runas /user:admin-pc\admin "cmd /c mkdir C:\Program^ Files\MyDir"
What's the right way to do this?
The question turned out to be IExpress specific.
You cam make your IExpress installer and use ResHacker to replace it's manifest with the one in my answer. http://angusj.com/resourcehacker It's Resource type 24.
Wow. I never would have believed it. Worked like a charm! Opened it in ResHacker, changed RequestedExecutionLevel in the manifest to level= "requireAdministrator" in ResHacker and saved. Zero defects. Thanks
This answer is console programs specific - see Run batch script as admin during Maven build for a more generic way.
To elevate in Windows the recommended way is by embedding a manifest. Which can't be done for text based programs. It's easy to put vbscript into VB.NET, add a manifest, and compile it.
Current scripting approaches mimic a right click and then Run As Administrator. This only works if file associations are Windows' defaults, eg the user can stop this approach from working by customising their system.
Be aware that runas does not provide the ability to launch an application with an elevated access token, regardless of whether it is a standard user with privileges like a Backup Operator or an administrator. The runas command grants the user the ability to launch an application with different credentials. ... If your program programmatically uses the runas command, ensure that it is not intended to launch an elevated process. https://msdn.microsoft.com/en-us/library/bb530410.aspx
To use
RunAsAdminConsole <CMD Command Line>
EG
RunAsAdminConsole mkdir "C:\Program Files\MyDir"
The files. Place each file on the desktop. The must be ANSI. Change this line from /k
to /c
as you prefer Shell("cmd /k " & Command())
RunAsAdminConsole.vb
imports System.Runtime.InteropServices
Public Module MyApplication
Public Sub Main ()
Dim wshshell as object
WshShell = CreateObject("WScript.Shell")
Shell("cmd /k " & Command())
End Sub
End Module
RunAsAdmin.Manifest
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="*"
name="Color Management"
type="win32"
/>
<description>RunAsAdminConsole</description>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
And the batch file RunAsAdminConsole.bat to compile above
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%userprofile%\Desktop\RunAsAdminconsole.vb" /win32manifest:"%userprofile%\Desktop\RunAsAdmin.manifest" /out:"%userprofile%\Desktop\RunAsAdminConsole.exe" /target:exe
A file called RunAsAdminConsole.exe will appear on the desktop.
来源:https://stackoverflow.com/questions/47895544/mkdir-in-batch-file-as-admin