簡體   English   中英

如何根據 XML 和 CSV 更改 PowerShell 結果行為?

[英]How to change PowerShell result behavior based on XML & CSV?

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>

但顯示如下不正確的格式-

<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。

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

如果我理解正確,您希望使用模板為在“CH21000546421_ProductID.csv”中找到的每個服務器的 output 和 xml 文件,但在為重復的服務器名寫出這些文件時遇到問題。

如果您的模板看起來像這樣:

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

你的輸入 csv 看起來像這樣:

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

現在您有幾個 xml 文件,每個服務器一個。 被發現為重復的服務器都具有名為<servername>_<id>.xml的 xml 文件。
唯一服務器具有名為<servername.xml>的文件

'Server1_123.xml' 的演示內容(其中一個副本)

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

輸入文件“Server2.xml”中唯一唯一服務器的演示內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM