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.

3
  • So you have a string where you want it to expand some variables but not others. Commented Mar 3, 2017 at 14:42
  • @Matt yes that's it Commented Mar 3, 2017 at 14:44
  • How are you planning on expanding the variables? Commented Mar 3, 2017 at 14:46

2 Answers 2

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'))
Sign up to request clarification or add additional context in comments.

4 Comments

When I try with app.controller('Ctrl', function($scope, $window, $sce) {} I get an empty output file ?
$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
Because you're not using the actual file contents in foreach: replace $text with $_
Oops thanks it's a long I didn't do any powershell script I didn't remember $_ :)
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.

2 Comments

seems a good solution I chose the first one just because it's the quicker one to implement but will try yours after
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.