powershell: how to evaluate a string read from a f

2019-02-08 04:31发布

file a.txt is:

delete from test_$suffix

$a=get-content a.txt

$suffix="tableA"

how to manipulate the variable to set it as

delete from test_tableA

3条回答
小情绪 Triste *
2楼-- · 2019-02-08 05:01

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
查看更多
来,给爷笑一个
3楼-- · 2019-02-08 05:06

Invoke-Expression is the equivalent.

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

See http://technet.microsoft.com/en-us/library/ee176880.aspx for more information.

查看更多
地球回转人心会变
4楼-- · 2019-02-08 05:14
$a=get-content a.txt
$suffix="tableA"

$ExecutionContext.InvokeCommand.ExpandString($a)
查看更多
登录 后发表回答