【发布时间】:2017-07-13 04:12:17
【问题描述】:
在 Windows 命令提示符中,特殊文件夹的解析方式如下:
但是,在 powershell 中,这些文件夹似乎没有被解析:
考虑字符串:
$myfile = "%temp%\\myfolder\\myfile.txt"
如何将其用作 PowerShell 函数的参数(例如:Remove-Item),并让 PowerShell 正确解析特殊文件夹,而不是按字面意思理解并将当前工作目录放在前面?
编辑:
我正在使用来自外部配置文件的标准 Windows 路径表示法处理字符串,例如:
config.json:
{
"some_file": "%TEMP%\\folder\\file.txt"
}
myscript.ps1:
$config = Get-Content -Raw -Path "config.json" | ConvertFrom-Json
Remove-Item -path $config.some_file -Force
注意:由于任何Windows special folders 都可以出现在这些字符串中,我宁愿避免像这样可怕的查找替换黑客攻击
$config.some_file = $config.some_file -replace '%TEMP%' $env:temp
【问题讨论】:
-
好吧,你可以创建自己的函数来解析它们,并将它们转换为值,我认为你不能做任何其他事情(除了你的 hack)
-
-替换 '%(.*)%', '$env:$1'
标签: shell powershell command-line path