I have a program I am trying to run on logon which is dependent on files that are in the same directory that it resides. By manually running the program from the command line, which I have cd'd to, everything runs as it should. However, if I schedule a task to run the program I get an error from my program that complains about not finding a file it needs. So my question(s) is/are: 1.) Is the working directory of a scheduled task the location of the program? 2.) If not, how would I set the working directory to the location of the program?
Here is the code I am using the schedule my task:
SCHTASKS /Create /TN "Test" /TR $MyLocation\Test.exe /sc onlogon /RL Highest
Where "$MyLocation" is a powershell variable that I set to reference the directory of my program.
I've got around this in the past by using a batch file. There's a neat trick you can use in a batch file which will give you the directory that tha batch file is located in. So, you can very easily run the app you want. Create a batch file with the following contents and place it in the directory next to your app, then set it's path as the value for the /TR argument.
%~dp0\Test.exe
Strictly speaking, the backslash isn't required as %~dp0 includes a trailing backslash anyway. I think it makes the batch file a bit more readable though.
If you run a script and send $MyLocation in as a parameter something like this would set up the task o:
$cmdFile = "$MyLocation\Test.cmd"
New-Item $cmdFile -type file
"cd $MyLocation" >> $cmdFile
"powershell -command '& {$($MyLocation)\Test.exe}'" >> $cmdFile
SCHTASKS /Create /TN "Test" /TR $cmdFile /sc onlogon /RL Highest
GĂsli
Related
SCHTASKS /create /S $strComputer /tn "David\Start Gecko" /tr
"'C:\eQube-Tools\Batch and Registry Files\gecko.bat'" /sc once /sd
01/01/2099 /st 01:00 /u Boss /p *password* /RU SYSTEM
pause
SCHTASKS /run /S $strComputer /tn "David\Start Gecko"
So, I have a problem with running the batch file this creates, it says it's running on the task scheduler on the remote pc, but nothing happens. But the odd thing is: if I locally create a "basic task" it runs fine both remotely and running it by hand at that pc.
My question is there any way to have it know you want to create a "basic task" I've looked at the switches but can't find anything for /create, I also tried the /xml but this is something I plan to create, use then destroy so simpler the better.
** the reason I'm using SCHtasks is because I had a lot of issues with redirect errors trying to run it directly using powershell.
Specs
All win 7 WORKGROUP
All have matching Admin Accounts with matching password
Firewall set to allow remote sch tasks
Batch file is on remote pc
the batch file I'm trying to run
#echo off
c:
cd \eQube-Tools\Batch and Registry Files
start virtualhelpscreen.exe
:top
c:
cd \gecko
timeout 12 /nobreak
gecko.exe
cls
goto :top
SCHTASKS /create /S $strComputer /xml "filesavedtomainpc" /tn "David\Start Gecko"
one issue left is it not forcing full screen like it does when you run the batch file but that might be down to the program, for now the question I asked is answered.
I want to write a Windows script (running in Windows 7) that runs every time when a new files has been created in a specified folder. It then checks weather the newly created file matches a certain naming convention and if so it moves it to a different folder.
How can I do the above?
If it doesn't have to be immediately after the files are created, then maybe simple task, checking the folder with interval and using robocopy to move the files will suffice? This can be accomplished with Task Scheduler:
schtasks /create /sc minute /mo 3 /tn FileCopy /tr ROBOCOPY C:\source_folder c:\destination_folder some_file_name*.* /MOV
/sc - Specifies the schedule frequency
/mo - Refines the schedule type to allow finer control over schedule recurrence. In this case it just specifies how many minutes until the task is run again
/tn - Name for the task that will be visible in task scheduler
/tr - Task to be executed /rl
You can also append /rl HIGHEST if you need more privileges to complete the task, and /ru SYSTEM to execute as SYSTEM user (you'll need admin privilege for that), so there won't be any console windows popping out every time tasks executes.
I am trying to become familliar with add task to task scheduler via the command prompt. I seem to be having difficuly with spaces.
This command works successfully in creating a task:
schtasks /create /tn \Daily\SOTask /tr "C:\Users\User Name\FolerWhereScriptIs\vbsScript.vbs" /sc daily /mo 1 /st 08:50
SUCCESS: The scheduled task "\Daily\SOTask" has successfully been created.
However, when I open the properties of this task in task scheduler i see in:
Program/script: C:\Users\User
Add arguments(optional): Name\FolerWhereScriptIs\vbsScript.vbs
Start in(optional): empty
this is obviously not what I want. ideally I would like:
Program/script: C:\Windows\System32\WScript.exe
Add arguments(optional): "C:\Users\User Name\FolerWhereScriptIs\vbsScript.vbs"
Start in(optional): C:\Users\User Name\FolerWhereScriptIs\
How do I achive this from the command line?
This lists all tasks in the Daily folder (so I can see the one I jst created)
schtasks /query /TN Daily\
NOTE: To get this far I ahve gotten help for here and here
EDIT1 I found help here and it mentions using a /V1 flag but todate I cannot get to work. Another Option would be to use XML, whcih I will investigate again. For now I will use the command for setting up my schedule and then go into task scheduler to set the Start in(optional): in directory.
I would like to create a batch file in order to run a windows command on command prompt as administrator whenever I start the PC.
For example I would like to run the command prompt as administrator and run the command Ping 8.8.8.8 on startup of windows.
Can anyone please explain the full procedures with snapshots?
Thanks in advance
M. Ashraful Haque
You could try something like this create another batch file and add the below command it will run it as the user you specify
#echo off
Runas /env /user:PCNAME\Administrator test.bat
Or
you can add your batch file to the Scheduled task list link below on how to do this. In Scheduled Task you can set what account to use and when it should run.
Change when it runs in create task go to Triggers tab click new and change "Begin the task" to start up
Create Scheduled Task
First with SCHTASKS you can create add a batch file to run as a service and you can give the top privileges , example:
SCHTASKS /Create /TN "Ping My Web Host" /SC HOURLY /TR "C:\services\pinghost.cmd" /RU username /RP password /RL HIGHEST
Here's the documentation documentation and here
And this you'd wanted to read this.
I am setting custom schtasks in windows using php to write and execute .bat files like this one:
schtasks /create /tn "RA AUTOMATIC BACKUP" /tr "\"C:\Program Files (x86)\MBM Software\Apache\htdocs\ra\automatic_backup\scriptspg_backup.bat\"" /ru "ProiectIM3" /rp "parola1" /sc minute /mo 1
The touble starts now:
1) if i don't use "\" after /tr the schedule tasks has the action: C:\Program Files and the rest is viewed like an argument
2) if i use "\" the schedule has the right action written:
"C:\Program Files (x86)\MBM Software\Apache\htdocs\ra\automatic_backup\scripts\pg_backup.bat" (quotes included)
BUT it will not run, on the other hand if i click on browse, set up the same file the action becomes
"C:\Program Files (x86)\MBM Software\Apache\htdocs\ra\automatic_backup\scripts\pg_backup.bat" (quotes included) and IT RUNS so the paths are EXACTLY the same but will not run with the first.
I am out of ideeas.
what is your OS and OS language? the /SC parameter is language-specific on old OSes. I.e. it is MINUTO, not MINUTE on Portuguese Windows XP. Also, try to include computer or domain name in the user's name