Problem executing multiple powershell scripts from a batch file - powershell

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.

Related

How to execute a 7zip sfx with extra command line parameters

I have an installer (installer.msi) and I made a self-extracting 7zip with a config file.
Here's my config file :
;!#Install#!UTF-8!
ExecuteFile="Installer.msi"
ExecuteParameters="TARGETDIR=\"[DirectoryPath]\" ALLUSERS=1"
;!#InstallEnd#!
These ExecuteParameters need to be embedded.
Now I have 2 scenario, I want to be able to execute it like normal or fully silent. To execute it fully silent I want to use the /qn switch when executing it from the command line :
.\installer.exe /qn
The problem is if ExecuteParameters is set in the config file, those extra parameters are ignored but works as expected when ExecuteParameters is not set (Tested it).
How can I give these extra parameters to the .msi without embedding it in the config file?

TeamCity running Powershell script, but failing to execute a batch file

I am currently in the process of implementing a deployment method using Teamcity, which runs a Powershell script on my Build Agent, which then configures my Production environment etc.
I have a problem with the Powershell script though, in that it can't seem to run the batch file from it.
The script runs perfectly if I run it manually, it only fails when run via TeamCity.
In the build log I am getting the error:
'myBatchFile.bat' is not recognized as an internal or external command, operable program or batch file.
The batch file and the powershell script are in the same directory and the batch file is called as such:
cmd /c Deploy.bat
I have my TeamCity configuration set up to have the build step for this as:
Script: File
ScriptExecutionMode: Execute script with -File argument
Script Arguments: None
Additional CMD line params: None
I had originally not used the cmd to try to execute the batch file, but executing the batch file like .\Deploy.bat did not seem to work either.
Is there an additional thing I need to set up in order to get the batch file to run? The rest of the script runs fine, just the call to the batch that doesn't.
This is a bit of a wild stab as it's difficult to predict what's happening, but from the description it seems like the path is been altered in the script and it's also dynamic as TeamCity creates temp directories, but if you replace:
cmd /c Deploy.bat
with
cmd /c "$(Split-Path $myinvocation.MyCommand.Path)\Deploy.bat"
then I think this will be able to located the deploy script.
Let me know how it goes.

Powershell script to run multiple batch files in order

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.

try run to `eof script batch file` (.bat)

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.

Disable powershell expansion of command's extension?

We have a lot of existing batch files that help with the running of different perl and ruby scripts.
A batch file (e.g. test.bat) would normally be invoked like:
$ test
and within the batch file, it will set some settings and finally try to run the corresponding script file (e.g. test.pl) like this:
perl -S "%0.pl" %*
All works with cmd.exe, but today, I decided to switch to PowerShell and found out that it expands the commands. So trying to run "test" will actually run "Full\path\test.bat" and my script would complain that there is no file test.bat.pl.
Is there a way to prevent this command expansion? Rewriting all batch files is not an option.
One way is to call cmd explicitly:
cmd /c test