1

I am struggling on this for hours now ...

Its a syntax question basically how do I make this work : $test = '{"Title": $name}' The $name is not recognize as a variable. this is a simple example the real line is here and the unrecognize var is the $li at the end:

Add-PnPPageWebPart -Page "Home.aspx" -DefaultWebPartType ContentRollup  -Section 3 -Column $counter -WebPartProperties '{"query": {"contentLocation": 4,"contentTypes": [1],"sortType": 1,"filters": [{"filterType": 1,"value": "","values": []}],"documentTypes": [1,2,3,10],"advancedQueryText": ""}, "listTitle": $li}'

Thanks to those who will help :)

mklement0
  • 382,024
  • 64
  • 607
  • 775
Ricola
  • 51
  • 4
  • Yes, switch the quotes to double quotes. Interpolation doesn't occur in single quotes. So either switch the quotes, or escape the double quotes `"""{...}"""`. – Abraham Zinala Aug 22 '22 at 15:37
  • 1
    In short: In PowerShell, only `"..."` strings (double-quoted aka _expandable strings_) perform string interpolation (expansion of variable values and expressions), not `'...'` strings (single-quoted aka _verbatim strings_). With `"..."` quoting, if the string value itself contains `"` chars., escape them as `\`"` or `""`, or use a double-quoted _here-string_. See the conceptual [about_Quoting_Rules](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Quoting_Rules) help topics. – mklement0 Aug 22 '22 at 15:57

1 Answers1

1

Use a double-quoted here-string:

$test = @"
{"Title": $name}
"@

Or use a regular double-quoted string, and then escape the literal "'s, by either doubling them:

$test = "{""Title"": $name}"

... or by using a backtick `:

$test = "{`"Title`": $name}"
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206