简体   繁体   English

使用 Tee 从另一个 shell 运行 PowerShell

[英]Running PowerShell from another shell with Tee

I'd like to learn to execute a PowerShell command from another shell or language, eg Python os.system() .我想学习从另一个 shell 或语言(例如 Python os.system()执行 PowerShell 命令。 What I want to achieve is the following:我想要实现的是以下内容:

  1. Execute the PowerShell command执行 PowerShell 命令
  2. Tee the output to both the console and a file将 output 连接到控制台和文件
  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:我认为这给出了我想要实现的想法,假设使用 cmd.exe 作为调用者环境:

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, eg :首先,我不能在命令中使用引号,例如:

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. cat参数似乎是不带引号传递的,因此cat会查找“bar.txt”参数。

I think $LASTEXITCODE is expanded soon, that is before cat is executed.我认为$LASTEXITCODE很快就会扩展,即在执行cat之前。

& is inconvenient to use, because it does not accept a single command line string including arguments. &使用不方便,因为它不接受包括 arguments 在内的单个命令行字符串。 An alternative to & is iex , however I cannot use it from cmd.exe. &的替代方法是iex ,但是我不能从 cmd.exe 使用它。 In fact:实际上:

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

returns:返回:

iex cat foo.txt

From cmd.exe , use the following ( -c is short for -Command ):cmd.exe中,使用以下命令( -c-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.没有理由在传递给-Command的字符串中使用& {... } - 只需使用...代替。

  • Escape embedded " chars. as \" (PowerShell (Core) 7+ also accepts "" ).嵌入" chars. as \"转义(PowerShell (Core) 7+ 也接受"" )。

  • 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 .由于该命令仅涉及PowerShell 原生命令(在 Windows 上, cat只是Get-Content的别名),因此设置$LASTEXITCODE ,因为它仅反映外部程序的退出代码。 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. 变量适用,它是一个Boolean ,指示最近执行的管道中的命令是否发出任何错误。

    • 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 .-not否定这个值意味着$true转换为$false$false转换为$true ,并且这些值转换为外部整数$false映射到0$true1

Powershell supports single quotes, which saved me in such situations quite a lot of times. Powershell 支持单引号,这在这种情况下为我节省了很多次。 The good thing about it: They are unambiguous and easy to read.这样做的好处是:它们明确且易于阅读。 But mind that variable expansion won't work inside single-quoted strings.但请注意,变量扩展在单引号字符串中不起作用。

powershell -NoProfile -command "cat 'foo bar.txt' | tee ps-log.txt"

Apart from that, have a look at the useful advice in mklement0's answer.除此之外,请查看 mklement0 的答案中的有用建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Tee-Object Powershell去除颜色 - Remove Color From Tee-Object Powershell 2>&1 的正确用法| 三通或| 带 Powershell 的 T 恤 - Right usage of 2>&1 | tee or | tee with powershell 是否可以从 C# 运行 PowerShell shell 并使其保持运行,同时(同步)在其中执行多个 PowerShell 脚本? - Is it possible to run the PowerShell shell from C# and keep it running, while (synchronously) executing multiple PowerShell scripts within it? PowerShell ISE:调试从power-shell脚本调用的另一个power-shell脚本 - PowerShell ISE: Debug another power-shell script called from a power-shell script 从Powershell脚本运行多个cmd命令,就像我在cmd shell中运行它一样 - Running multiple cmd commands from powershell script as if I was running it in cmd shell PHP Shell_exec - Powershell未运行 - PHP Shell_exec - Powershell not running PowerShell相当于bash`exec >>(tee -a $ logfile); exec 2 >>(tee -a $ logfile>&2)` - PowerShell equivalent to bash `exec > >(tee -a $logfile); exec 2> >(tee -a $logfile >&2)` 在另一个 Powershell 脚本中运行 Powershell 脚本 - Running a Powershell script inside another Powershell script PowerShell 控制台 output 来自 IF 语句内 function 内的 Tee-Object 命令 - PowerShell console output from Tee-Object command inside function inside IF statement 以另一个用户身份运行 powershell 命令 - Running powershell commands as another user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM