@echo off
@setlocal enableextensions
@cd /d "%~dp0"
setlocal disabledelayedexpansion
goto check_Permissions

:check_Permissions
    net session >nul 2>&1
    if %errorLevel% == 0 (
			echo This user must be either a domain account with local admin priviledges and should be able to logon as a service.
			set /p "username=Enter the username with domain: "
			echo Enter password:
			
			for /f "delims=" %%a in ('powershell -command "$password = Read-Host -AsSecureString; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password); [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)"') do set "password=%%a"
			echo Stopping ConnSolace...
			sc query ConnSolace| find "STATE" | find "RUNNING" > nul
			if %errorlevel% equ 0 (
				sc stop ConnSolace
			)
			setlocal enabledelayedexpansion
			sc config ConnSolace obj= "!username!" password= "!password!"
			
			if %errorlevel% neq 0 (
				echo Error: Failed to set the new username/password for the service.
				pause
			)

			sc start ConnSolace

			set "serviceStatus="
			for /f "tokens=3 delims=: " %%A in ('sc query ConnSolace ^| find "STATE"') do set "serviceStatus=%%A"

			echo Service modified, restarted, and current status is: "!serviceStatus!"
    ) else (
			echo ERROR: You need administrator privileges in order to start ConnSolace service!
    )
pause