0

I have a CSV of hostname,ip of routers in our network and I want to create an XML template for importing these all into putty.

I have the xml schema required

My existing plan was to import the CSV and call it $router then place $router.hostname and $router.ip into the script, but I can't work out how to make this populate the text with the csv values nor how to then export it as XML.

Ideally it would then output each xml as a file per router $hostname.xml.

Does anyone have any pointers on how to make this work? I have a fair amount of experience with PS and CSV but never XML, I'm aware this is gonna need a foreach but I am fairly new at this all.

Thanks Sam

$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"
3
  • Be aware that Export-CLIXML exports data to a format that is specific to powershell that is for storing objects. It includes information about the objects themselves in addition to the data. It is not what you are looking for. Basically you will need to create the root node then append child nodes to build the XML. Commented Jun 5, 2018 at 13:25
  • can you add a couple of lines of your csv Commented Jun 5, 2018 at 14:27
  • also what have you tried so far with foreach and writetohost Commented Jun 5, 2018 at 14:41

1 Answer 1

0

Assuming a csv format of:

hostname,ip
router_site_london,192.168.0.1
router_site_paris,192.168.1.1
router_site_newyork,192.168.2.1

You can treat the files as plain text and just create them in a loop from a multi-line string using variables:

$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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi! This is exactly what i was hoping for, as much as i'd like to understand XML more fully, handling this as text was perfect for my needs and urgency. Worked a treat with a bit of fiddling to make columns match. Thanks so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.