1

The following is generating the exception:

& : The term '.\Run-Regression.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was 
included, verify that the path is correct and try again.
At line:1 char:3
+ & '.\Run-Regression.ps1' -InputCSV '..\Desktop\tests\V10MWB.csv' -CAR ...
+   ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\Run-Regression.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

What am I doing wrong? and/or how do I resolve this issue?

I do want to preserve relative paths because has additional dependencies.

@ECHO OFF

:: Enable PowerShell script execution
PowerShell Set-ExecutionPolicy Unrestricted

:: Navigate to the 'Common' directory (preserves relative paths)
PUSHD %~dp0\..\Common

:: Prepare the 'logs' directory
IF NOT EXIST ..\logs (MD ..\logs)
DEL /Q ..\logs\*.log 1>NUL 2>&1

:: Execute script
PowerShell "& 'Run-Regression.ps1' -InputCSV '..\Desktop\tests\%1.csv' -CARS_ID 0 -RunOnDesktop -Log -Email -Progress -Archive 2>&1" 1>"..\logs\%1.log";

:: Navigate back to original directory
POPD
Compo
  • 36,585
  • 5
  • 27
  • 39
Allan G
  • 11
  • 2

4 Answers4

0

According to the error message, it can't find the script in the current directory as you are invoking it. Either change to the correct directory, or invoke it with the fully-qualified path.

codewario
  • 19,553
  • 20
  • 90
  • 159
0

Your error message does not match the actual invocation command that is part of your batch file:

PowerShell "& 'Run-Regression.ps1' ..." ...

fails, because PowerShell by design does not permit running executables and scripts by mere file name from inside PowerShell (whether invoked directly or via &, the call operator).

Instead, you must prepend .\ to explicitly signal the intent to run a script from the current directory.

PowerShell "& .\Run-Regression.ps1 ..." ...

If your *.ps1 isn't actually in the current directory, but in the batch file's, see Vladimir Dronov's helpful answer

However, consider using the -File CLI parameter instead of the (implied) -Command parameter, in which case the .\ prefix isn't needed and which simplifies the syntax in general:

PowerShell -File Run-Regression.ps1 -InputCSV ..\Desktop\tests\%1.csv -CARS_ID 0 -RunOnDesktop -Log -Email -Progress -Archive 2>&1 1>"..\logs\%1.log";

Note:

  • There are many subtle differences between using -File and -Command; for more information, see this answer.

  • PowerShell [Core], whose executable name is pwsh.exe, defaults to -File, not -Command, a change that was needed to support scripts with shebang lines on Unix-like platforms.

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

Working with relative paths there can be a misunderstanding with what is a startup folder where your batch file is started. If your batch and PowerShell scripts located in the same folder and you don't want to care about startup folder, try %~dp0 instruction - it'll point to the folder, where batch file is located. For example, this will execute Run-Regression.ps1 script located in the same folder with bat\cmd file without taking into account execution policy and startup folder.

PowerShell.exe -ExecutionPolicy Bypass -File %~dp0Run-Regression.ps1

You can find more useful thing in this thread: What does %~dp0 mean, and how does it work?

Vladimir Dronov
  • 1,182
  • 9
  • 11
0

Thank you all for your responses, because they helped me bring together the following solution which is working very well for my situation:

PUSHD %~dp0..\Common

...

PowerShell "& '%CD%.\Run-Regression.ps1' ... POPD

Thanks again, Allan G.

Allan G
  • 11
  • 2