我有一个.msha文件。以下是文件的内容。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Class References</title>
</head>
<body class="vendor-book">
<div class="details">
<span class="vendor">Pearson</span>
<span class="locale">en-us</span>
<span class="product">Class References</span>
<span class="name">Class References</span>
</div>
<div class="package-list">
<div class="package">
<!-- NOTE: The "name" span value cannot contain any periods! -->
<span class="name">ClassReference</span>
<a class="current-link" href="ClassReference.mshc">ClassReference.mshc</a>
</div>
</div>
</body>
</html>
我必须替换以下标记
<a class="current-link" href="ClassReference.mshc">ClassReference.mshc</a>
如下
<a class="current-link" href="ClassReference.mshc.cab">ClassReference.mshc</a>
如何使用PowerShell做到这一点?
答案 0 :(得分:2)
试试这个:
${C:\test\test.msha} = ${C:\test\test.msha} -replace 'href="ClassReference\.mshc"','href="ClassReference.mshc.cab"'
答案 1 :(得分:1)
最简单的方法是将文件读取为XML并替换标记。仅当标记始终采用相同格式时,以下内容才有效。
$xml = [xml](Get-Content C:\script\test.hta)
$xml.html.body.div[1].div.a.href = "ClassReference.mshc.cab"
$xml.html.body.div[1].div.a.href
$xml.InnerXml.ToString() | Out-File .\test.hta
HTH
答案 2 :(得分:0)
您可以通过将文件的内容作为文本阅读并执行简单的搜索&amp;使用PowerShell中的-replace
string operator替换:
(Get-Content MyFile.hta) -replace "href=`"ClassReference.mshc`"", "href=`"ClassReference.mshc.cab`"" `
| Set-Content MyFile.hta