如何基于 XML & 更改 PowerShell 结果行为CSV? [英] How to change PowerShell result behavior based on XML & CSV?

查看:17
本文介绍了如何基于 XML & 更改 PowerShell 结果行为CSV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 脚本为 -

$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

 }

我确信下面的部分正在处理这个逗号分隔,请帮助如何更新它,这样它就不会破坏或影响脚本中的代码 1.

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

推荐答案

如果我理解正确,您想使用模板为CH21000546421_ProductID.csv"中找到的每个服务器输出一个 xml 文件,但在编写这些时遇到问题删除重复的服务器名称.

If I understand correctly, you want to output an xml file for every server found in the 'CH21000546421_ProductID.csv' using a template, but having trouble with writing these out for the duplicate servernames.

如果您的模板如下所示:

If your template looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<Servers>
  <Server title="#Title#">#computername#</Server>
</Servers>

并且您的输入 csv 看起来像这样:

and your input csv looks anything like this:

"IGPID","ServerName"
123,"Server1"
43,"Server2"
8,"Server3"
11,"Server1"
1,"Server1"
17,"Server3"

然后下面的代码应该做你想做的:

Then the below code should do what you want:

$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
}

现在您有多个 xml 文件,每个服务器一个.发现重复的服务器都有名为 _.xml 的 xml 文件.
唯一的服务器具有名为

Now you have several xml files, one for each server. The servers that were found as duplicate all have xml files named <servername>_<id>.xml.
The unique servers have files called <servername.xml>

'Server1_123.xml'的演示内容(重复之一)

Demo content of 'Server1_123.xml' (one of the duplicates)

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

输入文件'Server2.xml'中唯一唯一服务器的Demo内容:

Demo content of the only unique server in the input file 'Server2.xml':

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

这篇关于如何基于 XML &amp; 更改 PowerShell 结果行为CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆