Running remote batch in elevated command - batch-file

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?

Related

Restarting wrapper.exe by Tanuki software as a Windows Service using command line

Would anyone know how can I restart this service without manually triggering it thru services.msc? How can I put it in a script where this service be automatically restarted as a service? thanks in advance!
"c:\Program Files (x86)\Tanuki\bin\wrapper.exe" -s "c:\Program Files (x86)\Tanuki\conf\MTG.conf"
I'm getting this error message:
C:\Users\Ning>"c:\Program Files (x86)\Tanuki\bin\wrapper.exe" -s "c:\Program Files (x86)\Tanuki\conf\MTG.conf"
Attempting to start MTG as an NT service.
Calling StartServiceCtrlDispatcher...please wait.
StartServiceControlDispatcher failed!
The -s and --service commands should only be called by the Windows
ServiceManager to control the Wrapper as a service, and is not
designed to be run manually by the user.
For help, type
c:\Program Files (x86)\Tanuki\bin\wrapper -?}
Try this (run the script as administrator):
Starting a service:
RunWait, %comspec% /c "net start service_name",, Hide
Stopping a service:
RunWait, %comspec% /c "net stop service_name",, Hide
Replace "service_name" with the service name you want restart.
Make sure that you are using the correct service name,
shown in the properties for that service (not the Display name).

winrs reg query works but reg query hostname does not

I am trying to verify that the usbstor driver is set to disabled on a remote machine in my domain in a vbscript but I'm having issues. I've turned to troubleshooting from the command line.
Using plain old reg query fails.
reg query \\hostname\hklm\system\currentcontrolset\services\usbstor /v start
ERROR: The network path was not found.
But if I run essentially the same command with winrs, it works fine.
winrs -r:hostname reg query hklm\system\currentcontrolset\services\usbstor /v start
HKEY_LOCAL_MACHINE\system\currentcontrolset\services\usbstor
start REG_DWORD 0x4
Both commands work fine on other computers. What gives with this one?
Regardless of the computer I'm running the commands against, I open the firewall with this command:
winrs -r:hostname netsh advfirewall firewall set rule
group="Windows Management Instrumentation (WMI)" new enable=yes
The service RemoteRegistry is not running (or not accessible) on the remote host. You should be able to start it like this:
sc \\hostname config RemoteRegistry start= auto
sc \\hostname start RemoteRegistry
If the service is running but still not accessible, check that the Windows Firewall allows RPC.

SQL Server agent for scheduling SFTP using WinSCP under SSIS

I have a batch script which generates a WinSCP upload script to upload a file to an SFTP location. Now when I run the batch file via command prompt - it runs successfully and loads it. I called the same thru SSIS Execute process task - it runs successfully and loads it. Now when I put the same on SQL Agent - I tried the following two options:
Using Operating System (CmdExec) - cmd.exe /c "\.bat"
Added the SSIS package to SSISDB and added it as a job step.
With both the above options the job showed a successful run. However the file is not uploaded! Any ideas on what is happening?
Here's my batch script:
echo off
SET winscp=C:\"Program Files (x86)"\WinSCP\WinSCP.com
SET stagingDirectory=\\<staging path>\
SET scriptPath=\\<ScriptPath>\UploadScript.txt
SET ftpHost=xx.xx.xx.xx
SET ftpUser=user
SET ftpPass=password
SET fileName=Test.xlsx
SET ftpFlags=
#REM ftpFlags: -explicit
echo deleting uploadScript if it already exists
IF EXIST %scriptPath% del /F %scriptPath%
IF EXIST %scriptPath% exit 1
echo Generating WINSCP Upload Script
>>%scriptPath% echo option batch abort
>>%scriptPath% echo option confirm off
>>%scriptPath% echo open sftp://%ftpUser%:%ftpPass%#%ftpHost% %ftpFlags%
>>%scriptPath% echo option transfer binary
>>%scriptPath% echo put %stagingDirectory%%fileName% /
>>%scriptPath% echo close
>>%scriptPath% echo exit
echo Launching WINSCP upload
start /wait %winscp% /console /script=%scriptPath%
As you start the WinSCP via the start (why?), the exit code is not propagated to the SSIS. So, you never learn, if the script fails. And it most probably fails.
You also should enable logging, so that you can see what's wrong.
You should use this code to propagate the WinSCP exit code to SSIS and to enable logging:
%winscp% /log=\\<ScriptPath>\UploadScript.log /script=%scriptPath%
exit /b %ERRORLEVEL%
(Note that the winscp.com does not have the /console parameter)
Anyway, one clear problem is that you do not specify an expected SSH host key in the script. When you run the script manually, you probably have the key cached in the registry of your Windows account. But under the SSIS a different account is used, and its host key cache is likely empty. You should add the -hostkey switch to the open command in the script to make the script independent on the cache. See Where do I get SSH host key fingerprint to authorize the server?
When testing the script, add the /ini=nul parameter to isolate the script from your configuration.
For this and other hints, when debugging WinSCP running under SSIS, see My script works fine when executed manually, but fails or hangs when run by Windows Scheduler, SSIS or other automation service. What am I doing wrong?
And finally, see WinSCP SFTP Task for SSIS.
Your variable seems set incorrectly. To manage with a space in the path and into the variable you have to put in quotes the whole path or the whole variable.
i.e.
set "winscp=C:\Program Files (x86)\WinSCP\WinSCP.com"
echo start "%winscp%"
:: output: start "C:\Program Files (x86)\WinSCP\WinSCP.com"
or
set winscp="C:\Program Files (x86)\WinSCP\WinSCP.com"
echo start %winscp%
:: output: start "C:\Program Files (x86)\WinSCP\WinSCP.com"
Another point, you have to check this file: UploadScript.txt because your script adds new lines rather than remake the file.
change this line to >%scriptPath% echo option batch abort instead of >>%...
Ah, I did not pay attention to the IF EXIST.

Run batch script on a remote server (windows 7) from jenkins(windows server 2008)

I created a CI(jenkins) environment on windows server 2008. Now I want to execute windows batch command to remote to another windows(win7) and run a bat file on win7.
The batch command is:
PsExec.exe -accepteula \machinename -u username -p password cmd /c c:\test\test.bat
Jenkins console output is:
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
The remote procedure call failed and did not execute.
Connecting to ...
Starting PSEXESVC service on ...
Could not start PSEXESVC service on :
Connecting to ...
Starting PSEXESVC service on ...
The batch command can be executed successfully by CMD, why failed on jenkins command?
I checked Remote Procedure Call(RPC) and Remote Procedure Call(RPC) Locator have been started in services.msc list.
I did a large of research by google, and spent the whole day to fix the issue. However I still haven't figured out how to fix the issue.
Any help is appreciated and will respond quickly to any suggestions, thanks!

PsExec and invalid handles

I am trying to use a windows batch script that uses PsExec to execute commands on a remote machine. Periodically it has "invalid handle" and the script then fails.
The script has not altered or indeed either machine.
Does anybody know why this happens as sometimes the scripts runs without a hitch.
Alternatively does anybody know how to run a script on a machine as the local user for that machine with a more reliable technology.
PS Sometimes the first PsExec works and the others fail.
EDIT
The script is just on line (apart from setting the appropriate variables)
PsExec %HOSTNAME% -I -u %USERNAME% -p %PASSWORD% CMD /C RMDIR /S /Q e:\SomeDir
This sometimes works but sometimes fails with "invalid handle"
You need to debug the situation.
You have a script, then something (what is Jenkins?) launch it on a remote PC, sometime it works, sometime it fail.
Is it deterministic?
When it fail does it always fail?
How does it fail?
You need to acquire better knowledge of how/when the script fail.
Here is what I would do to gather better understanding of these fails.
Can you run the script multiple time?
From the comments it seem that you run the script every hours, can you run it 3/4/5 time in a row, for each hours?
This will help you to determine how it fail: if you run it 5 time, does it works every time? it it fail, does it fail 5 times in a row?
Can you try to use different script?
You can create some more similar, but simpler, scripts.
So you can try your script with the RMDIR, then another script with a simple DIR command (just to se if the script launching/connection mechanism works) then another script with a simple ECHO command (so it doesent need to access any files/folder)
Run debug scripts on the local PC
Then, you can simultaneously run other scripts that run on the LOCAL PC (not the remote one where you need to execute the RMDIR) that try to access the remote PC, with a PING, or by copying a file from/to a network share...
Sniff the network
You can even set up a Wireshark instance that log all the packet sent between the 2 PC, this can be helpful to analyse/exclude networking issue.
You clearly need to track/log everything.
With this kind of information maybe you/we can have a better understanding of where the issue is.
=====================================
UPDATE 1 - Record some log
=====================================
Maybe you can try to use the following modified scripts to have some log files.
These script will create 2 log files, one on the remote PC (containing the message of the remotely executed command) and one on the local PC (containing any message from PsExec)
(you'll need to tweak the path where the log file are saved)
psexec %HOSTNAME% -I -u %USERNAME% -p %PASSWORD% CMD /C "RMDIR /S /Q e:\SomeDir >>c:\RemoteComputer.log 2>&1" >>c:\LocalComputer.log 2>&1
or the following one without the /I
Are you sure you need the /I parameters for CMD? On my Pc it doesn't works if I use the /I parameters...
psexec %HOSTNAME% -u %USERNAME% -p %PASSWORD% CMD /C "RMDIR /S /Q e:\SomeDir >>c:\RemoteComputer.log 2>&1" >>c:\LocalComputer.log 2>&1
After some testing on my PCs, I've seen that PsExec install a service on the remote PC to run the command remotely. (It's called PsExecSvc.exe, installed in c:\windows\ on the WinXP PC I'm using for this test)
The remote installation/uninstallation of this temporary service for the command execution can surely be one of the possible "failure point" that generate the error.
If this is the case, then you should be able to track this down by looking at the LocalComputer.log, that will contain the message/error from PsExec.
As stated in my previous advice, I would also try to schedule simpler script like
psexec %HOSTNAME% -u %USERNAME% -p %PASSWORD% CMD /C "dir c:\ >>c:\RemoteComputerDir.log 2>&1" >>c:\LocalComputerDir.log 2>&1
and
psexec %HOSTNAME% -u %USERNAME% -p %PASSWORD% CMD /C "echo SuperEchoTest >>c:\RemoteComputerEcho.log 2>&1" >>c:\LocalComputerEcho.log 2>&1
===================================
UPDATE 2 - Try to use WMI
===================================
You can try to run the remote command by using WMI
wmic /node:%HOSTNAME% /user:%USERNAME% /password:%PASSWORD% process call create "CMD /C RMDIR /S /Q e:\SomeDir"
When you use WMI you need to be sure that windows firewall is not blocking your command. (when I tried to run a remote command with WMIC the windows firewall notification popped up on my Win 7 PC)
(I've the instruction to use WMIC here)
Yes, there is a more reliable technology for executing commands on a remote machine and is called powershell. For example, you can run :
test-connection -computername server01, server02, server12
pings from local computer to several remote computers.
Another very useful command is:
invoke-command -filepath c:\scripts\test.ps1 -computerName Server01
runs the Test.ps1 script on the Server01 computer.
A tutorial gives several examples on how to Run PowerShell Commands on Remote Computers.
A different technology can be found mimicking the Linux world, and using ssh. It's very common with clusters and I have personally used it with Windows Server 2008 R2, so I don't expect any difference on windows 7.
This task is commonly performed with ssh and password-less public key authentication. With it, the only needed information is the IP of the remote server and the public key of the client, stored on the server: only the client with the corresponding private key can connect to it (the keys must be created with ssh-keygen, on the client. The public key is copied to the server)
The server must have the TCP port 22 accessible from outside, in case there are firewalls, NATs,...
In my case I used the ssh server included in Windows SUA, but I suggest you forget them (they are deprecated, and quite cumbersome actually) and give a try to the OpenSSH cygwin server, sshd - even if not officially Microsoft, there is a large community supporting it at least - and occasionally I have used it reliably.
The client ssh command is included in SUA, in cygwin, or you can use putty if you want a lightweight solution on the client (not that cygwin is heavy - just the burden of having a sort of linux emulation that's not needed)
Giving a search for example I have found this post, explaining well the needed steps.

Resources