简体   繁体   中英

Replacing XML attribute in Powershell instead of adding

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Config>
    <Variable>
        <Name>SVAR_Action</Name>
        <Value>Archive</Value>
    </Variable>
    <Variable>
        <Name>SVAR_ActionByFile</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_ArchiveDir</Name>
        <Value>\\s1000\PROD\sFTP\abc\cde\From\efg</Value>
    </Variable>
    <Variable>
        <Name>SVAR_BatchFTPOK</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_BatchFTPOKFilename</Name>
        <Value>triggerfile_(\d{8})\.ftpok</Value>
    </Variable>
    <Variable>
        <Name>SVAR_CleanWorkDir</Name>
        <Value>1</Value>
    </Variable>
    <Variable>
        <Name>SVAR_CompletedEmail</Name>
        <Value>1</Value>
    </Variable>
    <Variable>
        <Name>SVAR_CompletedEmailNoFiles</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_CompletedEmailMessage</Name>
        <Value>The event rule has completed sucessfully</Value>
    </Variable>
    <Variable>
        <Name>SVAR_CompletedEmailSubject</Name>
        <Value/>
    </Variable>
    <Variable>
        <Name>SVAR_ConnectionType</Name>
        <Value>SMB</Value>
    </Variable>
    <Variable>
        <Name>SVAR_DestinationDir</Name>
        <Value>\\domain.local\data\Applic\SVSSL\from\AAA</Value>
    </Variable>
    <Variable>
        <Name>SVAR_ErrorEmail</Name>
        <Value>1</Value>
    </Variable>
    <Variable>
        <Name>SVAR_ErrorEmailAddress</Name>
        <Value/>
    </Variable>
    <Variable>
        <Name>SVAR_ErrorEmailMessage</Name>
        <Value>An error occurred in the event rule</Value>
    </Variable>
    <Variable>
        <Name>SVAR_ErrorEmailSubject</Name>
        <Value/>
    </Variable>
    <Variable>
        <Name>SVAR_FileFTPOK</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_FileFTPOKExisting</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_FileFTPOKSuffix</Name>
        <Value>.ftpok</Value>
    </Variable>
    <Variable>
        <Name>SVAR_FileFTPOKType</Name>
        <Value>Append</Value>
    </Variable>
    <Variable>
        <Name>SVAR_FilesBlacklist</Name>
        <Value/>
    </Variable>
    <Variable>
        <Name>SVAR_FilesWhitelist</Name>
        <Value/>
    </Variable>
    <Variable>
        <Name>SVAR_FTPOKDelete</Name>
        <Value>1</Value>
    </Variable>
    <Variable>
        <Name>SVAR_FTPOptions</Name>
        <Value>None</Value>
    </Variable>
    <Variable>
        <Name>SVAR_Host</Name>
        <Value/>
    </Variable>
    <Variable>
        <Name>SVAR_IgnoreInvalidCertificate</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_KeyFile</Name>
        <Value/>
    </Variable>
    <Variable>
        <Name>SVAR_KeyPassphrase</Name>
        <Value/>
    </Variable>
    <Variable>
        <Name>SVAR_MaxBatchSize</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_MaxFilesPerRun</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_MaximumFileSize</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_MinimumFileSize</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_Password</Name>
        <Value/>
    </Variable>
    <Variable>
        <Name>SVAR_Port</Name>
        <Value/>
    </Variable>
    <Variable>
        <Name>SVAR_Proxy</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_SkipValidation</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_SourceDir</Name>
        <Value>\\dataserver\hiddenshare$\WorkDir\FROM</Value>
    </Variable>
    <Variable>
        <Name>SVAR_SourceFilter</Name>
        <Value>*</Value>
    </Variable>
    <Variable>
        <Name>SVAR_StrictBlacklist</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_StrictWhitelist</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_TestMode</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_Username</Name>
        <Value/>
    </Variable>
    <Variable>
        <Name>SVAR_ValidationContinue</Name>
        <Value>1</Value>
    </Variable>
    <Variable>
        <Name>SVAR_ValidationEmail</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_ValidationEmailAddress</Name>
        <Value/>
    </Variable>
    <Variable>
        <Name>SVAR_ValidationEmailMessage</Name>
        <Value>The event rule was unable to verify the integrity of the batch.</Value>
    </Variable>
    <Variable>
        <Name>SVAR_ValidationEmailNonFatal</Name>
        <Value>0</Value>
    </Variable>
    <Variable>
        <Name>SVAR_ValidationEmailSubject</Name>
        <Value/>
    </Variable>
    <Variable>
        <Name>SVAR_VerifyTransfer</Name>
        <Value>Nothing</Value>
    </Variable>
</Config>

Im trying to go through this xml and update SVAR_ArchiveDir or the SVAR_DestinationDir.

$csvSource = "C:\xml.csv"
$xmlFilesPath = "C:\Temp\Config"

# load files that need to be changed
$filesToChange = Import-Csv -path $csvSource -delimiter ';'

# search for file
foreach($filechange in $filesToChange[0]){
    $targetFile = Get-ChildItem -Path $xmlFilesPath -Include $filechange.File -Recurse
    [xml]$targetFileXML = Get-Content -Path $targetFile.FullName
}

# search and find the corresponding node and update value
$x = [xml] (Get-Content C:\Temp\EFTConfig\ADP\ADP_FROM_Config_Push.xml)
Select-Xml -xml $x  -XPath / |
    ForEach-Object { $_.Node.'#text' = 'SVAR_ArchiveDir'
        $_.Node.Value = '\\fancyArchive\share'
      }


      $nodes = $x.SelectNodes("/Config/Variable")

      foreach($node in $nodes[0]){
        if($node.Name -eq 'SVAR_ArchiveDir'){
            Write-Host "Change node value from: $($node.Value)"
            Write-Host 'Change node value to: Nothing'
            $node.SetAttribute("\\slowstorage\share");
            $node
        }
      }

But when I try to adjust the value I get either multiple values. But I wan't the value to be updated and not added. The result is now: $node.value gives me 2 values, the original value and the new value.

I tried to reset it and then add a new value. Or use the appendChild but I get errors that I can't overload or I get Cannot set "Value" because only strings can be used as values to set XmlNode properties.

I'm almost there but can't find the right solution for this. Could someone push me into the right direction?

Your XML contains no attributes (only element nodes and text nodes), so it's unclear why you're trying to call .SetAttribute() in order to update existing nodes.

From what I can tell, you're looking for something like this:

# Parse the file.
($x = [xml]::new()).Load('C:\Temp\EFTConfig\ADP\ADP_FROM_Config_Push.xml')

# Select the node of interest and update the child node of interest.
$x.SelectSingleNode(
  '/Config/Variable[Name="SVAR_ArchiveDir"]'
).Value = '\\fancyArchive\share'

# Use $x.Save(...) to save back to a file.

Note: The reason that an [xml] instance is explicitly constructed with ::new() , followed by a call to its .Load() method (always be sure to pass a full path) is that only this approach robustly reads XML files - [xml] (Get-Content ...) is not robust; see this answer for an explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM