Batch file doesn't continue until after I exit psexec remote connection - windows

I'm trying to make a batch file to run a script on remote server. If I enter the commands below in the cmd prompt seperately it works fine but it seems to hang after I enter the psexec commands. It only continues the rest of the batch file when I exit the psexec remote connection. The rest of the commands are run on my local computer(which I don't want). Anyone have any ideas or suggestions?
psexec \\ServName -u DOMAIN\UserName -p password cmd.exe
pause
cd c:\Users\UserName
pause
cscript \\NetworkName\filepath\blankTest.vbs

You can bundle the commands into one batch file and then execute that using one line:
psexec \\ServName -u DOMAIN\UserName -p password cmd.exe -c mybatchfile.bat
That will cause the file to be copied to the remote machine first.
Alternatively if you have problems with that, copy the file first, then execute it; note the different parameter, /c vs -c:
copy mybatchfile.bat \\ServName\Admin$
psexec \\ServName -u DOMAIN\UserName -p password cmd.exe /c mybatchfile.bat

Personnaly if I want to start a background process in a batch I use this :
start /B cmd /C "mycommand" which in your case should give :
start /B cmd /C "psexec \\ServName -u DOMAIN\UserName -p password cmd.exe"

Related

PuTTY: How do I automate logging in as sudo and then passing my password

We have this setup where we have password authentication to our remote unix server. I always run the same commands every day and it is very repetitive. What I basically do is:
Open putty, enter hostname
input username and password
sudo su - oracle
input password again
cd /someDirectory
./script.sh
copy output of script and save to txt file
I am able to automate step 1 and 2 by writing a small bat file, but I don't know how to do the rest of the steps
putty.exe -ssh <user>#<server> -pw <password>
You can't with putty, but with the included plink command:
#echo off
REM Automate PUTTY use
set /P user="Enter User:"
set /P server="Enter Server:"
set /P password="Enter Password:"
set /P logfile="Enter SSH logfile:"
REM SUDO SU Requires ASKPASS and X11 forwarding to be configured
REM
plink.exe -ssh -sshlog %logfile% %user%#%server% -pw %password% "cd temp; pwd; ./script.sh"
REM clear inputs from environment
set "server="
set "user="
set "password="
set "logfile="
echo on
Use a command like plinktest.bat | tee readable_output.log to run the batch script and place the readable output into a file called readable_output.log.
You could place all the commands in a text file and redirect it to the batch script or use the -m switch.
I hope that gets you started.

Running a UNC path using PSexec through CMD

I got this code below, it will run the LS-PrePost-3.0-Win32_setup.exe using cmd.
My psexec is in c:\psexec. And I put put my batch file and the exe file in the same folder.
c:\psexec\psexec -d \\%%M cmd /c start /wait "%~dp0LS-PrePost-3.0-Win32_setup.exe" /quiet /silent /norestart
My code seems does nothing. It execute the code but the exe file didnt run the the remote PC.
Edit: I changed the directory of Psexec.
You can try with something like
c:\psexec\psexec \\%%M -d -c "%~dp0LS-PrePost-3.0-Win32_setup.exe" /quiet /silent /norestart
As you are not waiting (-d) you don't need the start /wait
As you are starting a executable file not an internal command, you don't need the cmd /c.
The file is executed in the remote machine, so, it must be available in the remote machine. Copy it (-c)
From psexec help
-c Copy the specified program to the remote system for
execution. If you omit this option the application
must be in the system path on the remote system.
-d Don't wait for process to terminate (non-interactive).
cmd Name of application to execute.
arguments Arguments to pass (note that file paths must be
absolute paths on the target system).
You can use pushd and popd to pass the the command through psexec for UNC paths.
psexec.exe //FQDN -d cmd /c "pushd \\UNCFOLDER\ && file2execute.exe && popd"

How to connect to adb using a batch file?

I want to create a batch file which will open command prompt and connect to adb after changing the directory. My current code opens command prompt and change the directory but does not executes the last command of connecting to adb. If anyone can help me in this direction, it would be great.
#ECHO OFF
cmd.exe /K "cd C:\Users\SAM\AppData\Local\Android\sdk\platform-tools"
adb connect 192.168.200.25:5555
cmd /k starts a command prompt process that runs a command and does not quit afterwards. Why are you using cmd.exe at all?
#echo off
cd /d c:\users\sam\appdata\local\android\sdk\platform-tools\
adb connect 192.168.200.25:5555

PsExec use MKDIR

Hi I try to create a folder on local user which doesn't have access right.
So the script is to help user creating a folder by using domain admin account and will be converted to EXE when it run.
So I figure out using PsExec to use it. But I'm no idea how to combine it with MKDIR.
Here is the BAT script.
#echo off
setlocal enableextensions
set today=%date:~10,4%-%date:~7,2%-%date:~4,2%
set todayyear=%date:~10,4%
set todaymonth=%date:~10,4%-%date:~7,2%
set pass_local=adminpass
set path_pskill="\\SERVER\$PROGRAM\PsKill.exe"
set path_psexec="\\SERVER\$PROGRAM\PsExec.exe"
set path_backup=\\SERVER\$WORKSTATIONS\UPGRADE
set path_sourcedrive=C:\Users
set path_source01=AppData\Local\Microsoft\Outlook
set path_source02=Contacts
set path_source03=Desktop
set path_source04=Documents
set path_source05=Downloads
set path_source06=Favorites
set path_source07=Pictures
set path_source08=Music
set path_source09=Videos
subst b: "%path_backup%"
%path_psexec% \\%computername% -u DOMAIN\Administrator -p %pass_local% "cmd /C \"mkdir \"b:\%todayyear%\%todaymonth%\%username%\"\""
subst b: /D
Other simple to understand is:
psexec.exe \\hostname -u DOMAIN\Administrator -p adminpass "cmd /C \"mkdir \"b:\%todayear%\%todaymonth%\%username\"\""
Anyone please can help ?
Use the below command to create a directory in remote location.
psexec \\\IPAddress -u username -p Password cmd /c mkdir c:\testfolder
Actually, you're on the right track as long as the login credentials that you're adding is correct, else, you'll receive an access denied error. Also, you may have to place some START /wait timers in the batch script to make sure that the lines before the psexec call get executed first.

Opening cmd using psexec to a specific remote directory

The goal is to delete the temporary internet files on a remote computer.
start psexec -u domain\username -p password -s \\xxx.xxx.xxx.xxx
cmd cd C:\Documents and Settings\USERACCOUNT\Local Settings\Temporary Internet Files
Which is connecting to the remote computer but just opening to c:windows\system. I am then able to cd to that directory and use del /f /s /q *.* to delete all the problem files.
I tried using psexec \\computer cmd /c del fileName but had even less luck with that.
Why bother cd'ing to directory first? This should work:
psexec \\1.2.3.4 -u "domain\username" -p "password" cmd /c del /f /s /q "C:\Document and Settings\USERACCOUNT\Local Settings\Temporary Internet Files"
I finally got it to work using
psexec -u domain\user -p password \\xxx.xxx.xxx.xxx -s cmd /c rd "C:\Documents and Settings\%USERACCOUNT%\Local Settings\Temporary Internet Files\Content.IE5\" /s /q
which connects to the remote server, goes straight to the directory I want and proceeds to delete the particular temp internet files folder that's causing issues without bothering me at all.
Your command is okay but missing the server name
psexec \\\servername -u ...
Use the below command to create a directory in remote location.
psexec \\\IPAddress -u username -p Password cmd /c mkdir c:\testfolder

Resources