26

My file a.txt contains:

delete from test_$suffix

My PowerShell is:

$a = get-content a.txt
$suffix = "tableA"

How would I manipulate $a to obtain the string delete from test_tableA ?

3 Answers 3

41

Invoke-Expression is the equivalent.

$strExpression = "5 + 5 -eq 10"
Invoke-Expression $strExpression
True

See this blog post for more information.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for that, accepted answer did not worked for me, it just echoed variable contents. Your answer works well and even shorter
The accepted answer answers the OP's question of doing a "format string". This answers the title of the question of how to eval a string. If you just want to do "format string", the accepted answer is safer as it will not run arbitrary code (in case the input file is malicious)
@AmbroseLeung, good contrast, but note that $ExecutionContext.InvokeCommand.ExpandString() is not inherently safer, because arbitrary commands can be embedded in an expandable string via $(...); e.g., $ExecutionContext.InvokeCommand.ExpandString('$(Get-Date)')
@mklement0 thanks for that - you are correct. I thought that if you read $(...) from a file, powershell would escape the $, but this is not the case. Perhaps sanitizing user input (replace $ with ``$) ...ExpandString((gc myinput.txt).Replace('$','`$')) - there's probably some way to bypass this as well? Best to avoid expanding user input
@AmbroseLeung: Given that expanding variable references in the string (e.g, $suffix) is the very point of using $ExecutionContext.InvokeCommand.ExpandString() in the case at hand, the challenge is to selectively ignore embedded $(...) subexpressions. Doing this robustly, without getting tricked by escaping embedded in the source is somewhat tricky: $a = $a -replace '(?<!`)((?:``)*)\$\(', '$1`$$('
40
$a=get-content a.txt
$suffix="tableA"

$ExecutionContext.InvokeCommand.ExpandString($a)

7 Comments

Just be aware that if $a contains a quoted string (e.g. $a = 'delete from "test_$suffix"') you're going to lose your quotes when it gets expanded.
That is true. Same goes for $ signs. You can escape them with a tick `. Also, the here string technique only allows lines of recognizable PowerShell. I cannot have 'This is text' in the file (without the quotes). Enabling that is useful when doing templating type applications.
I don't understand this part: "Also, the here string technique only allows lines of recognizable PowerShell. I cannot have 'This is text' in the file (without the quotes). Enabling that is useful when doing templating type applications." mjolinor.wordpress.com/2011/02/02/here-string-test
My apologies, the testfile.txt I tested against was saved from ISE as Unicode, I re-saved it as Ascii and it worked.
No problem. I've had times when I've thought I understood things better than I did, and made statements were just flat wrong. I hate when I do that, and wanted to be sure this wasn't one of those times.
|
3

Here's one way. Variables in a double-quoted here-string get substituted automatically. Just be sure your input file conforms to the PS rules for here-strings.

 function convertto-herestring { 
 begin {$temp_h_string = '@"' + "`n"} 
 process {$temp_h_string += $_ + "`n"} 
 end { 
     $temp_h_string += '"@' 
     iex $temp_h_string 
     } 
 } 

 $suffix = "tableA"

 get-content testfile.txt

 delete from test_$suffix

 get-content testfile.txt | convertto-herestring

 delete from test_tableA

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.