PowerShell XML脚本问题,名称值被覆盖

时间:2013-12-21 03:06:03

标签: xml powershell

我编写了以下脚本来修改监控应用程序的配置文件。

基本上,有一个特定的协议导致.net崩溃并停止服务,所以我们需要能够推送这个脚本来禁用它。但在某些情况下,还有其他协议被排除在它们未在环境中使用但不需要被监控的情况下,因此我试图保留这些排除,但似乎当添加另一个“名称”值时,它覆盖最后一个。

基本上,我希望能够添加

<ScannerExclusion>
  <add name="ZeroConfSweep" />
  <add name="AmtScan" />
</ScannerExclusion>

而是我得到了这个

<ScannerExclusion>
  <add name="ZeroConfSweep" />
</ScannerExclusion>

这是剧本,想知道是否有人能够对出了什么问题有所了解

    # get config file name
    $MWExpertConfigFile = "MWExpertSystem.exe.config"

    # set the full file path
    $MWExpertConfigPath = $OMREGKEY.Path + "Bin\" + $MWExpertConfigFile

    # Load the MWExpertConfig file from the installation directory
    Write-Output "Loading MWExpert Config"
    $MWExpertConfig = [xml](Get-Content $MWExpertConfigPath)

    # Define new element for scanner exclusions
    $ScannerExclusion = $MWExpertConfig.CreateElement("ScannerExclusion")

    # Define an exclusion to add
    $ExclusionAdd = $MWExpertConfig.CreateElement("add")

    # check if any exclusions exist
    if ($MWExpertConfig.configuration.ScannerExclusion -ne $null)
        {
        #find previous exclusion names
        $PreviousExlusions = $MWExpertConfig.configuration.ScannerExclusion.add.name

        Write-Output "Recreating previous exclusions"

        foreach ($name in $PreviousExlusions)
            {
             if ($name -ne "ZeroConfSweep")
                {
                # Set the attribute name and value for the exclusion
                $ExclusionAddAttributeName = $MWExpertConfig.CreateAttribute("name")
                $ExclusionAddAttributeName.Value = $name

                # Append the exclusion attribute to the exclusion element
                $ExclusionAdd.Attributes.Append($ExclusionAddAttributeName)

                $ScannerExclusion.AppendChild($ExclusionAdd)
                }
            }
        }

    # Set the attribute name and value for the exclusion
    Write-Output "Creating exclusion for ZeroConf"
    $ExclusionAddAttributeName = $MWExpertConfig.CreateAttribute("name")
    $ExclusionAddAttributeName.Value = "ZeroConfSweep"

    # Append the exclusion attribute to the exclusion element
    $ExclusionAdd.Attributes.Append($ExclusionAddAttributeName)

    # Append the exclusion element to the Scanner Exclusions element
    $ScannerExclusion.AppendChild($ExclusionAdd)

    # define our insert point
    $InsertPoint = $MWExpertConfig.configuration.appSettings

    # Check if scannerexlusions exist so we can determine whether to append or replace with the new element
    if ($MWExpertConfig.configuration.ScannerExclusion -eq $null)
        {
        # Now append the scanner exclusions element to the configuration tree
        $MWExpertConfig.configuration.InsertAfter($ScannerExclusion,$InsertPoint)
        }
    else
        {
        $ScannerExclusionRefNode = $MWExpertConfig.configuration.ScannerExclusion
        $MWExpertConfig.configuration.ReplaceChild($ScannerExclusion,$ScannerExclusionRefNode)
        }

    # Finally lets save our new config file

    Write-Output "Saving configuration"
    $MWExpertConfig.Save($MWExpertConfigPath)

可以用手

0 个答案:

没有答案