【发布时间】:2020-12-03 02:26:19
【问题描述】:
我在网上找到了这个函数,它根据我从 CSV 文件中提供的值更新 IIS web.config XML 文件。我想防止在执行函数时打印每个 XML 条目修改的输出。我已经尝试了很多次来使用 Out-Null,但我似乎无法找到这个 XML 更新函数的哪个部分导致了输出。
我想删除“为 D:\Inetpub\WWWRoot\test 设置 web.config appSettings”行之后的所有 XML 更新输出,如下所示。输出似乎是函数从 CSV 文件中读取并更新 web.config 文件的键/值对。
为 D:\Inetpub\WWWRoot\test 设置 web.config appSettings #文本 ----- allowFrame TRUE
authenticationType SSO
cacheProviderType 共享
这是我正在使用的功能:
function Set-Webconfig-AppSettings
{
param (
# Physical path for the IIS Endpoint on the machine without the "web.config" part.
# Example: 'D:\inetpub\wwwroot\cmweb510\'
[parameter(mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String] $path,
# web.config key that you want to create or change
[parameter(mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String] $key,
# Value of the key you want to create or change
[parameter(mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String] $value
)
Write-Host "Setting web.config appSettings for $path" -ForegroundColor DarkCyan
$webconfig = Join-Path $path "web.config"
[bool] $found = $false
if (Test-Path $webconfig)
{
$xml = [xml](get-content $webconfig);
$root = $xml.get_DocumentElement();
foreach ($item in $root.appSettings.add)
{
if ($item.key -eq $key)
{
$item.value = $value;
$found = $true;
}
}
if (-not $found)
{
$newElement = $xml.CreateElement("add");
$nameAtt1 = $xml.CreateAttribute("key")
$nameAtt1.psbase.value = $key;
$newElement.SetAttributeNode($nameAtt1);
$nameAtt2 = $xml.CreateAttribute("value");
$nameAtt2.psbase.value = $value;
$newElement.SetAttributeNode($nameAtt2);
$xml.configuration["appSettings"].AppendChild($newElement);
}
$xml.Save($webconfig)
}
else
{
Write-Error -Message "Error: File not found '$webconfig'"
}
}
【问题讨论】:
标签: xml powershell