Parse string content on the fly in PowerShell
The first example below is a regular static string that is processed. The second example is trying to do the same, but make the string parse live, dynamically. I need to know what to put instead of (($myparse gets evaluated))
below to make it parse content $myparse
on the fly. I'm sure this is some kind of script block, but I can't figure out which type.
The following code correctly parses the static string as "Hello John Smith" and saves it to $mysalutation
:
>$firstName = "John"
>$lastName = "Smith"
>$mysalutation = "Hello $firstName$(if($lastname) {" " + $lastName})."
>$mysalutation
Hello John Smith.
What I want to do is parse the same line on the fly:
>$myparse = 'Hello $firstName$(if($lastname) {" " + $lastName}).'
>$myparse
Hello $firstName$(if($lastname) {" " + $lastName}).
>$firstName = "Jason"
>$lastName = "Bourne"
>$mysalutation = (($myparse gets evaluated))
>$mysalutation
Hello Jason Bourne.
+3
source to share