How to make SCHTASKS run command? - windows-task-scheduler

I am new to SCHTASKS and cannot get it to run a simple command. What I tried is below. The file c:\downloads\temp_w.txt is not created. What is wrong?
c:\downloads>ver
Microsoft Windows [Version 10.0.17763.503]
c:\downloads>SCHTASKS /Create /SC MINUTE /MO 1 /TN mydir /TR "dir c:\windows > c:\downloads\temp_w.txt"
WARNING: The task name "mydir" already exists. Do you want to replace it (Y/N)? y
y
SUCCESS: The scheduled task "mydir" has successfully been created.
c:\downloads>schtasks /run /tn mydir
SUCCESS: Attempted to run the scheduled task "mydir".
c:\downloads>dir temp_w.txt
Volume in drive C has no label.
Volume Serial Number is ECC7-1C96
Directory of c:\downloads
File Not Found

DIR is an internal command (see: https://ss64.com/nt/syntax-internal.html)
The following should work (untested):
c:\>SCHTASKS /Create /SC MINUTE /MO 1 /TN mydir /TR "cmd /c dir c:\windows > c:\downloads\temp_w.txt"

Related

Run .exe on remote machine with PSEXEC

I am trying to run an .exe that sits on a remote machine with PSEXEC, but it does not run properly.
The exe is a converted python script:
usage: WindowsUpdates3.exe [-h] [-v] [-m] [-i] [-u] [-r] [-s SKIP]
List, download and update Windows clients
optional arguments:
-h, --help show this help message and exit
-v, --version Show program version and exit
-m, --list-missing List missing updates
-i, --list-installed List installed updates
-u, --update List and install missing updates
-r, --reboot Reboot after installing updates if needed
-s SKIP, --skip SKIP Skips these KB numbers
I am able to run the .exe as intended with a PSEXEC console session:
PSEXEC \\<hostname> cmd
Navigate to the exe my.exe -i, run it and it works the same way if I execute it on the machine locally.
When I try to execute the file directly some fucntionality does not work
PSEXEC.exe \\<hostname> -h "C:\WindowsUpdates\WindowsUpdates3.exe" -i
...
C:\WindowsUpdates\WindowsUpdates3.exe exited on <hostname> with error code 0.
I am able to get the help-menu (-h) and the version (-v) of the exe with the above command.
The other arguments don't return anything but code 0 and the --update argument throws a com_error: -2147024891, which translates to access denied...
How can this be as I have the same privileges as if I spawn a cmd terminal?

taskscheduleR ERROR: The system cannot find the file specified

I tried to add a task scheduler from taskscheduleR on rstudio but it gives this error;
Warning: running command 'schtasks /Delete /TN "script1.R" /F' had status 1
Creating task schedule: schtasks /Create /TN "script1.R" /TR "cmd /c C:/PROGRA~1/MICROS~3/srv~1/R_SERVER/bin/Rscript.exe \"C:/Users/user1/Desktop/script1.R\" >> \"C:/Users/user1/Desktop/script1.log\" 2>&1" /SC ONCE /ST 23:40
how can I solve it?
Might have to do with the locale settings. I faced same issue and I used the task scheduler to set up a job for the future date (01-01-2020). The job set successfully. Then I went to Windows Task Scheduler and adapted the starting date to be as I wanted (e.g. 19-09-2019).

Delete task on multiple remote servers

I can run this command line to delete task on single remote server:
SCHTASKS /delete /tn "testTask" /s (hostname) and it works perfect.
However I need to be able to do the same on multiple servers from one command line or batch file. I've tried using for loop option to run against the list of servers provided in text file and seems like it does not read that text file. Here is the example:
For /F %f in (c:\temp\testservers.txt) do schtasks /delete /tn "testTask"
it outputs this ERROR: The specified task name "testTask" does not exist in the system.. Its seems like it searching task on my laptop instead of on remote servers provided in .txt file. (task does exist on remote server)
I have used the same For loop command to create task on multiple servers with a bit different parameters and it works fine. Any help would be greatly appreciated. Let me know if you need any clarification.
I think you simply forgot to add your %f parameter after the Delete command, which doesn't use the same parameters as the Create command, as documented on this MSDN page:
schtasks /delete /tn {<TaskName> | *} [/f] [/s <Computer> [/u [<Domain>\]<User> [/p <Password>]]]
So you should try :
For /F %f in (c:\temp\testservers.txt) do schtasks /delete /tn "testTask" /s %f
To log the results in a file, use something like:
For /F %f in (c:\temp\testservers.txt) do schtasks /delete /tn "testTask" /s %f >> C:\Mylogs\TaskDeletions.log

Transfer folder from one SFTP server to another every day

I have two different servers. I use WinSCP to transfer a folder manually:
I connect to SFTP on SVR-01 -> Transfer folder to my local machine
I connect to SFTP on SVR-02 -> Transfer folder from my local machine to SVR-02
What I want?:
I would like to do it automatically.
Example: in a command OR script, the SVR-01 transfer folder /opt/home/files-everyday/ to SVR-02 /opt/backup/
When transfer is finished, delete /opt/home/files-everyday/ from SVR-01.
This should be done every day.
One folder is created every day by date "20150613" in SVR-01 and this folder to be automatically transferred to the SVR-02.
You can use WinSCP scripting from a Windows batch file:
#echo off
mkdir %temp%\intermediate
winscp.com /log=c:\path\log.log /command ^
"open sftp://user1:password1#server1.example.com/ -hostkey=""ssh-rsa 2048 87:d6...a7""" ^
"get /opt/home/files-everyday/* %temp%\intermediate\" ^
"open ftps://user2:password2#server2.example.com/" ^
"put %temp%\intermediate\* /opt/backup/" ^
"session 1" ^
"rm /opt/home/files-everyday/*" ^
"exit"
rmdir /q /s %temp%\intermediate
Then use the Windows scheduler to run the script regularly (every day).

sudo login using plink executing by a batch file (mind blogging)

Here is what I wanna do... connent to a remote unix server from plink which is executed in a batch file, to run a script of other user name(as sudo user). I tried this.
#echo off
set /p id="Enter Unix Login ID: " %=%
set /p pwd="Enter Password: " %=%
cls
del jobInfo_error.txt
plink -l %id% -pw %pwd% Server0 "sudo su - user222" 2>>JobInfo_error.txt
plink -l %id% -pw %pwd% Server0 "/export/home/user222/user222_unix_works.ksh" 2>>JobInfo_error.txt
start notepad JobInfo_error.txt
In the above code i login as user000 and then i wanna sudo login as user222 and run the script user222_unix_works.ksh. The privilege for executing and writing is only given to user222 and i dont wanna change it.
here is why i think I am not geting the result I want, the first plink command plink -l %id% -pw %pwd% Server0 "sudo su - user222" 2>>JobInfo_error.txt is execute but the session is terminated and when the second runs i login as user000 and fails to execute.
is there a way to achive this?
here is the error in JobInfo_error.txt
bash: /export/home/user222/user222_unix_works.ksh: Permission denied
oh 2 more things i cannot directly login as user222
and i can execute any file for which user000 has privileges. i.e if i simple run with
plink -l %id% -pw %pwd% Server0 "/export/home/user000/user000.ksh"
it works.

Resources