@ECHO off
REM Assigns a service to a user account

SET exec=%~dp0

SET account=
SET service=

if [%1]==[] goto usage 1

:CHECKFORSWITCHES
IF '%1'=='-h' goto usage 0
IF '%1'=='-a' goto setaccount
IF '%1'=='-s' goto setservice
IF '%1'=='-p' goto setpassword
goto execute

:setaccount
set account=%2
SHIFT
SHIFT
goto CHECKFORSWITCHES

:setpassword
set password=%2
SHIFT
SHIFT
goto CHECKFORSWITCHES

:setservice
SET service=%2
SHIFT
SHIFT
goto CHECKFORSWITCHES

:execute

IF "%account%" == "" (
    goto usage 1
)

IF "%password%" == "" (
    goto usage 1
)

IF "%service%" == "" (
    goto usage 1
)

sc config %service% obj= %USERDOMAIN%\%account% password= %password%

goto eof

:usage
echo usage: %0 options
echo.
echo Assigns a service to a user account
echo.
echo OPTIONS:
echo -h             Show this message
echo -a account     Account name to use
echo -p password    Password for the service account.
echo -s service     A windows service id. The user will be configured as the "Log on as" account.
exit /b %1

:eof

