批处理文件中的mkdir以管理员身份 [英] mkdir in batch file as admin

查看:150
本文介绍了批处理文件中的mkdir以管理员身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个在%programfiles%中创建新目录的批处理文件.它需要以管理员身份运行.我尝试过:

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"

什么是正确的方法?

推荐答案

问题原来是针对IExpress的.

The question turned out to be IExpress specific.

您可以制作IExpress安装程序,并使用ResHacker将其清单替换为我的答案中的清单. http://angusj.com/resourcehacker 这是资源类型24.

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.

哇.我永远也不会相信.像魅力一样工作!在里面打开 ResHacker,将清单中的RequestedExecutionLevel更改为level = 在ResHacker中保存并保存"requireAdministrator".零缺陷.谢谢

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

此答案是特定于控制台程序的-请参阅运行在Maven构建过程中以管理员身份执行批处理脚本,以更通用的方式.

This answer is console programs specific - see Run batch script as admin during Maven build for a more generic way.

要在Windows中提升,建议的方法是嵌入清单.对于基于文本的程序无法做到这一点.将vbscript放入VB.NET,添加清单并进行编译很容易.

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.

当前的脚本编写方法模仿右键单击,然后模仿以管理员身份运行".这仅在文件关联是Windows的默认设置时才有效,例如,用户可以通过自定义其系统来停止这种方法.

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.

请注意,runas不提供启动程序的功能. 具有提升的访问令牌的应用程序,无论是否 具有特权的标准用户,例如备份操作员或 行政人员. runas命令授予用户启动功能 具有不同凭据的应用程序. ...如果您的程序 以编程方式使用runas命令,请确保它不是 旨在启动提升流程. https://msdn.microsoft.com/en-us/library/bb530410. aspx

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

要使用

 RunAsAdminConsole <CMD Command Line>

EG

 RunAsAdminConsole mkdir "C:\Program Files\MyDir"

文件.将每个文件放在桌面上.必须为ANSI.根据您的喜好Shell("cmd /k " & Command())

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>

以及要在上面编译的批处理文件 RunAsAdminConsole.bat

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

名为 RunAsAdminConsole.exe 的文件将出现在桌面上.

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

这篇关于批处理文件中的mkdir以管理员身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆