如何更改基于XML的PowerShell结果行为&;CSV?

如何更改基于XML的PowerShell结果行为&;CSV?,xml,powershell,Xml,Powershell,嗨,我有下面的脚本&当我运行这个脚本时,它会基于一个主XML生成多个XML,现在这些XML有计算机名信息,应该用XML更新,如下所示- <ComputerName>Server1</ComputerName> Server1 但显示如下,这是不正确的格式- <ComputerName>Server1, server2, server3</ComputerName> Server1、server2、server3 PS脚本作为- $templ

嗨,我有下面的脚本&当我运行这个脚本时,它会基于一个主XML生成多个XML,现在这些XML有计算机名信息,应该用XML更新,如下所示-

<ComputerName>Server1</ComputerName>
Server1
但显示如下,这是不正确的格式-

<ComputerName>Server1, server2, server3</ComputerName>
Server1、server2、server3
PS脚本作为-

$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

 }
$template=Get Content-Path'C:\pscript\vj\MasterXML.xml'-Raw
$csv=导入csv'C:\pscript\vj\CH21000546421_ProductID.csv'
删除项目“C:\pscript\vj\dup.csv”
删除项目“C:\pscript\vj\uniq.csv”
$csv|
组对象-属性ServerName|
Where对象-FilterScript{
$\计数-gt 1
} |
选择对象-ExpandProperty组|导出Csv-路径“C:\pscript\vj\dup.Csv”-NoTypeInformation
导入Csv-路径“C:\pscript\vj\dup.Csv”|组对象服务器名| ForEach对象{
#替换模板中的占位符。对数值进行排序以保持整洁。
$template-replace'#Title#',([int[]$\uu.Group.IGPID | Sort Object)-join',')-replace
“#计算机名称”,“$($.Name)”|
设置内容-路径“C:\pscript\vj\$($\名称).xml”-编码UTF8
}
$csv|
组对象-属性ServerName|
Where对象-FilterScript{
$\计数-lt 2
} |
选择对象-ExpandProperty组|导出Csv-路径“C:\pscript\vj\uniq.Csv”-NoTypeInformation
导入Csv-路径“C:\pscript\vj\uniq.Csv”|组对象IGPID | ForEach对象{
#替换模板中的占位符。对数值进行排序以保持整洁。
$template-replace'#computername#',“$($.Group.ServerName)-join',')”-replace
“#Title#”,($#.Name)|
设置内容-路径“C:\pscript\vj\$($\名称).xml”-编码UTF8
}
我确信下面的部分正在处理这个逗号分隔,请帮助更新它,这样它就不会破坏或影响脚本中的代码1

$template -replace '#computername#',"<ComputerName>$(($_.Group.ServerName) -join ',')</ComputerName>" -replace
$template-replace'#computername#',“$($.Group.ServerName)-join',”-replace

如果我理解正确,您希望使用模板为“CH210005461_ProductID.csv”中找到的每台服务器输出一个xml文件,但在为重复的服务器名写出这些文件时遇到困难

如果您的模板看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<Servers>
  <Server title="#Title#">#computername#</Server>
</Servers>
"IGPID","ServerName"
123,"Server1"
43,"Server2"
8,"Server3"
11,"Server1"
1,"Server1"
17,"Server3"
然后,下面的代码应该执行您想要的操作:

$template = Get-Content -Path 'C:\pscript\vj\MasterXML.xml' -Raw
$csv      = Import-Csv -Path 'C:\pscript\vj\CH21000546421_ProductID.csv' | Group-Object -Property ServerName

# get the duplicate servernames
$dupes = $csv | Where-Object { $_.Count -gt 1 }
# get the unique servernames
$uniq = $csv | Where-Object { $_.Count -lt 2 }
            
# save this as new csv files
$dupes | Select-Object -ExpandProperty Group | Export-Csv -Path 'C:\pscript\vj\dup.csv' -NoTypeInformation
$uniq  | Select-Object -ExpandProperty Group | Export-Csv -Path 'C:\pscript\vj\uniq.csv' -NoTypeInformation

# $dupes is already grouped by servername
$dupes | ForEach-Object {
    # remember you're iterating Groups with multiple items here!
    foreach ($item in $_.Group) {
        $template -replace '#Title#', $item.IGPID -replace '#computername#', "<ComputerName>$($item.ServerName)</ComputerName>" |
        # write an xml for each of the duplicates. By appending the IGPID to the name of the file
        Set-Content -Path "C:\pscript\vj\$($item.ServerName)_$($item.IGPID).xml" -Encoding UTF8
    }
}

# $uniq is already grouped by servername
$uniq | ForEach-Object {
    # or use -replace '#computername#', "<ComputerName>$($_.Group[0].ServerName)</ComputerName>"
    $template -replace '#Title#', $_.Group[0].IGPID -replace '#computername#', "<ComputerName>$($_.Name)</ComputerName>" |
    # write an xml for each of the unique servers.
    Set-Content -Path "C:\pscript\vj\$($_.Name).xml" -Encoding UTF8
}
$template=Get Content-Path'C:\pscript\vj\MasterXML.xml'-Raw
$csv=导入csv-路径“C:\pscript\vj\CH21000546421_ProductID.csv”|组对象-属性服务器名
#获取重复的服务器名
$dupes=$csv |其中对象{$\计数-gt 1}
#获取唯一的服务器名
$uniq=$csv |其中对象{$\.Count-lt 2}
#将此保存为新的csv文件
$dupes |选择对象-ExpandProperty组|导出Csv-路径'C:\pscript\vj\dup.Csv'-NoTypeInformation
$uniq |选择对象-ExpandProperty组|导出Csv-路径'C:\pscript\vj\uniq.Csv'-NoTypeInformation
#$dupes已按服务器名分组
$dups | ForEach对象{
#请记住,您在这里迭代包含多个项目的组!
foreach($组中的项目){
$template-替换“#Title”、$item.IGPID-替换“#computername”、“$($item.ServerName)”|
#为每个副本编写一个xml
设置内容-路径“C:\pscript\vj\$($item.ServerName)\$($item.IGPID.xml”-编码UTF8
}
}
#$uniq已按服务器名分组
$uniq | ForEach对象{
#或使用-replace“#computername#”,“$($.Group[0].ServerName)”
$template-replace'#Title#',$#.Group[0]。IGPID-replace'#computername#',“$($#.Name)”|
#为每个唯一的服务器编写xml。
设置内容-路径“C:\pscript\vj\$($\名称).xml”-编码UTF8
}
现在您有了几个xml文件,每个服务器一个。发现为重复的服务器都有名为
\uu.xml

的xml文件 唯一的服务器具有名为

“Server1_123.xml”的演示内容(其中一个副本)


服务器1
输入文件“Server2.xml”中唯一唯一服务器的演示内容:

<?xml version="1.0" encoding="utf-8"?>
<Servers>
  <Server title="43"><ComputerName>Server2</ComputerName></Server>
</Servers>

服务器2

Cleary编写代码不是为了这样做。问题是,
ForEach对象
as按原样为每个条目输出一个新文件,而不是写入单个XML文件的附加字符串。另外,您能否提供使脚本按需工作的尝试?否则这是一个功能齐全的脚本。感谢Alex的回复!是的,脚本工作正常,但输出不正确,或者我会说不是我所期望的。我对脚本编写一无所知,它已经在系统中了,但并没有按照预期的方式工作。我正在使用下面的位-“#computername”、“$($)($.Group.ServerName)-join”、”),这是我希望生成输出的区域,作为Server1而不是Server1、server2、server3,我尝试将join by更改为我想要的,但它不起作用。
<?xml version="1.0" encoding="utf-8"?>
<Servers>
  <Server title="43"><ComputerName>Server2</ComputerName></Server>
</Servers>