我的网络中有一个hostname,ip
个路由器的CSV,我想创建一个XML模板,用于将这些路由器全部导入putty。
我需要xml架构
我现有的计划是导入CSV并将其命名为$router
,然后将$router.hostname
和$router.ip
放入脚本中,但我无法弄清楚如何填充文本使用csv值,以及如何将其导出为XML。
理想情况下,它会将每个xml作为每个路由器$hostname.xml
的文件输出。
有没有人对如何使这项工作有任何指示?我对PS和CSV有相当多的经验,但从来没有XML,我知道这需要一个foreach
,但我对这一切都很新。
由于 山姆
$router=import-csv C:\Users\Admin\Desktop\RouterIPHost.csv
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<root expanded="True" name="Test MSP" type="database"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<connection name=($router.hostname) type="PuTTY">
<name>$Putty.name </name>
<protocol>Telnet</protocol>
<host>($router.ip)</host>
<port>23</port>
<session>Default Settings</session>
<connectiontimeout>1500</connectiontimeout>
<logintimeout>1000</logintimeout>
<passwordtimeout>1000</passwordtimeout>
<commandtimeout>1000</commandtimeout>
<postcommands>False</postcommands>
</connection>
</root>
Export-Clixml -path "C:\Users\PRTG_Admin\Desktop\xmlrouter.xml"
答案 0 :(得分:0)
假设csv格式为:
hostname,ip router_site_london,192.168.0.1 router_site_paris,192.168.1.1 router_site_newyork,192.168.2.1
您可以将文件视为纯文本,只需使用变量从多行字符串循环创建它们:
$routers = Import-Csv C:\Users\Admin\Desktop\RouterIPHost.csv
foreach ($router in $routers) {
$router_hostname = $router.hostname
$router_ip = $router.ip
$xml = @"
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<root expanded="True" name="Test MSP" type="database"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<connection name=$router_hostname type="PuTTY">
<name>$router_hostname</name>
<protocol>Telnet</protocol>
<host>$router_ip</host>
<port>23</port>
<session>Default Settings</session>
<connectiontimeout>1500</connectiontimeout>
<logintimeout>1000</logintimeout>
<passwordtimeout>1000</passwordtimeout>
<commandtimeout>1000</commandtimeout>
<postcommands>False</postcommands>
</connection>
</root>
"@
$xml | Out-File -FilePath C:\folder\putty_routers\$router_hostname.xml
}