Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with v2 variable substitution - single-quoted strings get $var references replaced #65

Closed
ALuckyGuy opened this issue Apr 6, 2016 · 5 comments

Comments

@ALuckyGuy
Copy link

Try the following running PowerShell -version 2:

1..2|start-rsjob -Name {$_} -ScriptBlock {
        $var = 'this value'
        new-object psobject -Property @{
            Result=($_)
            Test='Confirming $var is not substituted'
            Something="Confirming $var is substituted"
        }
}            
Get-RSjob | Receive-RSJob

v2 produces this result:

Result Something                            Test                                    
------ ---------                            ----                                    
     1 Confirming this value is substituted Confirming this value is not substituted
     2 Confirming this value is substituted Confirming this value is not substituted

v3 & later produces correct results:

Result Something                            Test                                    
------ ---------                            ----                                    
     1 Confirming this value is substituted Confirming $var is not substituted
     2 Confirming this value is substituted Confirming $var is not substituted
@ALuckyGuy
Copy link
Author

I believe I have a fix for this. Might be a more elegant way to do it, but it works.

The problem is in the ConvertScriptBlockV2 function and the fact that it always added strings to the built script using double-quotes. Instead the string needs to be re-added to the script using the same quoting as the source.

This code:

        'String' {
            [void]$StringBuilder.Append(("`"{0}`"" -f $Tokens[$i].Content))
        }

can be changed to:

        'String' {
            $qchar = $ScriptBlock.ToString().Split("`n")[($Tokens[$i].StartLine-1)].Substring($Tokens[$i].StartColumn-1,1)
            [void]$StringBuilder.Append(("{0}{1}{0}" -f $qchar,$Tokens[$i].Content))
        }

This fixes the problem I demonstrated above but I don't really have experience with the parser and haven't tested with "here" strings.

@proxb
Copy link
Owner

proxb commented Apr 7, 2016

Great! I'll incorporate that and give it a run to test it. Hopefully I can include this as well as a couple of other things in an update later this week/weekend.

@proxb proxb self-assigned this Apr 7, 2016
@proxb proxb added this to the 1.5.6.2 milestone Apr 8, 2016
@proxb
Copy link
Owner

proxb commented Apr 8, 2016

I tested the fix action and it seems to work. I'll include it in the next update.

@proxb proxb added the Ready label Apr 8, 2016
@proxb proxb modified the milestones: 1.5.6.3, 1.5.6.2 Apr 8, 2016
@proxb
Copy link
Owner

proxb commented Apr 9, 2016

This one should be fixed in the latest release

@ALuckyGuy
Copy link
Author

Yep, corrected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants