gpt4 book ai didi

windows - 在 Maven 构建期间以管理员身份运行批处理脚本

转载 作者:可可西里 更新时间:2023-11-01 11:44:53 25 4
gpt4 key购买 nike

我正在尝试设置构建过程,在此过程中必须启动和停止 Windows 服务。我尝试使用 exec-maven-plugin 来做到这一点:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>startServer</id>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${project.basedir}/bin/startService.cmd</executable>
<workingDirectory>${project.basedir}/bin</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>

我遇到的问题是,要能够控制服务,您需要拥有管理权限。我的用户是本地管理员,因此如果我在提升的提示符下运行该脚本(右键单击 ->“以管理员身份运行”),该脚本将起作用。

我试过使用 runas/user:administrator 但它提示输入密码。我可以以管理员身份运行 Maven 构建本身,但我想在这可能无法实现的环境(Eclipse、Jenkins)中运行它。

有没有人知道如何实现所描述的场景?

最佳答案

这是我解决高程问题的另一半mkdir in batch file as admin .上半场是特定于控制台的。这是更通用的非控制台解决方案。 WshShell.Run 作为控制台程序效果不佳,因此使用了 VB/VBA shell 命令。 WshShell.Run 有一个窗口样式(0 是隐藏的)和一个是否等待应用程序的标志。

将文件放在桌面上。它们必须是 ANSI。

RunAsAdmin.vb

imports System.Runtime.InteropServices 
Public Module MyApplication

Public Sub Main ()
Dim wshshell as object
WshShell = CreateObject("WScript.Shell")
'8 is non active window, true means wait for exit
WshShell.Run("""C:\Windows\Notepad.exe""",8, true)
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>Serenity's Editor</description>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>

</assembly>

以及编译命令。

C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%userprofile%\Desktop\RunAsAdmin.vb" /win32manifest:"%userprofile%\Desktop\RunAsAdmin.manifest" /out:"%userprofile%\Desktop\RunAsAdmin.exe" /target:winexe

关于windows - 在 Maven 构建期间以管理员身份运行批处理脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47902221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com