Invoke .cmd file using variable name - powershell

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"

Related

parse error in one-line powershell script

I am trying to create a one-line powershell script that just requests an url. The script is working fine when I run it as a ps1 file:
File "test.ps1":
$webclient=New-Object "System.Net.WebClient"
$data=$webclient.DownloadString("https://google.com")
I run this script in PS console like this:
PS C:\test.ps1 -ExecutionPolicy unrestricted
This runs without any problem, but when I try to schedule this script and make it a one-line according to these recommendations i.e. replace "" with '' and separate commands with ; so the result will be:
one-line:
powershell -ExecutionPolicy unrestricted -Command "$webclient=New-Object 'System.Net.WebClient'; $data=$webclient.DownloadString('https://google.com');"
Then I got the following problem:
Error:
The term '=New-Object' is not recognized as the name of a cmdlet,
function, script file, or operable program
I tried another script that also works fine as ps1 file, but not working as one-liner:
$request = [System.Net.WebRequest]::Create("https://google.com")
$request.Method = "GET"
[System.Net.WebResponse]$response = $request.GetResponse()
echo $response
one-line:
powershell -ExecutionPolicy unrestricted -Command "$request = [System.Net.WebRequest]::Create('https://google.com'); $request.Method = 'GET'; [System.Net.WebResponse]$response = $request.GetResponse(); echo $response"
Error:
Invalid assignment expression. The left hand side of an assignment
operator needs to be something that can be assigned to like a variable
or a property. At line:1 char:102
According to get-host command I have powershell v 2.0. What is the problem with one-line scripts above?
Put the statements you want to run in a scriptblock and run that scriptblock via the call operator:
powershell.exe -Command "&{$webclient = ...}"
Note that pasting this commandline into a PowerShell console will produce a misleading error, because PowerShell (the one into which you paste the commandline) expands the (undefined) variables in the string to null values, which are then auto-converted to empty strings. If you want to test a commandline like this, run it from CMD, not PowerShell.
It might also be a good idea to have the scriptblock exit with a status code, e.g.
&{...; exit [int](-not $?)}
or
&{...; $status=$response.StatusCode.value__; if ($status -eq 200) {exit 0} else {exit $status}}

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('\')

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

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

How to run a Powershell script from the command line and pass a directory as a parameter

PowerShell -Command .\Foo.ps1
Foo.ps1:
Function Foo($directory)
{
echo $directory
}
if ($args.Length -eq 0)
{
echo "Usage: Foo <directory>"
}
else
{
Foo($args[0])
}
Despite Foo.ps1 being in the directory from where I am calling Powershell, this results in:
The term '.\Foo.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.
EDIT: Wasn't working because PowerShell was changing directory due to profile.ps1 containing cd C:\
I then tried to call it specifying the full path to the script file, but no matter what I try, I can't get it to work. I believe I have to quote the path because it contains whitespaces, as does the file name I need to pass in an argument to the script.
Best guess so far:
PowerShell -Command "'C:\Dummy Directory 1\Foo.ps1' 'C:\Dummy Directory 2\File.txt'"
Outputs error:
Unexpected token 'C:\Dummy Directory 2\File.txt' in expression or statement.
At line:1 char:136.
try this:
powershell "C:\Dummy Directory 1\Foo.ps1 'C:\Dummy Directory 2\File.txt'"
you are calling a script file not a command so you have to use -file eg :
powershell -executionPolicy bypass -noexit -file "c:\temp\test.ps1" "c:\test with space"
for PS V2
powershell.exe -noexit &'c:\my scripts\test.ps1'
(check bottom of this technet page http://technet.microsoft.com/en-us/library/ee176949.aspx )
Using the flag -Command you can execute your entire powershell line as if it was a command in the PowerShell prompt:
powershell -Command "& '<PATH_TO_PS1_FILE>' '<ARG_1>' '<ARG_2>' ... '<ARG_N>'"
This solved my issue with running PowerShell commands in Visual Studio Post-Build and Pre-Build events.
Add the param declation at the top of ps1 file
test.ps1
param(
# Our preferred encoding
[parameter(Mandatory=$false)]
[ValidateSet("UTF8","Unicode","UTF7","ASCII","UTF32","BigEndianUnicode")]
[string]$Encoding = "UTF8"
)
write ("Encoding : {0}" -f $Encoding)
result
C:\temp> .\test.ps1 -Encoding ASCII
Encoding : ASCII
Change your code to the following :
Function Foo($directory)
{
echo $directory
}
if ($args.Length -eq 0)
{
echo "Usage: Foo <directory>"
}
else
{
Foo([string[]]$args)
}
And then invoke it as:
powershell -ExecutionPolicy RemoteSigned -File "c:\foo.ps1" "c:\Documents and Settings" "c:\test"
you have type and hit enter :
PowerShell -Command

Pass parameter from a batch file to a PowerShell script

In my batch file, I call the PowerShell script like this:
powershell.exe "& "G:\Karan\PowerShell_Scripts\START_DEV.ps1"
Now, I want to pass a string parameter to START_DEV.ps1. Let's say the parameter is w=Dev.
How can I do this?
Let's say you would like to pass the string Dev as a parameter, from your batch file:
powershell -command "G:\Karan\PowerShell_Scripts\START_DEV.ps1 Dev"
put inside your powershell script head:
$w = $args[0] # $w would be set to "Dev"
This if you want to use the built-in variable $args. Otherwise:
powershell -command "G:\Karan\PowerShell_Scripts\START_DEV.ps1 -Environment \"Dev\""
and inside your powershell script head:
param([string]$Environment)
This if you want a named parameter.
You might also be interested in returning the error level:
powershell -command "G:\Karan\PowerShell_Scripts\START_DEV.ps1 Dev; exit $LASTEXITCODE"
The error level will be available inside the batch file as %errorlevel%.
Assuming your script is something like the below snippet and named testargs.ps1
param ([string]$w)
Write-Output $w
You can call this at the commandline as:
PowerShell.Exe -File C:\scripts\testargs.ps1 "Test String"
This will print "Test String" (w/o quotes) at the console. "Test String" becomes the value of $w in the script.
When a script is loaded, any parameters that are passed are automatically loaded into a special variables $args. You can reference that in your script without first declaring it.
As an example, create a file called test.ps1 and simply have the variable $args on a line by itself. Invoking the script like this, generates the following output:
PowerShell.exe -File test.ps1 a b c "Easy as one, two, three"
a
b
c
Easy as one, two, three
As a general recommendation, when invoking a script by calling PowerShell directly I would suggest using the -File option rather than implicitly invoking it with the & - it can make the command line a bit cleaner, particularly if you need to deal with nested quotes.
Add the parameter declaration at the top of ps1 file
test.ps1
param(
# Our preferred encoding
[parameter(Mandatory=$false)]
[ValidateSet("UTF8","Unicode","UTF7","ASCII","UTF32","BigEndianUnicode")]
[string]$Encoding = "UTF8"
)
write ("Encoding : {0}" -f $Encoding)
Result
C:\temp> .\test.ps1 -Encoding ASCII
Encoding : ASCII
The answer from #Emiliano is excellent. You can also pass named parameters like so:
powershell.exe -Command 'G:\Karan\PowerShell_Scripts\START_DEV.ps1' -NamedParam1 "SomeDataA" -NamedParam2 "SomeData2"
Note the parameters are outside the command call, and you'll use:
[parameter(Mandatory=$false)]
[string]$NamedParam1,
[parameter(Mandatory=$false)]
[string]$NamedParam2