Run batch script as admin during Maven build

后端 未结 2 592
野趣味
野趣味 2020-12-07 04:27

I\'m trying to set up a build process during which, a Windows service has to be started and stopped. I tried doing that by using the exec-maven-plugin:

相关标签:
2条回答
  • 2020-12-07 04:35

    This is the other half of my solution to elevation mkdir in batch file as admin. The first half was console specific. This is the more general non console solution. WshShell.Run didn't work well as a console program, hence the use of VB/VBA shell command. The WshShell.Run has a window style (0 is hidden) and a flag to wait on the app or not.

    Put files on the desktop. They must be 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>
    

    And the command to compile.

    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
    
    0 讨论(0)
  • 2020-12-07 04:59

    Thanks to @ACatInLove I have one working solution. It is based on their answer to mkdir in batch file as admin

    The problem was that it was starting a new shell and not waiting for the batch script to finish. I had to modify the script to do so. I found a was to do that at How to wait for a shell process to finish before executing further code in VB6

    This is the script:

    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
    

    I copied the resulting .exe into my project folder and changed the exec-maven-plugin configuration to

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

    The only restriction is that UAC pops up but I was anticipating that.

    I will leave my question open to other suggestions for now. Please give @ACatInLove some credit over at mkdir in batch file as admin

    0 讨论(0)
提交回复
热议问题