0

I've a sample code like this:

[string] $Text = "This is a string text with some $($variable.Option) and $otherOption and that's it". 

Now what I would like to know is it possible to split that $Text into standard strings and variables separately? So that when I pass this $Text variable to a method it's able to extract $($variable.Option) separately?

I know it's a long shot but maybe it's not being processed right away at assignment time?

The ultimate goal is to create a better version of method I wrote for making colorful powershell:

function Write-Color([String[]]$Text, [ConsoleColor[]]$Color = "White", [int]$StartTab = 0, [int] $LinesBefore = 0,[int] $LinesAfter = 0) {
    $DefaultColor = $Color[0]
    if ($LinesBefore -ne 0) {  for ($i = 0; $i -lt $LinesBefore; $i++) { Write-Host "`n" -NoNewline } } # Add empty line before
    if ($StartTab -ne 0) {  for ($i = 0; $i -lt $StartTab; $i++) { Write-Host "`t" -NoNewLine } }  # Add TABS before text
    if ($Color.Count -ge $Text.Count) {
        for ($i = 0; $i -lt $Text.Length; $i++) { Write-Host $Text[$i] -ForegroundColor $Color[$i] -NoNewLine } 
    } else {
        for ($i = 0; $i -lt $Color.Length ; $i++) { Write-Host $Text[$i] -ForegroundColor $Color[$i] -NoNewLine }
        for ($i = $Color.Length; $i -lt $Text.Length; $i++) { Write-Host $Text[$i] -ForegroundColor $DefaultColor -NoNewLine }
    }
    Write-Host
    if ($LinesAfter -ne 0) {  for ($i = 0; $i -lt $LinesAfter; $i++) { Write-Host "`n" } }  # Add empty line after
}

Usually I can assign colors by doing something like

write-color -Text "[View][$($singleView.Title)]", 
                  "[Row Limit: $($singleView.RowLimit)]",
                  "[Paged: $($singleView.Paged)]",
                  "[Default View: $($singleView.DefaultView)]",
                  "[Style ID: $($singleView.StyleID)]" -Color Yellow, Green, Red, Gray, Green

But this means I get colors for the whole "line". If i would like to get normal text colors in one color and variables being in second color I would have to do something like this:

write-color -Text "[View: ", "$($singleView.Title)", "]",
                  "[Row Limit: ", "$($singleView.RowLimit)", "]" `
            -Color Yellow, Green, Yellow, Yellow, Green, Yellow

It's not bad.. but I just thought if this can be accomplished in a better way where simple text is one color and variables are the 2nd. If I would want to go even further and have $true being in Green and False being in Red that would also require some parsing.

10
  • 1
    I don't think so. As far as I'm aware, the string is processed and parsed immediately, all embedded variables are resolved, and then the resulting string is what's stored in $Text. $Text has no knowledge about the variables used to define it. Commented Apr 12, 2016 at 12:46
  • 1
    Can you tell us more what you want to implement? This sounds very awkward to me. Commented Apr 12, 2016 at 12:50
  • In case this is an XY Problem do you have a concreate example of how you would use this information if it was possible (which is not because of what Bacon bits said. ). OH....... you are asking for delayed string expansion..... Commented Apr 12, 2016 at 12:57
  • I think you would find using the format operator easier. $Text = "This is a string text with some {0} and {1} and that's it".. You can pass that to a funciont and then invoke it with $string -f $variable.option, $variable2 Commented Apr 12, 2016 at 13:01
  • 1
    If you are just trying to change colour on the fly I would recommend my answer to this question: stackoverflow.com/a/36429374/3829407 Commented Apr 12, 2016 at 13:13

1 Answer 1

1

Variables will expand inside double quotes. Once that is done there is no history to speak of.

You have one of two options here. You can use the format operator to send in string with placeholders for your variables.

Use the format operator

ss64 on Using the format operator

# Create a formatted string with variable placeholders
$Text = "This is a string text with some {0} and {1} and that's it"

# Pass that string into a function and put in the variables 
$stringParameterinFunction -f $variable.Option, $otherOption

String Expansion

If you really wanted what I think you are asking for then you can delay the string expansion by use single quotes on the original string. Note the single quote inside the string was escaped.

$Text = 'This is a string text with some $($variable.Option) and $otherOption and that''s it.'

# Pass that string into a function and put in the variables
$ExecutionContext.InvokeCommand.ExpandString($stringParameterinFunction) 
Sign up to request clarification or add additional context in comments.

Comments

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.