代码之家  ›  专栏  ›  技术社区  ›  André Stannek

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

  •  2
  • André Stannek  · 技术社区  · 7 年前

    我正在尝试设置一个生成过程,在此过程中,必须启动和停止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)。

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

    2 回复  |  直到 7 年前
        1
  •  2
  •   ACatInLove    7 年前

    这是我的海拔解决方案的另一半 mkdir in batch file as admin . 上半部分是特定于控制台的。这是更通用的非控制台解决方案。 WshShell.Run 不能很好地作为控制台程序工作,因此使用VB/VBA shell 命令这个 WshShell。跑 有一个窗口样式(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。显示

    <?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
    
        2
  •  0
  •   André Stannek    7 年前

    感谢@ACatInLove,我有了一个有效的解决方案。这是基于他们对 mkdir in batch file as admin

    问题是它正在启动一个新的shell,而不是等待批处理脚本完成。我必须修改脚本才能这样做。我找到了一个在 How to wait for a shell process to finish before executing further code in VB6

    以下是脚本:

    imports System.Runtime.InteropServices 
    Public Module MyApplication  
        Public Sub Main ()
            Dim hProcess As Long
            Dim taskId As Long
            Dim wshshell as object
            WshShell = CreateObject("WScript.Shell")
            taskId = Shell("cmd /c " & Command())
            hProcess = OpenProcess(&H100000, True, taskId)
            Call WaitForSingleObject(hProcess, 10000)
            CloseHandle(hProcess)
        End Sub
        Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
        Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
        Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long 
    End Module
    

    我复制了结果。并更改了 exec-maven-plugin 配置到

    <executable>..\RunAsAdminConsole.exe startService.cmd</executable>
    

    唯一的限制是UAC弹出,但我早就预料到了。

    现在我将把我的问题留给其他建议。请给@ACatInLove一些积分 批处理文件中的mkdir作为管理员