Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running PowerShell from another shell with Tee

I'd like to learn to execute a PowerShell command from another shell or language, e.g. Python os.system(). What I want to achieve is the following:

  1. Execute the PowerShell command
  2. Tee the output to both the console and a file
  3. Return the command exit code

I think this gives an idea of what I would like to achieve, assuming to use cmd.exe as the caller environmnet:

powershell -NoProfile -command "& { cat foo.txt  | Tee-Object ps-log.txt; exit $LASTEXITCODE }"
echo %errorlevel%

There are some problems here. First, I cannot use quotations in the command, e.g. :

powershell -NoProfile -command "& { cat `"foo bar.txt`"  | Tee-Object ps-log.txt; exit $LASTEXITCODE }"

The cat argument seems to be passed unquoted and so cat looks for a 'bar.txt' parameter.

I think $LASTEXITCODE is expanded soon, that is before cat is executed.

& is inconvenient to use, because it does not accept a single command line string including arguments. An alternative to & is iex, however I cannot use it from cmd.exe. In fact:

powershell  -NoProfile -command  {iex cat  foo.txt}

returns:

iex cat foo.txt
like image 902
antonio Avatar asked Jan 25 '23 09:01

antonio


1 Answers

From cmd.exe, use the following (-c is short for -Command):

C:\>powershell -NoProfile -c "Get-Content \"foo bar.txt\" | Tee-Object ps-log.txt; exit -not $?"
  • There's no reason to use & { ... } in a string passed to -Command - just use ... instead.

  • Escape embedded " chars. as \" (PowerShell (Core) 7+ also accepts "").

    • Alternatively, as marsze's helpful answer points out, you can use '...' (single-quoting) inside the "..." string passed to -Command / -c, assuming that no string interpolation is required.
  • Since only PowerShell-native commands are involved in the command (on Windows, cat is simply an alias of Get-Content), $LASTEXITCODE is not set, as it only reflects the exit code of external programs. Instead, the automatic $? variable applies, which is a Boolean that indicates whether any errors were emitted by the commands in the most recently executed pipeline.

    • Negating this value with -not means that $true is converted to $false and $false to $true, and these values are converted to integers for the outside, with $false mapping to 0 and $true to 1.
like image 128
mklement0 Avatar answered Jan 29 '23 09:01

mklement0