[英]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:我想要实现的是以下内容:
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+ 也接受""
)。
'...'
(single-quoting) inside the "..."
string passed to -Command
/ -c
, assuming that no string interpolation is required.-Command
/ -c
的"..."
字符串中使用'...'
(单引号),假设不需要字符串插值。 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 ,指示最近执行的管道中的命令是否发出任何错误。
-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
和$true
到1
。 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.