install windows service through batch file in a particular directory and set login credentials - batch-file

I have a windows service that I need to install in a userdefined directory instead of C: drive and set login credentials for it.
I am using the below code to install the service but it installs the service in C drive instead of E drive.
E:
cd \MyService
msiexec /i MyServiceInstall.msi /L E:\MyService\MyServiceInstallLog.txt /qn
sc config MyServiceInstall obj= uid password= pwd start= auto
rem net start MyService
I want to install the service at the location where the install file, MyService.msi, is present.
How do I go around this?

You don't "get around" anything. You create a proper MSI that uses the ServiceInstall table to create the service. The ServiceInstall table has a UserName and Password column which is of type Formatted which means it can use properties like [USERNAME] and [PASSWORD]. Then you can simply say:
msiexec /i MyServiceInstall.msi INSTALLDIR=C:\INSTALLHERE USERNAME=bob PASSWORD=dontyouwhich
The only tricky part is that MSI doesn't grant the user account the SeLogonAsService right so a custom action will be needed to grant this prior to starting the service.

Related

Running remote batch in elevated command

I have a windows service install batch file in a remote computer (Install.cmd).
When I run it as administrator, it works just fine.
But when I run a script (RemoteInstall.cmd) that run the remote install script, it complete all the steps beside start running.
From my computer -- RemoteInstall.cmd script:
START /WAIT clusrun /scheduler:RemotePc /nodes:RemotePc /user:Administrator /password:MyPass \\RemotePc\D$\Service\Install.cmd
On server -- Install.cmd script:
%WinDir%\Microsoft.NET\Framework\v4.0.30319\installutil MyService.exe
sc config "MyService" start= auto
sc config "MyService" obj= ".\Administrator" password= "MyPass"
sc failure "MyService" actions= restart/10000/restart/10000/restart/10000 reset= 86400
net start MyService
The last command returns errorlevel=2 when I run it from remote, while all the rest return errorlevel=0 if I run it locally or from remote.
Same error occurs when I use sc instead of net.
How can I make it work?

installing windows service from command line "The specified service has been marked for deletion"

I am trying to create a batch file to automate uninstalling, building and reinstalling a windows service.
After uninstalling when I try to reinstall I get the error: The specified service has been marked for deletion.
here is the batch script:
#ECHO off
set filePath=<myfilepath>
set serviceName=<myservicename>
sc query %serviceName% > nul
if %ERRORLEVEL% EQU 0 net stop %serviceName% & sc delete %serviceName%
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe C:\Users\Christopher\Desktop\TFS\MOC\LaneUpdateService\LaneUpdateService.sln /property:Configuration=Debug
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe %filePath%
sc start %serviceName%
I do not have task manager, services or event viewer open, there are no other users logged in. I have tried deleting the reg key for my service, with no luck. If I run my script again after it failing it will install correctly. Is there a way to manually make sure the service is fully deleted?
The problem was that after stopping my service there was still a process running. I altered my onstop method to make sure all processes were stopped correctly. now the batch file works correctly. As #Harry Johnston suggested I will be adding a while loop to the batch file.

Jenkins doesn't use the same credentials that the logged in user has

I'm running Jenkins on a Windows Server and want to have access to a mapped network drive. I run the Jenkins using the same user credentials that I'm logged in with. Here's the problem: I don't have the same access to the files on the mapped drive from Jenkins and my local command prompt. Here's what I get when I one to change the directory in Window Server CMD:
C:\Users\DevBuildUser>cd /d r:
R:\>
and this is what happens in Jenkins' Execute Windows batch command running on the same machine:
cd /d r:
The system cannot find the drive specified.
Jenkins will run that batch file as the SYSTEM user. Go to the TARGET machine, and add the name of the machine from which you're running the Jenkins instance, with full write permissions.
...and use the full path, e.g., \10.1.2.3\myshare\mynewfiles\
Alternatively:
whoami
net use R: \\server\share\dir
R:
mkdir zipme

Creating Batch file to run exe. and output to log file via GPO startup/shutdown

I am having some trouble creating a batch file, I need it to remove a .msi file first and then run a .exe file this is what I have so far.
echo on
title remove mimecast 3.5.6
if exist c:\windows\mimecastrem.tag goto alldone
msiexec.exe /x {86C4653D-E9B2-4421-8578-D2B953FFAEDA} /qn /L "C:\windows\mimecast.log"
start \\fcmsvr2\MimecastMSO\deploymimecast.exe /zap
echo. >c:\windows\mimecastrem.tag
:alldone
rem exit
If I run this in DOS logged on the pc it runs ok, I have tried to add this to a GPO startup and shutdown and the batch runs but only removes the .msi file and won't start the .exe, is there a way to add logging to this so I can see why it isn't running?
Thanks
David
Create a Wrapper Script to Capture the Output
When I try to run batch commands from a GPO, I usually have two batch files:
One batch file does all the work.
One to call the other and redirect all the output.
This way, if the important batch file has syntax problems (usually due to the contents of variables or other conditions on the machine), I still see something in the log.
batch_worker.cmd
#ECHO OFF
SETLOCAL
#ECHO Proceeding with important tasks...
REM do other stuff (delete, install, etc.)
batch_wrapper.cmd
CALL %~dp0batch_worker.cmd > C:\Windows\TEMP\batch_worker.log 2>&1
Self-Wrapping
Another option that I occasionally employ is to have the script invoke itself, redirecting the output.
batch_worker.cmd
#ECHO OFF
SETLOCAL
IF NOT "%_LOGGING%"=="1" (
SET _LOGGING=1
CALL "%0" %* > C:\Windows\TEMP\batch_worker.log 2>&1
EXIT /B %ERRORLEVEL%
)
#ECHO Proceeding with important tasks...
REM do other stuff (delete, install, etc.)
Network Files from GPO
Any startup scripts that run on a Windows machine will have the network identity of the computer account (COMPUTERNAME$). This account doesn't usually have access to most network resources. The one exception is the GPO. All computer accounts have access to the GPO (which is stored as files).
If your script needs other files:
Store them in a world-readable network location (or grant any domain member RX access).
Store them with the GPO.
When you create/add a startup script, the GPMC snap-in gives you access to the network location (under SYSVOL) where the script should reside. Use that UNC path to save any other files the script needs access to.

Run as administrator from the command line

To help my computer boot faster, I created a simple batch file that will open the programs I want, rather than do it all on startup, when I sometimes don't want them to.
#ECHO OFF
cd "C:\Users\Aaron\Documents\Documents"
start SSS.lnk
cd "C:\Program Files (x86)\puush"
start puush.exe
cd "C:\Users\Aaron\AppData\Roaming\Google\Google Talk\"
start googletalk.exe
cd "C:\Users\Aaron\AppData\Local\Facebook\Messenger\2.1.4651.0\"
start FacebookMessenger.exe
cd "C:\Program Files\Synergy\"
start synergy.exe
cd "C:\Program Files (x86)\Skype\Phone\"
start Skype.exe
cd "C:\Program Files (x86)\Miranda IM\"
start miranda32.exe
However,
cd "C:\Users\Aaron\Documents\Documents"
start SSS.lnk
is a service that's set to Manual, and I start that myself, and it requires to be run as administrator to start. Is there anything to add in front of that to run just that as administrator?
You might wish to have a look at Runas.
Short answer: You can use runas.exe:
C:\>runas /user:<localmachinename>\administrator cmd
or
runas.exe /user:administrator "full qualified path to your exe"
For the last cmd, you can add /savecred to save the administrator's password (not that I'm saying this is a good idea).
Workaround: Create a shortcut to your script. Go to properties, shortcut, advanced. Check "run as administrator".
There you go; every time you access via shortcut it will open as administrator.
There some misunderstoods:
How to get localmachinename
There are many ways, some of them are:
a. c:\>hostname or
b. c:\>echo %computername%
You can't use runas [...] command if you don't have set password to your Windows.
1327: Logon failure: user account restriction. Possible reasons are blank passwords not allowed, logon hour restrictions, or a policy restriction has been enforced.

Resources