Real n00b to powershell. I was wondering if the community could help me build a pretty simple script.
Really, I can achieve everything I need in windows batch files - but these only complete one task at a time. I want a master script (a powershell script) to call each individual batch process in turn. That is, I want to run Batch_1 and then run Batch_2 only when a task from a program that Batch_1 calls has finished.
Is this possible? Is it a case of using some form of IF THEN ELSE type scenario or is there a way to make powershell wait for some event to happen?
Thanks!
GPC
You can use the FOR command, from regular windows shell (cmd.exe) to solve this problem. The following command executes every cmd file in current directory:
FOR %f IN (*.cmd) DO %f
The following command executes every file, in order, as returned by inner ´DIR´ command:
FOR /F %f IN ('DIR /b /oen *.cmd *.bat') DO %f
Normally calling out from a batch file to a console program is synchronous. A PowerShell script for this is trivial:
master-script.ps1 contents:
---------------------------
c:\batch1.bat
c:\batch2.bat
Now if the batch file is calling a Windows subsystem exe (non a console EXE) then this gets trickier because those execute async. If that's the case, update your question to indicate that.
Related
I have a command that calls powershell using !powershell . it works fine but, the first command will execute a publishing of data action from an external program and does not proceed to next line of code in MATLAB until there is a subscriber to the published data. the problem is that the next line of code is the one that subscribes to the published data so it just runs for ever waiting for the data. any ideas how to make the code continue? I have tried the continue statement but since I have called powershell, it stays there and MATLAB commands do not get executed. Also, I have tried to run the commands backwards, so subscriber first and publisher after but get same issue. Any ideas?
pubPath = 'powershell -inputformat none cd path' ;
subPath = 'powershell -inputformat none cd path2';
[status_one,publish] = system(pubPath);
[status_two,subscribe] = system(subPath);
You need to start the job in the background, such that PowerShell returns immediately before the job finishes. Note that it is PowerShell that waits for the job to finish, not MATLAB.
End the PowerShell command with an ampersand (&) to run it in the background:
[status_one,publish] = system('powershell -inputformat none cd path &');
I have the following function definition in my profile:
function design {
Set-Location $env:CC72\Designer
.\RunDesigner.bat
}
But when I execute the function design, PowerShell executes batch RunDesigner.bat twice and two instances of it are started:
PS C:\Users\s3201> design
Starting Designer...
Starting Designer...
PS C:\customer\Designer>
Why?
Update 1
The content of the batch is the following:
set LOC_WIN32=c:\Customer\xxxxxx
set LOC_UNIX=c:/Customer/xxxxxxx
echo Starting Designer...
start %LOC_WIN32%\bin\xxxxxxx.exe %LOC_UNIX%/Designer/lib/xxxxxxxx.tcl -guiFile %1
Update 2
If I start Vim from the directory where mentioned batch resides then they start both - Vim and batch file.
Therefore the reason for the message in begin of the question is clear - PowerShell tries to start that batch first, does not find it, writes the message and the starts Vim. But why it tries to start the that batch first?
Obviously, the root cause is in some PowerShell definitions. Which ones? (Aliases I checked).
I have scrip contain command line:
set dir=%1
cd %dir%
test.bat
echo successful
When run this script, file test.bat (this file run phpunit) run complete then this script don't run command line echo successful.
So, how to try run to eof script.
Use call test.bat.
When you try running a batch file from another batch like in your question control does not pass back to your calling batch.
Side note: I'd usually use pushd/popd for going into directories from batch files. At least I prefer when a batch file doesn't have a side-effect on the shell I'm working on (similar rationale for setlocal). Also this solves the problem when you pass a directory on another drive (although you could do cd /d in that case.
Let's say I have 5 batch files that run sequentially one after another (executed via the Windows task scheduler on a normal Windows XP PC):
Script1.bat
Script2.bat
Script3.bat
Script4.bat
Script5.bat
Suppose one of the scripts fail (an error condition is detected -- details on how this happens is not important for my question here). How do I stop the other scripts from running if they all run within the task scheduler? For example, if Script1.bat fails, I don't want to run Script2-5.bat. If Script3.bat fails, I don't want to run Script4-5.bat, etc.
I thought about writing a flag value to a temporary file that each script would read from. At the beginning of each script (except for the first one), it will check to see if the flag is valid. The first script would clear out this flag at the beginning each time these set of batch files run.
Surely there is a better way to do this or maybe there is a standard for how to handle this type of situation? Thanks!
Write a master.bat file that conditionally calls each of the scripts in sequence. Then schedule the master instead of directly scheduling the 5 scripts.
#echo off
call Script1.bat
if %errorlevel%==0 call Script2.bat
if %errorlevel%==0 call Script3.bat
if %errorlevel%==0 call Script4.bat
if %errorlevel%==0 call Script5.bat
I have a .bat that calls 3 PowerShell scripts
Basically the bat file looks like this
PScript1
Pscript2
Pscript3
After the Pscript1 the batch file does not execute Pscript2 or Pscript3, it stops and does not seem to return control to the batch file. Does anyone know what might cause this problem ?
In a batch file you would typically use && or || depending on whether or not you wanted the subsequent commands to run based on the success of previous commands e.g.:
powershell.exe .\PScript1.ps1 && powershell.exe .\PScript2.ps1
This invocation would execute the following command only if the preceeding command succeeded. You also need to specify powershell.exe as the EXE. The default action for a .ps1 is to open the file for editing.