Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

windows - Run batch script as admin during Maven build

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:WindowsNotepad.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:WindowsMicrosoft.NETFrameworkv4.0.30319vbc "%userprofile%DesktopRunAsAdmin.vb" /win32manifest:"%userprofile%DesktopRunAsAdmin.manifest" /out:"%userprofile%DesktopRunAsAdmin.exe" /target:winexe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...