How to execute a .Bat with mutliple Parameters from Powershell scripts - powershell

how do I execue a .bat from a powershell script.
I want to do something like:
foreach($item in $list){
param1= $item.Name
param2= $item.path
C:\Filesystem_Batches\test.bat param1 param2
}
Thank you

You can call
cmd.exe /C "test.bat param1 param2"
In powershell V3 there is a new espace string --% which allow to send "weird" parameters to your exes.
exemple :
PS> echoargs.exe --% %USERNAME%,this=$something{weird}
Arg 0 is <jason,this=$something{weird}>

I am new to Powershell but the following works for me. I am able to run the *.bat from my *.ps1 file. And I am using Powershell 5.1.17134.590.
& .\myFile.bat parm1

Related

Wrong encoding with powershell shell command

I setup my powershell so I can drag and drop files on .ps1 files and start the script with the file paths as arg parameters.
I set a default key to
\HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command
Default key
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -File "%1" %*
But this command can't handle files with cyrillic or japanese characters.
For example, create a test.ps1 file:
foreach ($arg in $args) {
Write-Host "$arg"
}
pause
and I start this script like this:
powershell -File test.ps1 "ダーク" "Олег"
I get this result:
PS C:\Users\...\Desktop> powershell -File test.ps1 "ダーク" "Олег"
???
????
Is there an alternative command I can set to start a script or is there a way to force an encoding for this command?

Invoke .cmd file using variable name

The current PowerShell script invokes a .cmd file as follows:
cd $pathToCmdFile
.\Xyz.Web.deploy /y
I would like to change this so that Xyz is used as a variable, something like:
$webAppName = "Xyz"
cd $pathToCmdFile
.\$webAppName.Web.deploy /y
I've already tried
$cmd = ".\$webAppName.Web.deploy /y"
Invoke-Expression $cmd
Shows
The term '.\Xyz.Web.deploy' is not recognized as the name of a cmdlet, function, script file or operable program...
I've also tried Invoke-Command, Invoke-Item but none seem to work. I have also tried putting an ampersand ($cmd = "& .\$webAppName.Web.deploy /y"), but that didn't work either.
The proper way of doing what you want is to use the call operator (&). You must not include the operator or command arguments in the command string, though.
This should work:
$webAppName = "Xyz"
cd $pathToCmdFile
& ".\${webAppName}.Web.deploy.cmd" /y
I strongly recommend to always specify commands/scripts with their extension.
So Invoke-Expressions should work:
test.cmd
set
test.ps1 calls test.cmd
cd .
$webAppName = "Test"
Invoke-Expression ".\\$webAppName.cmd"
So now you run the powershell script
> .\test.ps1
ALLUSERSPROFILE=C:\ProgramData
ANSICON=234x1000 (234x47)
ANSICON_DEF=7
...
Or you could do this so you can pass a parameter to the powershell script
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]$webAppName
)
cd .
Invoke-Expression ".\\$webAppName.cmd"

Powershell Inkove-expression

Yesterday I read about the invoke-expression
from
http://technet.microsoft.com/en-us/library/dd347550.aspx
invoke-expression -command "c:\batfile.bat"
It works very well, but how can I get the results and also , how to add args for .bat file?
Invoke-Expression is to Call up the EXpression,And '.Bat' is not an expression.
So if you want to call .Bat File Justc:\batfile.bat Could Do the trick.
Or if you want to explore on the invoke-expression Copy the commands in Batch file into a variable to see the code being executed.
There was similar instance where I had to do something similar,And I had used it as
$prog="cmd.exe"
$TARGETDIR = 'd:\temp'
$x='C:\Users\v-chetak\Desktop\1.bat'
$params=#("/C";"$x";" >d:\temp\result17.txt")
Start-Process -Verb runas $prog +$params
The $params Will Hold all the arguments you want to pass.

passing \ in argument to powershell script causes unexpected escaping

This is my powershell script test.ps1:
Write-Output $args;
Now suppose I have a batch script that calls this powershell script with all kinds of paths. One of those is c:\:
powershell -executionpolicy Bypass -file test.ps1 "c:\"
The output is:
c:"
Is there any way to quote my arguments such that c:\ would actually be taken and stored as is in the $args[0] variable? I know I can solve this quick'dirty by passing "c:\\", but that's not a real solution.
EDIT: using named parameters in test.ps1 doesn't make any difference:
[CmdletBinding()]
param(
[string]$argument
)
Write-Output $argument;
EDIT2: using a batch file instead works fine.
My test.bat script:
echo %~1
I run it:
test.bat "c:\"
Returns nicely:
c:\
Are you sure this comes form powershell and not from the program which invokes your statement? The backslash is no escape code in powershell.
my test.ps1 is working, when run from ise.
this works for me:
powershell -executionpolicy Bypass -command "test.ps1 -argument 'C:\'"
(end with quote double-quote)
Help file for PowerShell.exe says:
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.
You are against Powershell.exe's command line parser, which uses "\" to escape quotes. Do you need quotes? Not in your case:
powershell -file test.ps1 c:\
prints
c:\
Similarly, this works too
powershell -file test.ps1 "c:\ "
c:\
but then your arg has that extra space which you would want to trim. BTW, Single quotes do not help here:
powershell -file test.ps1 'c:\'
'c:\'
If you need the final backlash to be passed to the command, you can use
$ArgWithABackslashTemp = $ArgWithABackslash -replace '\\$','\\'
&$ExePath $ArgWithABackslashTemp
Or, if the exe is smart enough to handle it without the trailing backslash
&$ExePath $ArgWithABackslash.trim('\')

Calling powershell function with parameter from .cmd or .bat file

I have written a powershell script which is a complete function taking parameters (e.g. function name (param) { } ) and below this is a call to the function, with the parameter.
I want to be able to call this function in its .ps1 file, passing in the parameter. How would I be able to package a call to the function via a .bat or .cmd file? I am using Powershell v2.0.
You should use so called "dot-sourcing" of the script and the command with more than one statement: dot-sourcing of the script + call of the function with parameters.
The test script Test-Function.ps1:
function Test-Me($param1, $param2)
{
"1:$param1, 2:$param2"
}
The calling .bat file:
powershell ". .\Test-Function.ps1; Test-Me -Param1 'Hello world' -Param2 12345"
powershell ". .\Test-Function.ps1; Test-Me -Param1 \"Hello world\" -Param2 12345"
Notes: this is not a requirement but I would recommend enclosing the entire command text with double quotation marks escaping, if needed, inner quotation marks using CMD escape rules.
I believe all you have to do is name the parameters in the call to the script like the following:
powershell.exe Path\ScripName -Param1 Value1 -Param2 Value2
Param1 and Param2 are actual parameter names in the function signature.
Enjoy!
To call a PowerShell function from cmd or batch with arguments you need to use the -Commmand Parameter or its alias -C.
Romans answer will work with PowerShell 5.1 for example but will fail for PowerShell 7.1.
Quote from an issue I left on GitHub on why the same command didn't work is:
So as to support Unix shebang lines, pwsh's CLI now defaults to the -File parameter (which expects only a script-file path), whereas powershell.exe default to -Command / -c.
To make your commands work with pwsh, you must use -Command / -C explicitly.
So if you have a PowerShell file test.ps1 with:
function Get-Test() {
[cmdletbinding()]
Param (
[Parameter(Mandatory = $true, HelpMessage = 'The test string.')]
[String]$stringTest
)
Write-Host $stringTest
return
}
And the batch file will then be:
rem Both commands are now working in both v5.1 and v7.1.
rem v7.1
"...pathto\pwsh.exe" -NoExit -Command ". '"...pathto\test.ps1"'; Get-Test ""help me"""
rem v5.1
powershell.exe -NoExit -Command ". '"...pathto\test.ps1"'; Get-Test ""help me"""
The quotes around ...pathto\test.ps1 are a must if your .ps1 contains spaces.
The same goes for ...pathto\pwsh.exe
Here's the Github issue I posted in full:
https://github.com/PowerShell/PowerShell/issues/15281