Powershell 中间文件编辑文件

Powershell 中间文件编辑文件,powershell,Powershell,我想创建一个powershell脚本,该脚本将编辑配置文件apache httpd.conf,如果我发现 #keyword some data #keyword 我在关键字之间替换数据,否则我用数据添加关键字 有人能给我一些帮助吗 多谢各位 $fileName = "..\..\apache\conf\extra\httpd-vhosts.conf" $found = $false $contains = $false $kw = "##xceed.localhost" $text = "

我想创建一个powershell脚本,该脚本将编辑配置文件apache httpd.conf,如果我发现

 #keyword
some data
 #keyword
我在关键字之间替换数据,否则我用数据添加关键字

有人能给我一些帮助吗 多谢各位

$fileName = "..\..\apache\conf\extra\httpd-vhosts.conf"
$found = $false
$contains = $false
$kw = "##xceed.localhost"
$text = "A
BBBB
C"
(Get-Content ( $fileName )) | 
    Foreach-Object{ 
            if($_ -match $kw)
            {
            if($found -eq $false)
            {
                $found=$true
            }else
            {
                $found=$false
            }

            if($found -eq $true)
            {
                $contains=$true
                        #Add Lines after the selected pattern
                $kw
                        $text 
            }       
            }
        if($found -ne $true)
        {
            $_
        }

    } | Set-Content( $fileName )

if($contains -eq $false)
{
$value=($kw+"`r`n"+$text+"`r`n"+$kw)
    Add-Content $fileName $value
}