Attempt to run a PowerShell script from a batch file is failing - powershell

The following batch-file 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 powershell 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

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.

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?

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.

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.

Related

How to execute powershell code in current directory from terminal?

I run a powershell command like this
powershell.exe -File C:\GitHub\project\test\run.ps1 "foo bar"
And my current directory is already C:\GitHub\project\test. How can I make the -File argument just start from the current directory?
Thanks
Simply omit the directory path altogether (powershell.exe -File run.ps1 "foo bar") or prefix the script file name with .\ (powershell.exe -File .\run.ps1 "foo bar") in order to run a script located in the current directory.
Note that in cases where you need to use -Command rather than -File, the .\ prefix is required, because PowerShell then treats the CLI arguments as PowerShell code, and for security reasons PowerShell doesn't permit running scripts in the current directory by file name only - see this answer.
See also: about_PowerShell_exe, the documentation of the Windows PowerShell CLI (for PowerShell [Core] v6+, whose executable file name is pwsh, the relevant topic is about_pwsh.)

How to Handle Blanks in %UserProfile% Batch File?

I have created a script that copies our work PowerPoint Presentation Template from DropBox and saves it as the default template.
Some user accounts have spaces, others do not, and I cannot get the Batch file used to trigger this script to run.
I have tried every type of formatting I can think of for this line {}""'' etc
"'%UserProfile%'\'Dropbox'\'Group Team Folder'\'Templates'\'PowerPoint Template.potx'"
The expected result would be for the Batch file to use the blank in the username "John Smith" as part of the path to the PowerPoint template instead of cutting it off.
it spits out an error stating the path "C:\Users\John" is not recognized.
Any help would be greatly appreciated
Edit
#ECHO OFF
If exist "%UserProfile%\Dropbox\Group Team Folder\Templates\PowerPoint Template.potx" (
PowerShell.exe -ExecutionPolicy Bypass -Command "%UserProfile%\Dropbox\Group Team Folder\Templates\Script\Powerpoint Template.ps1"
)
If exist "%UserProfile%\Dropbox (Group)\Group Team Folder\Templates\PowerPoint Template.potx" (
PowerShell.exe -ExecutionPolicy Bypass -Command "%UserProfile%\Dropbox\Group Team Folder\Templates\Script\Powerpoint Template.ps1"
)
PAUSE
Without single ticks, it does not work on the systems with a user profile with no spaces.
Note the reason the code is repeated is because there are 2 different directories for Dropbox on certain systems so it uses the PowerPoint template to determine if the path what path is correct for that system.
Edit 2
C:\Users\John\Dropbox\Group : The term 'C:\Users\John\Dropbox\Group' 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:1
+ C:\Users\John\Dropbox\Group Team Folder\Templates\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
That is my command prompt error.
I have no idea why this isn't working.
These are the usual ways to run a powershell script;
using -File:
PowerShell.exe -ExecutionPolicy Bypass -File "%UserProfile%\Dropbox\Group Team Folder\Templates\Script\Powerpoint Template.ps1"
using -Command:
PowerShell.exe -ExecutionPolicy Bypass -Command "& '%UserProfile%\Dropbox\Group Team Folder\Templates\Script\Powerpoint Template.ps1'"
I had the same problem with some users trying to copy files from\to their profiles.
I end up using for windows 8.1 the $env variables.
$env:systemDrive\users\$env:USERNAME
but I see you want to run a PowerShell script there so Compo give you the hint on how to run those.

Powershell can not call ImageMagick commands - IM Montage

I wrote a script in PowerShell and I am having various success calling Image Magick's montage.exe on different computers. The computer on which I wrote the script has no problem executing the 'montage' command, however on another computer with IM Installed the script errors out:
montage : The term 'montage' 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 \\Server\Contact_Sheet_Local.ps1:51 char:9
+ montage -verbose -label %t -pointsize 20 -background '#FFFFFF ...
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (montage:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I have tried using montage.exe and even tried the entire path C:\Program Files\ImageMagick-7.0.3-Q16\montage.exe. Additionally I tried setting the directory first:
Set-Location -Path 'C:\Program Files\ImageMagick-7.0.3-Q16'
montage...
Each time on a particular computer this fails. I have tried with IM versions 7.0.3-Q16 and 6.9.1-6-Q8 both x64 as both computers are x64
In the past, I have created scripts in .bat that use ImageMagick and I had to define the full path to the .exe as I mentioned above. But this doesn't seem to help in PowerShell.
Does anyone have any advice or experience with this problem?
If your path has spaces, it will fail if you're just trying to execute based on that. You'll want to utilize the dot operator
$Exe = 'C:\Program Files\ImageMagick-6.9.1-6-Q8\montage.exe'
If (-not (Test-Path -Path $Exe))
{
$Exe = 'C:\Program Files\ImageMagick-7.0.3-Q16\montage.exe'
}
. $Exe -arg1 -etc
PowerShell does not execute programs in the current directory by default. If you want to run an executable that's sitting in the current directory, prefix the executable's name with .\ or ./. Example:
Set-Location "C:\Program Files\ImageMagick-7.0.3-Q16"
.\montage.exe ...
If you have an executable's name in a string or string variable and you want to execute it, you can do so using the & (call or invocation) operator:
& "C:\Program Files\ImageMagick-7.0.3-Q16\montage.exe" ...
If you specify a path and filename that doesn't contain spaces, the & operator isn't required; example:
C:\ImageMagick\montage.exe ...
You could also write it this way:
& C:\ImageMagick\montage.exe ...
If you have an executable's filename in a string variable and you want to execute it, use &; example:
$execName = "C:\Program Files\ImageMagick-7.0.3-Q16\montage.exe"
& $execName ...

Call .ps1 from cmd file give the error: : The term '.\setup.ps1' is not recognized as the name of a cmdlet, function, script file

Via VS 2017 calling Setup.cmd which contains:
#echo off
chcp 65001
powershell -ExecutionPolicy Unrestricted .\setup.ps1 "%*"
The file is called, and this error appear:
Active code page: 65001
.\setup.ps1 : The term '.\setup.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:1
+ .\setup.ps1 -SkipDbInstall:0 -SkipPandoSupportInstall:0 -SkipSearchServiceInstal ...
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\setup.ps1:String) [], Command NotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
setup.ps1 exist in the same level like setup.cmd.
What can be the problem?
Most likely your working directory is not what you think it is. You can verify that by adding a line echo %CD% to the batch file.
To change the working directory to the folder in which the scripts reside add a line cd /d "%~dp0". You also need to remove the quotes from %*, otherwise all arguments to your batch script will be passed as a single string argument to the PowerShell script. And I'd recommend using the parameter -File with powershell.exe, so that you'll get a proper exit code from the PowerShell script (if it returns one).
#echo off
cd /d "%~dp0"
chcp 65001 >nul
powershell.exe -ExecutionPolicy Unrestricted -File .\setup.ps1 %*
If the PowerShell script doesn't care about the working directory you could also run it with the full path instead of changing the directory:
powershell.exe -ExecutionPolicy Unrestricted -File "%~dp0setup.ps1" %*
Sometimes you have to change the directory by yourself:
PowerShell -Command "cd \scripts; .\setup.ps1"
My problem was that windows environment variable was pointing to 2 msbuild file locations on the file system, so when I ran the command msbuild in cmd - the wrong msbuild was called.
Deleting the wrong one and leaving the good one, in the windows environment variable - did the trick!

Powershell to unblock all the files in a folder

Trying to make a .bat I can drop in a folder, when run it will unblock all the files in that folder...
#ECHO OFF
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {get-childitem '%~dp0' | unblock-file}"
EXIT
...keeps telling me "The term 'unblock-file' is not recognized as the name of a cmdlet..." no matter how I try to format it, where am I going wrong?
I'm trying to do this so I can just copy the .bat to the folder (and NOT have to copy a .bat and .ps1) so I thought a 1-line powershell "call" was the way to go?
The unblock-file command is available from Powershell 3.0.
Upgrade your PowerShell and script should work
Tested and working:
dir -r | unblock-file
Unblocks everything from the current directory recursively