【发布时间】:2013-07-19 19:28:54
【问题描述】:
在 PowerShell 3 中,我正在对字符串执行正则表达式替换,我想将 '$1' 位置参数作为变量传递给函数(参见 script.ps1 中的最后一行)
我的代码
testFile.html
<%%head.html%%>
<body></body></html>
head.html
<!DOCTYPE html>
<html><head><title></title></head>
script.ps1
$r = [regex]"<\%\%([\w.-_]+)\%\%>" # matches my markup e.g. <%%head.html%%>
$fileContent = Get-Content "testFile.html" | Out-String # test file in which the markup should be replaced
function incl ($fileName) {
Get-Content $fileName | Out-String
}
$r.Replace($fileContent, (incl '$1'));
问题出在 script.ps1 的最后一行,即我找不到解决函数调用的方法,以便 Get-Content 从 $fileName 获取正确的值。它从错误消息中看到它是“$1”:
Get-Content : Cannot find path 'C:\path\to\my\dir\$1' because it does not exist.
我想要'C:\path\to\my\dir\head.html'
基本上,我想用这个脚本实现的是,它可以将其他静态 HTML 页面合并到我的页面中,无论我使用 标记。
有什么想法吗?谢谢。
【问题讨论】:
标签: powershell scripting