【发布时间】:2021-05-17 22:59:02
【问题描述】:
文件 a.txt 是:
从 test_$suffix 中删除
$a=get-content a.txt
$suffix="tableA"
如何操作变量以将其设置为
从 test_tableA 中删除
【问题讨论】:
标签: powershell eval
文件 a.txt 是:
从 test_$suffix 中删除
$a=get-content a.txt
$suffix="tableA"
如何操作变量以将其设置为
从 test_tableA 中删除
【问题讨论】:
标签: powershell eval
$a=get-content a.txt
$suffix="tableA"
$ExecutionContext.InvokeCommand.ExpandString($a)
【讨论】:
Invoke-Expression 是等价的。
$strExpression = "5 + 5 -eq 10"
Invoke-Expression $strExpression
True
更多信息请参见http://technet.microsoft.com/en-us/library/ee176880.aspx。
【讨论】:
这是一种方法。双引号 here-string 中的变量会自动替换。请确保您的输入文件符合此处字符串的 PS 规则。
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
【讨论】: