Zhaobab
3/5/2015 - 10:00 AM

Convert CSV file to XML

Convert CSV file to XML

# ----------------------------------------------
# Author: Romain Blanchard
# Date: 05.03.2015
# Description: Convert CSV file to XML
# ----------------------------------------------

# Strings
$csv = Import-Csv -Path "C:\Users\Romain\Desktop\DassaultAviation_UserMapping.csv" -Delimiter ';'
$xmlfile = "C:\Users\Romain\Desktop\UserMapping_NPExt.xml"
$count = 0
$numbermax = @($csv).count

# Script
$xml = "<?xml version=`"1.0`" encoding=`"utf-8`"?>`n"
$xml += "<UserMappingXml>`n"
$xml += "  <UserMappings>`n"

$csv | foreach {
    $xml += "    <UserMapping sourceUser=`"" + $_.Source + "`" destinationUser=`"i:0e.t|idp.saml.dsa.npext|" + $_.Target + "`" />`n"
    $count ++

    # Display progress bar
    $c = ($count / $numbermax)
    $d = "{0:P2}" -f $c
    Write-Progress -activity "Converting to XML..." -status "Progress: $d"
}

$xml += "   </UserMappings>`n"
$xml += "</UserMappingXml>"

$xml | Out-file $xmlfile

Write-Host "done!" -ForegroundColor Green