Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

runas - mkdir in batch file as admin

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-pcadmin "mkdir C:Program FilesMyDir"
runas /user:admin-pcadmin "mkdir "C:Program FilesMyDir""
runas /user:admin-pcadmin "cmd /c mkdir "C:Program FilesMyDir""
runas /user:admin-pcadmin "cmd /c mkdir %programfiles%MyDir"
runas /user:admin-pcadmin "cmd /c mkdir "C:/Program Files/MyDir""
runas /user:admin-pcadmin "cmd /c mkdir C:Program^ FilesMyDir"

What's the right way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 FilesMyDir"

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:WindowsMicrosoft.NETFrameworkv4.0.30319vbc "%userprofile%DesktopRunAsAdminconsole.vb" /win32manifest:"%userprofile%DesktopRunAsAdmin.manifest" /out:"%userprofile%DesktopRunAsAdminConsole.exe" /target:exe

A file called RunAsAdminConsole.exe will appear on the desktop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...