I have 8 scripts in Powershell which I run one by one. Let's call the scripts: script1.bat, script2.bat, .., script8.bat.
Now I need a script which runs all scripts.bat one by one, but not simultaneously.
And is there a way to check, if each script was successful?
./script1.bat
./script2.bat
./script3.bat
...
You'll get the picture, I guess. This will run them in sequence. To determine whether they were sucessful or not that depends very much on how those batch files signal errors or sucessful completion. If you exit with exit /b 234 or something similar on an error then you can use $LastExitCode or also $? to determine that. You could also look whether the changes made by those batch files are actually done, of there is no other way of figuring out whether they were sucessful.
Related
I'm trying to automate a workflow. The automation script is mainly written in Powershell It consists of these steps: 1) Opening a program 2) Communicating with the API, reading values, etc. 3) Closing the program. This script will be run many times a day, it would suffice to not close the program every time the script is finishing, but rather check at the beginning of the script whether the program is already opened, and if not, open it. I'd like to implement both, then decide which solution to use later on.
The code for opening the program is completed, but it's not enough to just run an .exe file to open the program, as I have to load the correct settings and GUI, for this while opening the .exe file from the command line, additionally, I have to use -s, also -c. I concluded all this in runProgram.cmd, so in the Powershell script, I only run this file to open the program. However, I am unsure how the already opened program can be detected (that it's opened), and how can I close it. I believe a solution might use processes, with the help of Get-Process, but I'm unsure of its capabilities and limitations (how do I check if my program's process is not amongst the list of running processes?), and whether there is a better way of dealing with this problem.
I have found the solution:
Open the program and open Powershell, and type Get-Process (this will list all the currently running processes)
Search yours (by name). If you don't know which process is the one you're looking for, you can close your program, then type Get-Process again, and look for the process that disappeared from the list, since you closed it. Let's assume the name of it is "yourprocess".
In the code, type $val = Get-Process -Name yourprocess. If it is running, $val should equal some data about the process, if it is not running, then $val is 0. Therefore, if you want to check whether it's opened, you should use:
if($null -ne $val){...}
Finally, stopping the process: Stop-Process -Name yourprocess.
I wrote a simple script that calls a test that takes about 3 days. I redirect the test's output to a log file, so when running the script there's nothing on the screen to indicate progress. It's very simple:
CD C:\Test
test.exe > log.txt
I can check the log file every once in a while sure, but if the machine freezes (which happens) I wouldn't notice right away.
So, I need an idea of a nice way to show progress. Outputting a dot every now and then is not nice I think, since it takes 3 days! Any other idea? As a beginner in PowerShell, an implementation for a given idea would also be nice.
Much appreciated,
Yotam
I have a few Perl scripts on a Solaris SunOS system which basically connect to other nodes on the network and fetch/process command logs. They run correctly 99% of the time when run manually, but sometimes they get stuck. In this case, I simply interrupt it and run again.
Now, I intend to cron them, and I would like to know if there is a way to detect if the script got stuck in the middle of execution (for whatever reason), and preferably exit as soon as that happens, in order to release any system resources it may be occupying.
Any help much appreciated.
TMTOWTDI, but one possibility:
At the start of your script, write the process id to a temporary file.
At the end of the script, remove the temporary file.
In another script, see if there are any of these temporary files more than a few minutes/hours old.
Call CreateObject("WScript.Shell").Run(some.exe,0,False)
I'm using that line to call a .exe that returns some text, but can also write it to a file.
I would use .Exec instead of .Run to get the results directly but then the script hangs.
I really don't want a timer checking if the output file is created or modified.
What I need is a way to catch an event somehow. Any Ideas?
Since you mention that it might either return it or write it to a file, does that mean that after the process has written to the file it'll exit?
If so, you could just call the Shell.Run method with the bWaitOnReturn parameter set to True and your script would wait for the process to finish before it continued.
Otherwise, if the process might write a file and then still continue to run, then I think you will have to poll to check if it exists, or possibly you could create a C# or VB.Net exe that uses FileSystemWatcher (or a normal Win32 exe that uses the API FindFirstChangeNotification) to look for the creation of the file and when it finds one it immediately exists and then you could run that process with bWaitOnReturn set to True, but that's probably just overcomplicating things.
I'm writing a script that performs the same function several times, but when I run the script only one of commands executes leaving the rest not executed after the .bat file has run.
Does this have to do with the long time it takes for my commands to run (15-20 sec)? I've written plenty of bat files and I've never run into this. Do I need to have a sleep function between each command?
I've been trying to figure this one out on google, but my available search terms makes my search results vague and difficult.
Any help is definitely appreciated.
the bat file looks something like the following
IF input1 == "search term" goto location
do something
do something
do something
etc
goto end of file
:location
do something else
do something else
do something else
...
Does one of your "do something else" lines involve calling another batch file? If so, do you use the CALL command?
If you want to call another batch file recursively, you need to use CALL. Otherwise, when the called batch file exits, it does not return to the calling batch file and simply exits. This is a relic from the MS-DOS days; since memory was at a premium, the MS developers decided that the batch interpreter shouldn't keep a call stack by default -- so if you wanted one, you had to use CALL.
See call /? for more information.