Want to use some PS command in Cmd script to set a var
Posted: 2021-Jul-26, 8:37 pm
28 Nov 2018 20:08
be
----------------------------
#2 29 Nov 2018 00:38
npocmaka
try like this:
or:
you can even combine powershell and batch code in one file (save this as .bat):
----------------------------
#3 30 Nov 2018 20:37
Simon Sheppard
npocmaka has given you some solutions, but to explain - these 2 lines:
To run the commands in a single process, you need to pass a single script or command to Powershell.
See this page for powershell.exe
https://ss64.com/ps/powershell.html
----------------------------
#4 01 Dec 2018 03:52
be
It demonstrates that the var is created.
Given that the var it is gone after the line, I continued the line with
a long series of commands
in order to have the var for what I wanted to do with it.
I much appreciate that you added it into your reply.
----------------------------
#5 01 Dec 2018 03:55
be
----------------------------
#6 03 Dec 2018 20:22
be
(This occurs also in script.)
DO command in parens, works :
cmd prompt > FOR /f %a in ('echo 5') DO ( PowerShell $x = %a ; $x )
5
DO command in parens, fails :
cmd prompt > FOR /f %a in ('echo 5') DO ( PowerShell $x = %a ; IF ($x -gt 1) { $x } )
{ was unexpected at this time.
Same DO command NOT in parens, works :
cmd prompt > FOR /f %a in ('echo 5') DO PowerShell $x = %a ; IF ($x -gt 1) { $x }
5
Explanation ?
Last edited by be (03 Dec 2018 20:23)
----------------------------
#7 03 Dec 2018 21:43
Aacini
----------------------------
#8 03 Dec 2018 22:15
be
Thanks, that's it alright. But darn,
Cmd should recognize nested parens pairs.
----------------------------
#9 03 Dec 2018 22:50
Aacini
be
Code: Select all
:: In cmd script, this line results in display of the text
PowerShell "54321"
:: In PS console, these twp lines
:: result in creation of var and display of its content
$x = "54321"
$x
:: In cmd script, these two lines result in nothing.
PowerShell $x = "54321"
PowerShell $x
:: The var is not created. No error.
#2 29 Nov 2018 00:38
npocmaka
try like this:
Code: Select all
powershell "$x='123';$x"
Code: Select all
powershell "$x="""123""";$x"
Code: Select all
<# : batch portion
@echo off & setlocal
set "arg=%~1"
powershell -noprofile "iex (${%~f0} | out-string)"
goto :EOF
: end batch / begin powershell #>
# display %arg%
$env:arg
$x="123"
$x
#3 30 Nov 2018 20:37
Simon Sheppard
npocmaka has given you some solutions, but to explain - these 2 lines:
Are creating two separate instances of Powershell.be wrote:
PowerShell $x = "54321"
PowerShell $x
To run the commands in a single process, you need to pass a single script or command to Powershell.
See this page for powershell.exe
https://ss64.com/ps/powershell.html
----------------------------
#4 01 Dec 2018 03:52
be
Good.npocmaka wrote:
powershell "$x='123';$x"
It demonstrates that the var is created.
Given that the var it is gone after the line, I continued the line with
a long series of commands
in order to have the var for what I wanted to do with it.
That is very, very clever. And so useful.npocmaka wrote:
powershell and batch code in one file:
<# : batch portion
@echo off & setlocal
set "arg=%~1"
powershell -noprofile "iex (${%~f0} | out-string)"
goto :EOF
: end batch / begin powershell #>
# display %arg%
$env:arg
$x="123"
$x
I much appreciate that you added it into your reply.
----------------------------
#5 01 Dec 2018 03:55
be
Thanks.Simon Sheppard wrote:
separate instances of Powershell.
----------------------------
#6 03 Dec 2018 20:22
be
(This occurs also in script.)
DO command in parens, works :
cmd prompt > FOR /f %a in ('echo 5') DO ( PowerShell $x = %a ; $x )
5
DO command in parens, fails :
cmd prompt > FOR /f %a in ('echo 5') DO ( PowerShell $x = %a ; IF ($x -gt 1) { $x } )
{ was unexpected at this time.
Same DO command NOT in parens, works :
cmd prompt > FOR /f %a in ('echo 5') DO PowerShell $x = %a ; IF ($x -gt 1) { $x }
5
Explanation ?
Last edited by be (03 Dec 2018 20:23)
----------------------------
#7 03 Dec 2018 21:43
Aacini
Code: Select all
FOR /f %a in ('echo 5') DO ( PowerShell $x = %a ; IF ($x -gt 1) { $x } )
this ^ open paren for CMD is closed by: ^
You must escape such right paren, so CMD don't "see" it:
FOR /f %a in ('echo 5') DO ( PowerShell $x = %a ; IF ($x -gt 1^) { $x } )
#8 03 Dec 2018 22:15
be
Thanks, that's it alright. But darn,
Cmd should recognize nested parens pairs.
----------------------------
#9 03 Dec 2018 22:50
Aacini
Really? Should it also sintactically parse each command in order to see if there is an unpaired right paren that don't matters? neutralbe wrote:
Cmd should recognize nested parens pairs.
Code: Select all
for %%a in (A B C) do (
echo Turn %%a with right paren ^)
)