#1 28 Nov 2018 20:08

be
Member
Registered: 10 Sep 2018
Posts: 8

Want to use some PS command in Cmd script to set a var

::  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.

Offline

#2 29 Nov 2018 00:38

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 430

Re: Want to use some PS command in Cmd script to set a var

try like this:

powershell "$x='123';$x"

or:

powershell "$x="""123""";$x"

you can even combine powershell and batch code in one file (save this as .bat):

<# : 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

Offline

#3 30 Nov 2018 20:37

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 976
Website

Re: Want to use some PS command in Cmd script to set a var

npocmaka has given you some solutions, but to explain - these 2 lines:

be wrote:

    PowerShell $x = "54321"
    PowerShell $x

Are creating two separate instances of Powershell.

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

Offline

#4 01 Dec 2018 03:52

be
Member
Registered: 10 Sep 2018
Posts: 8

Re: Want to use some PS command in Cmd script to set a var

npocmaka wrote:
powershell "$x='123';$x"

Good.
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. 


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

That is very, very clever.  And so useful.
I much appreciate that you added it into your reply.

Offline

#5 01 Dec 2018 03:55

be
Member
Registered: 10 Sep 2018
Posts: 8

Re: Want to use some PS command in Cmd script to set a var

Simon Sheppard wrote:

separate instances of Powershell.

Thanks.

Offline

#6 03 Dec 2018 20:22

be
Member
Registered: 10 Sep 2018
Posts: 8

Re: Want to use some PS command in Cmd script to set a var

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

Offline

#7 03 Dec 2018 21:43

Aacini
Member
Registered: 05 Dec 2012
Posts: 138

Re: Want to use some PS command in Cmd script to set a var

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 } )

Offline

#8 03 Dec 2018 22:15

be
Member
Registered: 10 Sep 2018
Posts: 8

Re: Want to use some PS command in Cmd script to set a var

Thanks, that's it alright.  But darn,
Cmd should recognize nested parens pairs.

Offline

#9 03 Dec 2018 22:50

Aacini
Member
Registered: 05 Dec 2012
Posts: 138

Re: Want to use some PS command in Cmd script to set a var

be wrote:

Cmd should recognize nested parens pairs.

Really? Should it also sintactically parse each command in order to see if there is an unpaired right paren that don't matters? neutral

for %%a in (A B C) do (
   echo Turn %%a  with right paren ^)
)

Offline

Board footer

Powered by FluxBB