0

I want to use PowerShell with HTML content which uses some AngularJS var like $scope, $window, or $sce.

One solution is to escape all $ occurrences only for some variables, but not for PowerShell variables as the solution given in Powershell cannot expand this javascript content.

Another solution would be to customize the $ string interpolation symbol in PowerShell: is it possible?

I searched, but can't seem to find it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user310291
  • 36,946
  • 82
  • 271
  • 487

2 Answers2

4

Use a different symbol for PowerShell variables, for example, surround them with %:

<script type="text/javascript">
    %var1%
    $jsvar
    $('.mylink').click(function(event) {
        var hash = $(this).attr("href");
    });
    %var2%
</script>

So it'll be easy to escape all JavaScript's $ and then convert % to PowerShell's $:

$ExecutionContext.InvokeCommand.ExpandString((
     $text -replace '\$', '`$' -replace '%(\w+)%', '$$$1'))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • When I try with app.controller('Ctrl', function($scope, $window, $sce) {} I get an empty output file ? – user310291 Mar 03 '17 at 16:19
  • $template = get-content -raw -Encoding utf8 test.html $Template = $Template | ForEach-Object { $ExecutionContext.InvokeCommand.ExpandString(($text -replace '\$', '`$' -replace '%(\w+)%', '$$$1')) } $Template | Out-File out.html -Encoding UTF8 – user310291 Mar 03 '17 at 16:20
  • Because you're not using the actual file contents in `foreach`: replace `$text` with `$_` – wOxxOm Mar 03 '17 at 16:22
  • Oops thanks it's a long I didn't do any powershell script I didn't remember $_ :) – user310291 Mar 03 '17 at 16:30
2

Another solution would be to customize the $ string interpolation symbol in Powershell

I am not aware of that being possible but you might not need to go that route anyway.

Not sure if this is a feasible solution for you but one option you could do is allow PowerShell to expand all of the strings it finds anyway. However what you would do before that is make some variables beforehand that would place in the text of the variable name itself.

$inputString = @'
There is some $data here.
It looks like we are getting $real $crazy.
Look at the colour $colour to see what is going on. 
'@

There are 4 potential variables in that here-string. It is single quoted so no expansion is taking place yet. We create a collection of "variables" that we do not want expanded. As well as the one variable we want expanded. I am choosing to replace $colour

$skip = "data","real","crazy"
$colour = "Green"

Then we create the variables for each string in $skip where the data is the variable name itself with a dollar sign.

$skip | ForEach-Object{New-Variable -Name $_ -Value "`$$_"}

So now when we do variable interpolation all variables are affected but we controlled the outcome of the ones that should not have their values changed.

$ExecutionContext.InvokeCommand.ExpandString($inputString)
There is some $data here.
It looks like we are getting $real $crazy.
Look at the colour Green to see what is going on.

So all of the string were expanded but we placed in the same text for data, real and crazy and the $colour was allowed to change. I had a look at your other question and this does not really help for subexpressions beyond what the answer there already tells you.

Be aware that $ExecutionContext.InvokeCommand.ExpandString can be dangerous if you do not control or trust the data being inserted and the source string itself. There is potential malicious behavior there.

Matt
  • 45,022
  • 8
  • 78
  • 119
  • seems a good solution I chose the first one just because it's the quicker one to implement but will try yours after – user310291 Mar 03 '17 at 16:10
  • Mine is answered based on _this_ question. It does not really apply to what you are actually asking which is content on another question. If that was included I would not have answered this way. I hope this is useful to others which is why I keep it here. – Matt Mar 03 '17 at 16:17