string PowerShell Invoke-Expression 在命令字符串中带有与号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12438479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:36:11  来源:igfitidea点击:

PowerShell Invoke-Expression with ampersand in the command string

stringpowershellcommand

提问by Matt Wall

I am trying to pass a variable with a string that contains an ampersand into Invoke-Expression, and it is telling me I have to put it in quotations and pass it as a string.

我正在尝试将一个带有包含符号的字符串的变量传递给 Invoke-Expression,它告诉我必须将它放在引号中并将其作为字符串传递。

I've tried multiple combinations of escaping and using a raw string and a string in a variable with combinations of "" and '' to no avail. How can I do it?

我已经尝试了多种组合的转义和使用原始字符串和变量中的字符串以及 "" 和 '' 的组合无济于事。我该怎么做?

Here's the code:

这是代码:

$streamout_calmedia01 = `
"rtmp://75.126.42.216/livepkgr/calmedialive01?adbe-live-event=liveevent&adbe-record-mode=record"

$streamcmd_calmedia01 = "C:\avconv\usr\bin\avconv.exe 'rtmp://75.126.42.211/transcoder/mobileingest live=1' -f flv -c:v libx264 -r 30 -g 120 -b:v 410000 -c:a aac -ar 22050 -b:a 64000 -strict experimental -y $streamout_calmedia01"

Invoke-Expression "$streamcmd_calmedia01"

I've tried using a `before the ampersand and using a double quotation in front of Invoke-Expression before putting in the variable,

我试过`在 & 号前使用 a并在放入变量之前在 Invoke-Expression 前使用双引号,

I've tried (as is shown) putting quotations around the variable with the -Commandfor Invoke-Expression and also putting '&' and "&" and concatenating the ampersand to the string.

我已经尝试过(如图所示)使用-Commandfor Invoke-Expression在变量周围加上引号,并将 '&' 和 "&" 并将与号连接到字符串。

I need the ampersand in there for Flash Media Server to parse the command out of the stream name and flush prior recorded data before beginning HTTP live streaming chunking.

我需要 Flash Media Server 中的&符号来解析流名称中的命令并在开始 HTTP 实时流分块之前刷新先前记录的数据。

回答by Ansgar Wiechers

The ampersand must be double-quoted insidethe string "&", so you need to escape the inner double quotes

该符号必须用双引号的字符串"&",所以你需要躲避内双引号

$streamout_calmedia01 = "rtmp://...vent`"&`"adbe-record-mode=record"

or put the string in single quotes

或将字符串放在单引号中

$streamout_calmedia01 = 'rtmp://...vent"&"adbe-record-mode=record'

回答by Keith Hill

Change $streamout_calmedia01 to:

将 $streamout_calmedia01 更改为:

$streamout_calmedia01 = "rtmp://75.126.42.216/livepkgr/calmedialive01?adbe-live-event=liveevent```&adbe-record-mode=record"

Then you have to re-assign $streamout_calmedia1 (with the new value of $streamout_calmedia1) and it should work.

然后你必须重新分配 $streamout_calmedia1 (使用 $streamout_calmedia1 的新值),它应该可以工作。

回答by latkin

You don't need to be using Invoke-Expressionat all. Avoiding its use will preclude the issue. Just call the EXE file tool directly:

你根本不需要使用Invoke-Expression。避免使用它会排除这个问题。直接调用EXE文件工具即可:

$streamout_calmedia01 = "rtmp://75.126.42.216/livepkgr/calmedialive01?adbe-live-event=liveevent&adbe-record-mode=record"

C:\avconv\usr\bin\avconv.exe 'rtmp://75.126.42.211/transcoder/mobileingest live=1' -f flv -c:v libx264 -r 30 -g 120 -b:v 410000 -c:a aac -ar 22050 -b:a 64000 -strict experimental -y $streamout_calmedia01

This avoids all of the complications of double-escaping and should do what you are intending.

这避免了双重转义的所有并发症,并且应该按照您的意图进行。