使用PowerShell脚本更新多个配置文件

使用PowerShell脚本更新多个配置文件,powershell,Powershell,任务是使用power shell脚本更新web.config和app.config中的应用程序设置。经过一些搜索,我找到了一些脚本来更新单个文件,但不能更新多个文件。有人能帮忙吗 $Config = C:\inetpub\wwwroot\TestService\Web.config $doc = (Get-Content $Config) -as [Xml] $obj = $doc.configuration.appSettings.add | where {$_.Key -eq 'SCVMMS

任务是使用power shell脚本更新web.config和app.config中的应用程序设置。经过一些搜索,我找到了一些脚本来更新单个文件,但不能更新多个文件。有人能帮忙吗

$Config = C:\inetpub\wwwroot\TestService\Web.config
$doc = (Get-Content $Config) -as [Xml]
$obj = $doc.configuration.appSettings.add | where {$_.Key -eq 'SCVMMServerName'}
$obj.value = CPVMM02
$doc.Save($Config)

我可以给你一个合乎逻辑的出发点。您可以使用选择字符串中的-match获取要更新的行,然后同样地,您可以使用-notmatch选择文件中已存在的剩余内容。 把它们放在变量中。更新行,将其存储回变量中

然后使用设置内容


希望您对如何接近有所了解有很多方法,例如:

"C:\inetpub\wwwroot\TestService\Web.config",
"C:\inetpub\wwwroot\TestService\App.config" |
    ForEach-Object {
        $doc = (Get-Content $_) -as [Xml]
        $obj = $doc.configuration.appSettings.add |
            Where-Object { $_.Key -eq 'SCVMMServerName' }
        $obj.value = CPVMM02
        $doc.Save($_)
    }

所以你想得到ChildItem和for循环?我不确定。但这可能会有所帮助。你能分享一下怎么做吗?$fileNames=Get ChildItem-Path“C:\TestService”-Recurse-Include*.config foreach($fileNames中的文件){$doc=(Get Content$file)-作为[Xml]If($doc-match'key1'){$obj1=$doc.configuration.appSettings.add |其中{$\uj1.Key-eq'key1'}$obj1.value='true'$doc Save($file)}这不起作用$fileNames=Get ChildItem-Path“C:\TestService”-Recurse-Include*.config foreach($fileNames中的文件){$doc=(Get Content$file)-作为[Xml]If($doc-match'key1'){$obj1=$doc.configuration.appSettings.add |其中{$$\ Key-eq'key1'}$obj1.value='true'$doc Save($file)}这并没有让我经历同样的解决方案对我有效。我使用了contains,并且我的密钥是“SCVMMServerName”,所以它工作正常
$fileNames=Get ChildItem-Path“C:\TestService”-Recurse-包括“*.config”foreach($fileNames中的文件){$doc=(Get Content$file)-作为[Xml]If($doc-包含“SCVMMServerName”){$obj1=$doc.configuration.appSettings.add |其中{$$\uke.Key-包含“key1”}$obj1.value='true'$doc Save($file)}
。索达维洛的解决方案也是有效的,对我来说也是有效的。非常感谢。但是我如何在kudu调试控制台中执行此脚本来更新web.config?@Sridhar:我没有使用kudu。我不确定。如何在kudu调试控制台中执行此脚本以更新web.config?