Hi I have below script & when I am running this script its generating multiple XML based on one master XML now these xml have computer name info which should be updated in xml as below-
<ComputerName>Server1</ComputerName>
but showing as below which is not right format-
<ComputerName>Server1, server2, server3</ComputerName>
PS script as -
$template = Get-Content -Path 'C:\pscript\vj\MasterXML.xml' -Raw
$csv= Import-Csv 'C:\pscript\vj\CH21000546421_ProductID.csv'
Remove-Item 'C:\pscript\vj\dup.csv'
Remove-Item 'C:\pscript\vj\uniq.csv'
$csv |
Group-Object -Property ServerName |
Where-Object -FilterScript {
$_.Count -gt 1
} |
Select-Object -ExpandProperty Group| Export-Csv -Path 'C:\pscript\vj\dup.csv' -NoTypeInformation
Import-Csv -Path 'C:\pscript\vj\dup.csv' | Group-Object ServerName | ForEach-Object {
# replace the placeholders in the template. Sort the numeric values for neatness as we go.
$template -replace '#Title#', (([int[]]$_.Group.IGPID | Sort-Object) -join ', ') -replace
'#computername#', "<ComputerName>$($_.Name)</ComputerName>" |
Set-Content -Path "C:\pscript\vj\$($_.Name).xml" -Encoding UTF8
}
$csv |
Group-Object -Property ServerName |
Where-Object -FilterScript {
$_.Count -lt 2
} |
Select-Object -ExpandProperty Group| Export-Csv -Path 'C:\pscript\vj\uniq.csv' -NoTypeInformation
Import-Csv -Path 'C:\pscript\vj\uniq.csv' | Group-Object IGPID | ForEach-Object {
# replace the placeholders in the template. Sort the numeric values for neatness as we go.
$template -replace '#computername#',"<ComputerName>$(($_.Group.ServerName) -join ',')</ComputerName>" -replace
'#Title#', ($_.Name) |
Set-Content -Path "C:\pscript\vj\$($_.Name).xml" -Encoding UTF8
}
I am sure below part is handling this comma separation, please help in how to update it so it will not break or effect code 1 in the script.
$template -replace '#computername#',"<ComputerName>$(($_.Group.ServerName) -join ',')</ComputerName>" -replace
ForEach-Objectas is outputs a new file per every entry and is not written to appended strings to a single XML file.