Xml 用CSV中的数据替换多个字符串
我已经从我们所有的访问点获得了一个CSV(带有标题),并希望通过XML上传将它们添加到Zabbix 因为我不想手动为每个AP创建XML文件,所以我将尝试在PowerShell中这样做。但是——怎么做 我用Xml 用CSV中的数据替换多个字符串,xml,powershell,csv,Xml,Powershell,Csv,我已经从我们所有的访问点获得了一个CSV(带有标题),并希望通过XML上传将它们添加到Zabbix 因为我不想手动为每个AP创建XML文件,所以我将尝试在PowerShell中这样做。但是——怎么做 我用(Get Content.\Template.xml).Replace()和ForEach Object尝试了一些方法,但还没有成功 文件名为: AP.csv(AP列表,标题为主机名和IP): Template.xml有两个名为“Hostname”的字符串和两个名为“PIIP”的字符串-两个主
(Get Content.\Template.xml).Replace()
和ForEach Object
尝试了一些方法,但还没有成功
文件名为:
(AP列表,标题为主机名和IP):AP.csv
有两个名为“Hostname”的字符串和两个名为“PIIP”的字符串-两个主机名字符串都应替换为Template.xml
中的一个主机名,并且两个PIIP都应替换为相关的IPAP.csv
$a=导入Csv。\Ap.Csv
$a主机名=($a).Hostname
$APIP=($a).IP
$xml=获取内容。\Template.xml
foreach($a中的行){
$xml.Replace('Hostname',$APHost.).Replace('PIIP',$APIP)|
输出文件“\Aps\$row.xml”
}
但这在所有主机名和所有IP中结束,其中包含一个XML文件
我希望每个主机的末尾都有一个XML文件,其中包含主机名和相关IP。示例:
$CSVData = Import-Csv "$env:userprofile\desktop\Ap.csv" -Delimiter ";"
$template = Get-Content "$env:userprofile\desktop\template.xml" -Raw
$count = 1
foreach ($row in $CSVData) {
$template.Replace("Hostname", $row.Hostname).Replace("PIIP", $row.IP) |
Out-File "$env:userprofile\desktop\$count.xml"
$count++
}
你的主要问题是:
- 您将AP名称和IP存储在循环外部的数组变量中(
和$APHost
)$APIP
- 然后在循环中将这些数组作为一个整体使用,而不是逐个引用它们的元素
$row.Hostname
和$row.IP
来获得迭代适当的值:
$a = Import-Csv -Delimiter ';' .\Ap.csv
$xml = Get-Content -Raw .\Template.xml
foreach ($row in $a) {
$xml.Replace('Hostname', $row.Hostname).Replace('PIIP', $row.IP) |
Out-File ".\Aps\$row.xml"
}
但是,您应该考虑使用真正的XML解析使替换更加健壮:
$a = Import-Csv -Delimiter ';' .\Ap.csv
$xmlTmpl = [xml] (Get-Content -Raw .\Template.xml)
foreach ($row in $a) {
# Find the parent Xml element of the elements to update.
$parentEl = $xmlTmpl.hosts.host
# Set the hostnames, by element *name*.
$parentEl.host = $parentEl.name = $row.Hostname
# Set the IP addresses, by element *name*
foreach ($el in $parentEl.interfaces.interface.ChildNodes) {
if ($el.Name -eq 'ip') { $el.InnerText = $row.IP }
}
# XPath alternative (doesn't work in PowerShell *Core*):
# foreach ($el in $parentEl.SelectNodes('interfaces/interface/ip'))
# {
# $el.InnerText = $row.IP
# }
# Save to file
# Note: This doesn't include an XML header.
$xmlTmpl.OuterXml | Out-File ".\Aps\$row.xml"
}
请注意如何针对文档层次结构中特定位置的特定元素,这是实例化模板的一种更可靠的方法(没有误报的可能性)。请添加您引用的xml/csv文件的示例,以及您期望的替换,以及您到目前为止导入csv的尝试?你试过了吗?更新了Mainpost,是的,已经试过了,但我不知道如何只选择1个主机名和相关的ip。
$a = Import-Csv -Delimiter ';' .\Ap.csv
$xmlTmpl = [xml] (Get-Content -Raw .\Template.xml)
foreach ($row in $a) {
# Find the parent Xml element of the elements to update.
$parentEl = $xmlTmpl.hosts.host
# Set the hostnames, by element *name*.
$parentEl.host = $parentEl.name = $row.Hostname
# Set the IP addresses, by element *name*
foreach ($el in $parentEl.interfaces.interface.ChildNodes) {
if ($el.Name -eq 'ip') { $el.InnerText = $row.IP }
}
# XPath alternative (doesn't work in PowerShell *Core*):
# foreach ($el in $parentEl.SelectNodes('interfaces/interface/ip'))
# {
# $el.InnerText = $row.IP
# }
# Save to file
# Note: This doesn't include an XML header.
$xmlTmpl.OuterXml | Out-File ".\Aps\$row.xml"
}