How to execute a batch file on remote using psexec? - windows

I have a batch script which is unzipping a folder. It looks as follows:
"C:\Program Files\7-Zip\7z.exe" e "C:\target\bin.zip"
the above batch script unzip the folder bin.zip to same location as that of batch file. Now, this batch script is placed on the remote machine and i have to start it there remotely . I used psexec and did following from my local machine command prompt:
psexec -s -i \\ip_add -u user -p pass C:\target\sample.bat
So, now it does not unzip any file on the remote machine. I am not getting what am i doing wrong. The batch script is perfectly working fine and i tried to verify psexec is working correctly or not by launching a calc on remote , it worked pretty well. so, please suggest what am i lacking.

You could try to launch cmd.exe from psexec to get a remote shell and then attempt to run the batch file from the command prompt

Related

how to run agent's command line or powershell using azure pipleine?

is there any way that i can invoke the command line of Azure Deployment agent? The tasks currently available don't invoke the agents' command line, but in fact install their own.
1.Make sure your .bat file itself is free of errors.
2.Use the following command in powershell in your pipeline
Start-Process "<the path of cmd.exe on our host agent>" -ArgumentList "/k <the target .bat file on your host agent>"
The function of this command line is to open the cmd on your host agent then run the .bat file. You can refer to this link to learn how does it work
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-6

Using psexec on a bat file that contains psservice

I have a bat file that tries to stop a service on a remote machine, the file contains the following
psservice \\remoteServerName -u domainName\userName -p password stop serviceName
where userName is a name of a user with Admin privileges on the remote machine.
If I run the last line using cmd then the requested service does stop.
I, however, run the bat file using psexec, since it contains more operation than just stopping the service. This is where my problem occurs:
If I run
psexec -u domainName\userName -p passsword batFilePath
the cmd window seems to get stuck.
But if I run
psexec batFilePath
then the psservice executes correctly.
My problem is that I need the user "domainName\userName" to be able to run the other commands in the bat file.
What can I do ? Why does the cmd got stuck when I gave psexec the parameters of the userName and password ?

Trying to delete files via a scheduled task and VBScript

I asked a previous question about downloading file unattended on a server, using VBScript and Windows Task Scheduler. (See
Scripting SFTP unattended download )
That works great but now I also need to delete the files from the server I am calling:
Set sessionses = WScript.CreateObject("WScript.Shell")
sessionses.Run "C:\TCS\SFTP\delThem.bat", , True
PSFTP calling a batch file that runs this command:
psftp user#host:22 -batch -b script.txt -pw pa$$word
script.txt is as follows:
cd FromCeridian
del *.GEN
If I run this command from the command line, or double click the batch file containing the command, it works "interactively", but when running unattended and scheduled, it doesn't do this part. The server is SFTP, so I can't just use win ftp commands to do it.
Any ideas?
Check the security options of your task to verify the user identity has correct access privileges.

PsExec hangs when executing batch file in bamboo continous integration server

I have a batch file on my bamboo server, that starts another batch file on an remote computer trough PsExec. If I double click the batch file on the bamboo sever everything is working fine, the batch file on the remote computer starts and PsExec exites with error code 0.
But if I start bamboo then PsExec hangs itself and the batch file on the remote computer don't start.
C:\PsTools>PsExec.exe \\<remote server> -u <user> -p <password> -s -i "C:\batchfile.bat"
PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
This is my output and where PsExec hangs.
I don't understand why it hangs only when bamboo starts the batch.
Hope someone can help me
[ Program output lost when passed through PsExec ]
there's a bug in psexec and it does redirects correctly when is called by everything different by cmd. You have few more options to start a process on remote machine:
1. http://feldkir.ch/xcmd.htm - it cannot be run on localhost
2. WMIC - also cannot be used on localhost , when host and and user/pass are given
2. SCHTASKS
This is an old question but maybe somebody has the same problem.
The first time you run psexec an eula dialog is shown. In order to avoid bamboo hanging with this dialog you may try the argument \accepteula. For instance:
PsExec.exe \\<remote server> -u <user> -p <password> -s -i "C:\batchfile.bat" -accepteula

ssh to run shell script on remote machine and then copy the output to local machine

I am using plink to execute the shell script on the remote MachineB. And shell script is there on the MachineA(Windows Box).
C:\PLINK>plink uname#MachineB -m test.sh
Using keyboard-interactive authentication.
Password:
Using keyboard-interactive authentication.
Your Kerberos password will expire in 73 days.
And that shell script generates the output in a text file(aa.txt) on MachineB and that gets stored in /export/home/uname/aa.txt
So my question is- Is there any way that I can copy the aa.txt file from MachineB to MachineA as soon the script has completed all its task using the ssh. Or we need to put all these things in Windows Batch file?
So Problem Statement is like this-
Execute the shell script on MachineB from MachineA.
Then wait for the shell script to complete its task, in my case it will write the output to a text file.
And after the shell script has completed all its task means it finished writing everything to a txt file, then copy that txt file to MachineA from MachineB
Any suggestions will be appreciated on how I can achieve the above scenario?
Update:-
So Suppose if this is the content in test.sh shell script file and also after adding pscp at the end of script, then it should be like this?
#!/bin/bash
export HIVE_OPTS="$HIVE_OPTS -hiveconf mapred.job.queue.name=hdmi-technology"
hive -S -e 'SELECT count(*) from testingtable2' > aa.txt
pscp uname#MachineB:/export/home/uname/aa.txt c:\documents\foo.txt
So I am executing a hive query in the above script and whose output is getting stored in aa.txt file and as soon as the query is completed and output is stored in aa.txt file, it will go to fourth line of pscp which will transfer aa.txt file to my local windows machine inside documents folder. Am I right? This will be the whole process?
And if the above process is right as far as I understood, then I can simply go to windows cmd prompt, and do like below and it will do the exact same process. Right?
C:\PLINK>plink uname#MachineB -m test.sh
Using keyboard-interactive authentication.
Password:
Using keyboard-interactive authentication.
Your Kerberos password will expire in 73 days.
Updated Again:-
So I need to create a bat file, and suppose this is the below test.bat file, so content should be like this in that test.bat file-
plink uname#MachineB -m test.sh
pscp uname#MachineB:/export/home/uname/aa.txt c:\documents\foo.txt
You can use scp to download the file after execution. If you setup winsshd on your windows machine and append a copy command in the test.sh file:
scp /export/home/uname/aa.txt user#windowsmachine.com:/homedir
The file will be transferred after completion.
Similarly, you can use a windows scp client like pscp on your windows machine, you can pull the file from the linux machine:
pscp uname#MachineB:/export/home/uname/aa.txt c:\documents\foo.txt
So on windows machine, have a batch script:
plink uname#MachineB -m test.sh
pscp uname#MachineB:/export/home/uname/aa.txt c:\documents\foo.txt
Just to add on to what has already been said, I am running something very similar and use the following format (from a batch script):
plink -ssh user# -pw password -m yourScript.sh
pscp -sftp -pw password user#IP-Address:/path/to/remote/file C:\path\to\local\save\directory
That second command is on one line, but it might not render like that on here. Of course, replace IP-Address, user, and password with the appropriate values for your login :-)
Hope that helps!
EDIT - Sorry, just realized this EXACT answer was already given. Kudos!

Resources