2

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:

<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>

The problem that I'm running into is, that to be able to control services, you need to have adminsitrative rights. My user is local admin so the script works if I run it in an elevated prompt (right click->'Run as Adminsitrator').

I've tried using runas /user:administrator but it's prompting for a password. I could run the Maven build itself as admin but I'd like to run it from environments where this might not be possible (Eclipse, Jenkins).

Does anyone have an idea on how to implement the described scenario?

André Stannek
  • 7,773
  • 31
  • 52
  • Why would you like to run an service during a build ? – khmarbaise Dec 20 '17 at 09:10
  • @khmarbaise It'll take too long to explain in detail. Let's just say I'd like to "mavenize" the build process of a product/framework we use and have little influence on. – André Stannek Dec 20 '17 at 09:13
  • 1
    This may give you some ideas https://stackoverflow.com/questions/47895544/mkdir-in-batch-file-as-admin/47896026#47896026 – ACatInLove Dec 20 '17 at 09:14
  • @ACatInLove this is a step in the right direction. When I use this, UAC pops up (which is fine for now) and after that the script is run as admin. The problem is, that it is starting a new shell and therefore not waiting until stratup is finished. I will experiment with it further to see if I can work around it. Thanks! – André Stannek Dec 20 '17 at 09:28
  • @ACatInLove I got it to work :-) I will post my modifications as an answer soon. The questions stays open for other creative solutions. – André Stannek Dec 20 '17 at 09:45

2 Answers2

2

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
ACatInLove
  • 530
  • 2
  • 5
0

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

André Stannek
  • 7,773
  • 31
  • 52