9

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.

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,

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

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt Wall
  • 475
  • 2
  • 7
  • 15

3 Answers3

21

The ampersand must be double-quoted inside the 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'
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
1

Change $streamout_calmedia01 to:

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

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • This should have no effect. `&` does not need escaping in double quote strings, and the backtick is ignored/discarded: `"&".Length -> 1` and `"``&".Length -> 1` (Second example should only have one backtick, seems I can't figure out how to put a single backtick in markdown backtick code!) – latkin Sep 15 '12 at 20:46
  • Maybe you meant to use single quotes, or double quotes with a **double** backtick? – latkin Sep 15 '12 at 20:50
  • No it was supposed to have 3 backticks. Not sure what happened there. – Keith Hill Sep 16 '12 at 00:25
0

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
latkin
  • 16,402
  • 1
  • 47
  • 62