以管理员身份在批处理文件中 mkdir
mkdir in batch file as admin
我正在尝试编写一个在 %programfiles%
中创建新目录的批处理文件。它需要 运行 作为管理员。我试过:
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 特有的。
您可以制作 IExpress 安装程序并使用 ResHacker 将其清单替换为我的答案中的清单。 http://angusj.com/resourcehacker 资源类型 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
此答案是特定于控制台程序的 - 请参阅 了解更通用的方法。
要在 Windows 中提升,推荐的方法是嵌入清单。对于基于文本的程序,这是做不到的。很容易将vbscript放入VB.NET,添加一个manifest,然后编译。
当前的脚本方法模仿右键单击然后 运行 以管理员身份。这仅在文件关联是 Windows' 默认值时有效,例如,用户可以通过自定义他们的系统来停止此方法。
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。根据您的喜好将此行从 /k
更改为 /c
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
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的文件将出现在桌面上。
我正在尝试编写一个在 %programfiles%
中创建新目录的批处理文件。它需要 运行 作为管理员。我试过:
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 特有的。
您可以制作 IExpress 安装程序并使用 ResHacker 将其清单替换为我的答案中的清单。 http://angusj.com/resourcehacker 资源类型 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
此答案是特定于控制台程序的 - 请参阅
要在 Windows 中提升,推荐的方法是嵌入清单。对于基于文本的程序,这是做不到的。很容易将vbscript放入VB.NET,添加一个manifest,然后编译。
当前的脚本方法模仿右键单击然后 运行 以管理员身份。这仅在文件关联是 Windows' 默认值时有效,例如,用户可以通过自定义他们的系统来停止此方法。
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。根据您的喜好将此行从 /k
更改为 /c
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
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的文件将出现在桌面上。