Dynamic string parsing in Powershell -
i'm trying unusual powershell parsing. basically, have literal string contains name of variable. tell powershell "hey, i've got string (may) contain 1 or more variable names--dynamically parse it, replacing variable names values"
here's textbook way understand parsing works:
ps c:\> $foo = "rock on" ps c:\> $bar = "$foo" ps c:\> $bar rock on
and if change value $foo:
ps c:\> $foo = "rock off" ps c:\> $bar rock on
no surprises here. value of $bar parsed assigned, , didn't change because value of $foo changed.
ok, if assign $bar single quote?
ps c:\> $foo = "rock on" ps c:\> $bar = '$foo' ps c:\> $bar $foo
that's great, there way powershell parse on demand? example:
ps c:\> $foo = "rock on" ps c:\> $bar = '$foo' ps c:\> $bar $foo ps c:\> some-parsefunction $bar rock on ps c:\> $foo = "rock off" ps c:\> some-parsefunction $bar rock off
why want this? able content file (or data source) , dynamically parse it:
ps c:\> $foo = "rock on" ps c:\> '$foo bad self.' | out-file message.txt ps c:\> $bar = (get-content message.txt) ps c:\> $bar $foo bad self. ps c:\> some-parsefunction $bar rock on bad self.
can done? realize pattern matching search/replace known names, i'd rather have powershell reparse string.
thanks!
$executioncontext.invokecommand.expandstring($bar)
Comments
Post a Comment