Windows Service Write output to file - windows

I have the following batch script:
echo %time% >> C:\file.txt
C:\MyProgram\program.exe /arg1 >> C:\file.txt
echo ------- >> C:\file.txt
I have created a service with
sc create ProgramRun type= own start= auto binPath= "cmd /c cd /d c:\MyProgram\ && start script.bat"
After a reboot, in file.txt I have printed the correct time, but without my program ouput. In that program, I have some simple printf
What can I do to write the output to a file without editing the program?

As a Windows Service, your program is being run in the context of the Local System account, which may not have the rights necessary to launch and run your program properly. Try editing the service and setting the Log On credentials to a Windows account that you know can execute your program properly.
Also, while the Windows Service Control Manager (SCM) will happily start your batch file, the outcome reported will always be failure, specifically "Error 1053: The service did not respond to the start or control request in a timely fashion". This is because your batch file can not communicate with the SCM! Have a look at Microsoft's free (and basic) Srvany "service wrapper" to avoid this problem.

Related

Run PowerShell script on remote server as different user without any pre-requisites

I need to trigger the execution of a powershell script on remote server under different user. This script uses 'gpg.exe' so the execution has to be on the remote server with a path pointing to 'gpg.exe'.
I created a bat file on remote server with following commands:
REM RUNTEST.BAT
time /T
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
echo started
start /wait powershell.exe "Start-Transcript -Path D:\logs\testlog.txt;import-module
'\\ServerName\FolderName\Script\AutomationScript.ps1';Stop-Transcript"
time /T
echo done
exit /b
Then invoked this bat file from PowerShell on another machine:
runas /user:DOMAIN\serviceuser "\\ServerName\FolderName\Script\Execute.bat"
These steps do execute the PowerShell script successfully and the output is also as expected. However, this requires 'gpg.exe' to be available on the path mentioned in the PowerShell script on the machine which will invoke the execution. Is there a way the script can look for pre-requisites (like gpg.exe, folder structure etc) only on the remote server, not the machine which invoked the bat file?
Not sure if OS makes any difference, the remote server has Windows Server 2019 and calling machine has Win 10. The remote server has the required folder structure, gpg.exe, bat file to execute the PowerShell script successfully. The calling machine will not have 'gpg.exe' & the folder structure.

Running mstsc.exe as batch job under task scheduler

I am trying to automate logging in to a bunch of RDP clients on a 2008 R2 Server, the batch below works fine when running double clicking the batch file from the desktop, however nothing happens when setting the .bat file to run as a task from Task Scheduler
#ECHO off
ECHO This .bat is used to automatically RDP in to the specified servers
ECHO which are contained within this file. Ensure you have logged in to
ECHO these servers beforehand and checked 'save credentials' otherwise
ECHO this batch file won't work as intended.
ECHO.
ECHO Beginning login...
ECHO.
ECHO Beginning login - Server ADMIN...
start mstsc.exe /v:00.00.00.00 /admin
PAUSE
The scheduled task is set to run under the Administrator account (only one available) and the (Start in) optional field has also been set "C:\Users\Administrator\Desktop\".
Turns out mstsc.exe only works if the user account is logged in... so under Security options in the General tab make sure 'Run only when user is logged in' is checked.

Run a bat script on a distant computer from my computer (Command Prompt)

I work on my computer A.
I would like to run a batch script.bat on my computer B (C:\Documents\script.bat). The password of the admin session of B is PASSWORD.
This is my command to execute my batch from computer B :
start C:\Documents\script.bat
Now, I would like to run it with this argument : 3.2.16
I've tried these 3 commands but it says "incorrect caracter"
start C:\Documents\script.bat "3.2.16"
start C:\Documents\script.bat '3.2.16'
start C:\Documents\script.bat 3.2.16
Also, I would like to run it from my computer A.
Can you help me please ? Thank you in advance.
If you want to run a process remotely, you'll either need to use Sysinternals psexec, or wmic. The difference is that psexec diverts its output to the computer it's invoked from; whereas wmic displays a window on the remote PC.
If you're curious, the wmic command syntax is as follows:
wmic /node:remotePC /user:remotePC\user /password:password process call create 'cmd /c "c:\path\to\script.bat" "arg1" "arg2"'
If you need to view the output of script.bat, redirect the output remotely to a text file, then read the text file.
wmic /node:remotePC /user:remotePC\user /password:password process call create 'cmd /c blah ^>c:\output.txt'
net use z: \\remotePC\c$ /user:remotePC\user password
type z:\output.txt
del z:\output.txt
net use z: /delete
...for example. But it's probably easier just to download and use psexec.

Windows: Batch File: Only Run as Administrator

I have a batch file to start an application as a Windows service. It is called start.bat
#ECHO off
START c:\Ruby193\bin\ruby c:\Ruby193\bin\thin start -R c:\coolapp\config.ru -p 4321 -a localhost -e production
My challenge is that this program only runs properly if it is "Run as Administrator" with admin privileges. So, I would like to add a line to check if this script is actually run with administrative privileges, and only execute if it is being run as administrator.
How can I do that from within the script?
Something like this might be what you need:
set isadmin=0
whoami /all | findstr /c:" S-1-16-12288 ">nul && set isadmin=1
That should result in the %isadmin% variable being either 1 or 0 depending on whether the shell was run as administrator or not.
This assumes the existance of the whoami utility which won't necessarily be available on older versions of Windows - I believe it was included from Windows Vista onwards though.
Two options:
Provoke elevation from a WSH script, like documented in the blog post Scripting Elevation on Vista.
Use an external executable that provokes the UAC prompt, such as Elevate32.exe/Elevate64.exe.
For your scenario, #2 may be preferable because you can detect whether the elevation prompt was canceled (exit code 1223) and you can also wait for the launched executable to finish before continuing (-w parameter).
Bill
It would probably be easier to convert the script to VBScript, then you can more easily check for Admin privileges and even elevate the script to Admin.
See here for how to do the check in VBScript: VBScript: Check if the script has administrative permissions

startnet.cmd issues when silently installing Windows 7

I am trying to start windows 7 unattended using the startnet.cmd file. The problem is that when the start setup.exe command is triggered, the setup runs and closes, but cannot reboot because the startnet.cmd is still open. If I put "exit", it starts the setup and exits immediately. Is there a way to check uf the setup ran successfully and then perform an exit?
wpeinit
net use u: \\192.168.2.10\windows
u:
start setup.exe /unattend:\\192.168.2.10\windows\unattend.xml /noreboot
REM ping -n 30 127.0.0.1
REM exit
I think your commands are correct but you explicitly state that setup.exe shall not initiate a reboot after the first phase of the setup procedure has completed.
The black cmd.exe Window will have to remain open during the setup procedure. If it's terminated then Windows PE performs an immediate reboot. Hence your setup.exe process will be terminated. I think your comment using ping to delay the exit very well shows that you have tried to delay terminating the shell process. But as you can't know how long the setup process will take you cannot fill any good value in the delay algorithm.
Although I think you don't need to. You are launching setup.exe using start which detaches the process and returns to the shell immediately. In fact since setup.exe is the last command you run you could also live with the fact that setup.exe is not detached. Either by not using start or by using start /wait
wpeinit
net use u: \\192.168.2.10\windows
u:
setup.exe /unattend:\\192.168.2.10\windows\unattend.xml /noreboot
exit
or
wpeinit
net use u: \\192.168.2.10\windows
u:
start /wait setup.exe /unattend:\\192.168.2.10\windows\unattend.xml /noreboot
exit
Although I think the entire purpose of this would be to return to the shell and perform the exit (which initiates a reboot) command after setup.exe terminates.
As there are no more commands to be executed I suggest letting setup.exe do the reboot itself by removing the /noreboot flag:
wpeinit
net use u: \\192.168.2.10\windows
u:
start setup.exe /unattend:\\192.168.2.10\windows\unattend.xml
This should keep the shell process (cmd.exe) open while setup is running. At the end of the setup process it will initiate a reboot (initiated by setup.exe) and therefore also te

Resources