簡單的方法:使用正則表達式的文本操作。在你想取消註釋的行中尋找一些獨特的東西。例如:
#Get-Content is in () to read the whole file first so we don't get file in use-error when writing to it later
(Get-Content -Path web.config) -replace '<!--(<sessionState allowCustomSqlDatabase.+?)-->', '$1' | Set-Content -Path web.config
Demo @ Regex101
硬的方式:XML操縱。我已經在這裏發表了第一條評論,但是您可以輕鬆地搜索特定的節點,就像我們上面所做的那樣:
$fullpath = Resolve-Path .\config.xml | % { $_.Path }
$xml = [xml](Get-Content $fullpath)
#Find first comment
$commentnode = $xml.configuration.'system.web'.ChildNodes | Where-Object { $_.NodeType -eq 'Comment' } | Select-Object -First 1
#Create xmlreader for comment-xml
$commentReader = [System.Xml.XmlReader]::Create((New-Object System.IO.StringReader $commentnode.Value))
#Create node from comment
$newnode = $xml.ReadNode($commentReader)
#Replace comment with xmlnode
$xml.configuration.'system.web'.ReplaceChild($newnode, $commentnode) | Out-Null
#Close xmlreader
$commentReader.Close()
#Save xml
$xml.Save($fullpath)
您嘗試過什麼嗎? –