I am not a scripter, please if anyone can help me with modify below script by removing UCPID value but keep only servername
Currently below script looking two columns from csv file, now I want to change the behavior to only look for ServerName because now CSV file have only one column which containing server only in each row and update related XML.
$data = Import-Csv .\MyFile.csv
$luTable = @{}
# Create Keys in Lookup Table
$data | % {
if (!$luTable.ContainsKey("$($_.ServerName)")) { $luTable["$($_.UCPID)"] = New-Object System.Collections.ArrayList }
}
$luTable.Keys | % {
$key = $_ # Store Key
$data | where UCPID -Match $_ | select ServerName | % {
$luTable[$key].Add($_.ServerName)
}
}
# Build XML Files
$luTable.Keys | % {
$key = $_
$filetext = gc ".\MyXML.xml"
$filetext = $filetext.Replace("#Title#", $key)
$targets = ""
$luTable[$key] | % {
$targets += "<ComputerName>$($_)</ComputerName>"
}
$filetext = $filetext.Replace("#computername#", $targets)
sc -Path ".\$($key).xml" -Value $filetext
}
I tried deleting below code but its not helping.
# Create Keys in Lookup Table
$data | % {
if (!$luTable.ContainsKey("$($_.ServerName)")) { $luTable["$($_.UCPID)"] = New-Object System.Collections.ArrayList }
}
//CSV file content
ServerName
Server1
Server2
Server3
Server4
Server5
//XML - location where I want server to be copied
<AnnounceOffer>false</AnnounceOffer>
<OfferCategory>false</OfferCategory>
<OfferDescriptionHTML>false</OfferDescriptionHTML>
</SettingsLocks>
<IsUrgent>false</IsUrgent>
<Target>
#computername#
</Target>
</SingleAction>
</BES>
#computername# must be replaced with below-
<ComputerName>Server1</ComputerName>
<ComputerName>Server2</ComputerName>
<ComputerName>Server3</ComputerName>
<ComputerName>Server4</ComputerName>
If your XML looks like this:
<BES>
<SingleAction>
<SettingsLocks>
<AnnounceOffer>false</AnnounceOffer>
<OfferCategory>false</OfferCategory>
<OfferDescriptionHTML>false</OfferDescriptionHTML>
</SettingsLocks>
<IsUrgent>false</IsUrgent>
<Target>
#computername#
</Target>
</SingleAction>
</BES>
Then here's two alternatives for you:
Method 1: use XML functionality of PowerShell
# load the xml from file
$xml= New-Object System.XML.XMLDocument
$xml.Load("D:\Test\MyXML.xml")
# select the node with the #computername# placeholder inside
$targetNode = $xml.SelectSingleNode('//BES/SingleAction/Target')
$targetNode.'#text' = '' # remove the placeholder text
# read the servernames from file and create and insert new nodes for each of them
(Import-Csv -Path 'D:\Test\AllServers.csv').ServerName |
ForEach-Object {
$newNode = $xml.CreateElement('ComputerName')
$newNode.InnerText = $_
$targetNode.AppendChild($newNode)
}
# save the updated XML
$xml.Save('D:\Test\NewXml.xml')
Method 2: treat the xml as plain text and do a simple textual -replace on it
# read the XML as multiline text
$xml = Get-Content -Path 'D:\Test\MyXML.xml' -Raw
# find the line where the #computername# placeholder is and get the indentation value
$indent = ' ' * ($xml | Select-String -Pattern '(?m)^(\s+)#computername#').Matches[0].Groups[1].Length
# read the servernames from file and construct a multiline string
$servers = ((Import-Csv -Path 'D:\Test\AllServers.csv').ServerName |
ForEach-Object { "$indent<ComputerName>$_</ComputerName>" }) -join [environment]::NewLine
# now replace in the xml and write to (new) file
$xml -replace "(?m)^$indent#computername#", $servers | Set-Content -Path 'D:\Test\NewXml.xml' -Encoding UTF8
Result in both cases:
<BES>
<SingleAction>
<SettingsLocks>
<AnnounceOffer>false</AnnounceOffer>
<OfferCategory>false</OfferCategory>
<OfferDescriptionHTML>false</OfferDescriptionHTML>
</SettingsLocks>
<IsUrgent>false</IsUrgent>
<Target>
<ComputerName>Server1</ComputerName>
<ComputerName>Server2</ComputerName>
<ComputerName>Server3</ComputerName>
<ComputerName>Server4</ComputerName>
<ComputerName>Server5</ComputerName>
</Target>
</SingleAction>
</BES>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments