Setting up a batch script to run powershell with arguments - powershell

I have a PowerShell script that takes an argument/command and executes well in PowerShell. I am looking at setting up a batch script to run following commands as is to mimic PowerShell
(-config is a function that's defined within script)
C\temp\power_t1.ps1 -config d:\temp\dirlist.txt
I wrote a batch script something like
ECHO OFF
POWERSHELL.EXE -file "C\temp\power_t1.ps1 -config d:\temp\dirlist.txt"
but this doesn't seem to be working.
Any suggestions?
Regards,
Ruben

The script file and the arguments for the script file are separate arguments for powershell.exe. If you look at powershell.exe /?, under -File, you see:
File must be the last parameter in the command, because all characters typed after the File parameter name are interpreted as the script file path followed by the script parameters.
Try:
POWERSHELL.EXE -File "C\temp\power_t1.ps1" -config d:\temp\dirlist.txt
Basically, treat everything after the -File parameter more or less as though you were writing it in PowerShell with the call operator (&).

Related

Jenkins Windows Batch Command Powershell Environment Variables

I have a PowerShell script to be executed as a build step in jenkins and need to pass environment variable to it
powershell -File .\Build.ps1 -Version $env:APP_VERSION_NUMBER
The APP_VERSION_NUMBER is an environment variable set by Version Number Plugin of Jenkins.
For some reason the -Version parameter is never set, and I see only $env:APP_VERSION_NUMBER in console log output.
Is this a syntax issue?
When you use the PowerShell CLI's -File parameter, the arguments passed to the script are treated as literals, so, given that you're invoking the command line not from PowerShell, $env:APP_VERSION_NUMBER is not expanded.
To force the target PowerShell process to evaluate the arguments, you must use -Command rather than -File:
powershell -Command .\Build.ps1 -Version $env:APP_VERSION_NUMBER
However, now that we know that you're invoking the command line via cmd.exe (a batch file) from Jenkins (a build step of type Execute Windows batch command), the simpler answer is indeed to let cmd.exe expand the environment-variable reference, using its %<envVarName>% syntax:
powershell -File .\Build.ps1 -Version "%APP_VERSION_NUMBER%"
Note: Enclosing the environment-variable reference in "..." isn't strictly necessary with a version number, but is a good habit to form, so that values with embedded spaces or other shell metacharacters are passed correctly too.
It turns out indeed its a syntax issue. The fix looks like the following
powershell -File ".\Build.ps1" -Version %APP_VERSION_NUMBER%

Call powershell script with multiple args from command line

My final goal is to have a .bat file that calls a powershell script.
This works OK in powershell (but can't be used in a batch file):
PS build-directory> .\ps1file.ps1 -ScriptArgs '-arg1="val1"', '-arg2="val2"'
But this one (batch file friendly) fails:
PS build-directory> powershell -File ps1file.ps1 -ScriptArgs '-arg1="val1"', '-arg2="val2"'
A parameter cannot be found that matches parameter name 'arg2=val2'.
But it works fine if there is only one param -arg1="val1"
You're having problems because ScriptArgs isn't a valid parameter for using with powershell command line (documentation link).
You just pass the arguments like this:
powershell -File ps1file.ps1 -arg1 "val1" -arg2 "val2"

Need to call powershell script from batch file

I have a batch file which is in a folder called script. The script folder also contains folder called powershell which has a script called IE-Settings.ps1.
I want to execute the powershell script from the batch file and I am unable to give powershell script path in the command. What I tried is
call %windir%\system32\WindowsPowerShell\v1.0\powershell.exe -File "& '%~dp0IESettings\IE-Settings.ps1'"
But it doesn't recognize the path
call is for running other batch files in a way that they return to the current batch file after they terminate, and per your question the subdirectory name is powershell, not IESettings. Also, when using the parameter -File you just specify the path to the file.
powershell.exe -File "%~dp0powershell\IE-Settings.ps1"
The call operator (&) is used when running PowerShell code via the -Command parameter, e.g.:
powershell.exe -Command "& { Write-Host 'foo' }"

How can the argument to a batch file be intrun passed onto a powershell script?

i am trying to call a powershell script through bat file such as below;
Bat file name is Script .bat and its current cuntents are below;
Powershell -sta -Windowstyle Hidden Script.Ps1
I am passing an argument to the bat file how can i pass it on to the powershell script in the bat file?
Please do let me know if any questions or confirmations needed.
In your shell script (batch file), you can write:
powershell -file script.ps1 %1
%1 will be replaced with the shell script's first command-line argument, which will be passed as the first argument to script.ps1.
Bill

Set up PowerShell Script for Automatic Execution

I have a few lines of PowerShell code that I would like to use as an automated script. The way I would like it to be able to work is to be able to call it using one of the following options:
One command line that opens PowerShell, executes script and closes PowerShell (this would be used for a global build-routine)
A file that I can double-click to run the above (I would use this method when manually testing components of my build process)
I have been going through PowerShell documentation online, and although I can find lots of scripts, I have been unable to find instructions on how to do what I need. Thanks for the help.
From http://blogs.msdn.com/b/jaybaz_ms/archive/2007/04/26/powershell-polyglot.aspx
If you're willing to sully your beautiful PowerShell script with a little CMD, you can use a PowerShell-CMD polyglot trick. Save your PowerShell script as a .CMD file, and put this line at the top:
#PowerShell -ExecutionPolicy Bypass -Command Invoke-Expression $('$args=#(^&{$args} %*);'+[String]::Join(';',(Get-Content '%~f0') -notmatch '^^#PowerShell.*EOF$')) & goto :EOF
If you need to support quoted arguments, there's a longer version, which also allows comments. (note the unusual CMD commenting trick of double #).
##:: This prolog allows a PowerShell script to be embedded in a .CMD file.
##:: Any non-PowerShell content must be preceeded by "##"
##setlocal
##set POWERSHELL_BAT_ARGS=%*
##if defined POWERSHELL_BAT_ARGS set POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%
##PowerShell -ExecutionPolicy Bypass -Command Invoke-Expression $('$args=#(^&{$args} %POWERSHELL_BAT_ARGS%);'+[String]::Join(';',$((Get-Content '%~f0') -notmatch '^^##'))) & goto :EOF
Save your script as a .ps1 file and launch it using powershell.exe, like this:
powershell.exe .\foo.ps1
Make sure you specify the full path to the script, and make sure you have set your execution policy level to at least "RemoteSigned" so that unsigned local scripts can be run.
Run Script Automatically From Another Script (e.g. Batch File)
As Matt Hamilton suggested, simply create your PowerShell .ps1 script and call it using:
PowerShell C:\Path\To\YourPowerShellScript.ps1
or if your batch file's working directory is the same directory that the PowerShell script is in, you can use a relative path:
PowerShell .\YourPowerShellScript.ps1
And before this will work you will need to set the PC's Execution Policy, which I show how to do down below.
Run Script Manually Method 1
You can see my blog post for more information, but essentially create your PowerShell .ps1 script file to do what you want, and then create a .cmd batch file in the same directory and use the following for the file's contents:
#ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%MyPowerShellScript.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'"
Replacing MyPowerShellScript.ps1 on the 3rd line with the file name of your PowerShell script.
This will allow you to simply double click the batch file to run your PowerShell script, and will avoid you having to change your PowerShell Execution Policy.
My blog post also shows how to run the PowerShell script as an admin if that is something you need to do.
Run Script Manually Method 2
Alternatively, if you don't want to create a batch file for each of your PowerShell scripts, you can change the default PowerShell script behavior from Edit to Run, allowing you to double-click your .ps1 files to run them.
There is an additional registry setting that you will want to modify so that you can run scripts whose file path contains spaces. I show how to do both of these things on this blog post.
With this method however, you will first need to set your execution policy to allow scripts to be ran. You only need to do this once per PC and it can be done by running this line in a PowerShell command prompt.
Start-Process PowerShell -ArgumentList 'Set-ExecutionPolicy RemoteSigned -Force' -Verb RunAs
Set-ExecutionPolicy RemoteSigned -Force is the command that actually changes the execution policy; this sets it to RemoteSigned, so you can change that to something else if you need. Also, this line will automatically run PowerShell as an admin for you, which is required in order to change the execution policy.
Source for Matt's answer.
I can get it to run by double-clicking a file by creating a batch file with the following in it:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe LocationOfPS1File
you can use this command :
powershell.exe -argument c:\scriptPath\Script.ps1