Batch files to install Windows Service as Administrator

15,353

Solution 1

Here is a solution that might be useful to somebody:

%WINDIR%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /username=domain\YourUserName /password=YourPassword YourApplication.exe

Solution 2

@echo off

SET PROG="c:\YourServiceLocation\Service.exe" SET FIRSTPART=%WINDIR%"\Microsoft.NET\Framework\v" SET SECONDPART="\InstallUtil.exe" SET DOTNETVER=4.0.30319 IF EXIST %FIRSTPART%%DOTNETVER%%SECONDPART% GOTO install

GOTO fail :install ECHO Found .NET Framework version %DOTNETVER% ECHO Installing service %PROG% %FIRSTPART%%DOTNETVER%%SECONDPART% %PROG% GOTO end :fail echo FAILURE -- Could not find .NET Framework install :param_error echo USAGE: installNETservie.bat [install type (I or U)] [application (.exe)] :end ECHO DONE!!! Pause

Run this bat file as administrator

Share:
15,353

Related videos on Youtube

Jeff B
Author by

Jeff B

Updated on September 18, 2022

Comments

  • Jeff B
    Jeff B over 1 year

    I'm trying to automate the install of a Windows Service for when I'm testing it locally. It is built in .NET... I'd usually open the Visual Studio Command Prompt as Administrator and then type the installutil command, I'm wanting to turn it into an "one-click" install.

    So I've created the following batch script to make sure the most recent version is built, then elevate myself to do the actual service install (UserAdmin is my administrator account).

    cd C:\Path\To\Solution
    echo Building Service
    msbuild WinService.sln /noconlog /nologo
    
    cd C:\Path\To\Solution\bin\Debug
    runas /user:UserAdmin "cmd /k C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil C:\Path\To\Solution\bin\Debug\WinService.exe"
    

    However, installutil always gives me the following error:

    An exception occurred during the Install phase.
    System.Security.SecurityException: The source was not found, but some 
    or all event logs could not be searched.  Inaccessible logs: Security.
    

    I'm not sure if this is configuration issue, or if my batch script is wrong. But since I can't create a directory in the C:\Windows\System32\ folder that the runas command prompt is started in, I suspect I'm not getting the permissions I was expecting. Is there a way I can check what user/permissions level I'm running at in the command prompt?

    Note: I want to make this script as generic as possible. In the environment I'm working in everyone's administrator account is guaranteed to be 'UserAdmin'. The path to the solution will also be the same. This script will be added to SVN, so something like adding my particular password for the UserAdmin account would not be acceptable.

    • HackToHell
      HackToHell over 11 years
      runas requires password to be entered, what do you then, do you manually enter the password ?
    • Jeff B
      Jeff B over 11 years
      Also, runas appears to not work quite as expected, as described here: superuser.com/a/485976/101146
  • Jeff B
    Jeff B over 11 years
    Hmmm, this StackOverflow answer suggests to me that using sc may not be the correct way to install a Windows Service developed in .NET stackoverflow.com/a/4694224/945456
  • d4v3y0rk
    d4v3y0rk over 11 years
    It may not be, I guess it depends on your needs. It looks like from that answer that installutil requires a restart to remove the service, and sc does not. Also I can speak from experience that sc delete <said service> does not remove the registry entries and I would be willing to bet that installutil does. (which is why it requires a restart, to reload the registry)
  • user 99572 is fine
    user 99572 is fine almost 10 years
    Could you please add a description to the code what it does does? You also seem to have a typo (installNETservie.bat).
  • Chenthurij
    Chenthurij almost 10 years
    without using the command prompt , use this bat file and u can install the windows service locally.