xml - Error formatting a string: Input string was not in a correct format -
i'm trying import excel file powershell , save xml file. got excel file , xml template. have done @ below:
# define xml configuration template $config = "c:\users\mustafaal\desktop\stajyermustafaal\gorev\xmltemplate_v2.ps1" import-csv c:\users\mustafaal\desktop\stajyermustafaal\gorev\variablesdeneme.csv | foreach-object { $xml = "c:\users\mustafaal\desktop\stajyermustafaal\gorev\mysampleconfig" + $_.username + ".xml" write-host $_.username write-host $xml [xml]$x = (&$config) -f $_.username, $_.switchip, $_.switchport, $_.macaddress $x.save($xml) } so basicly i'm trying save xml files every username in excel file.
i'm getting error:
error formatting string: input string not in correct format. @ c:\users\mustafaal\desktop\stajyermustafaal\gorev\deneme1.ps1:14 char:1 + [xml]$x = (&$config) -f $_.username, $_.switchip, $_.switchport, $_.macaddress
xml template sample:
<attributelist> <ruleattribute displayvalue="ethernet (15)" value="15" operator="equals" name="nas-port-type" type="radius:ietf"/> <ruleattribute displayvalue="login-user (1), framed-user (2), authenticate-only (8)" value="1,2,8" operator="belongs_to" name="service-type" type="radius:ietf"/> <ruleattribute displayvalue={1} value={1} operator="equals" name="nas-ip-address" type="radius:ietf"/> <ruleattribute displayvalue={2} value={2} operator="equals" name="nas-port-id" type="radius:ietf"/> <ruleattribute displayvalue={3} value={3} operator="equals_ignore_case" name="calling-station-id" type="radius:ietf"/> <ruleattribute displayvalue="teidom\"{0} value="teidom\"{0} operator="equals_ignore_case" name="user-name" type="radius:ietf"/> </attributelist> csv sample:
username,switchip,switchport,macaddress ahmeto,10.101.254.104,7,288023041d83 ahmett,10.101.254.136,4,480fcf4a6719
basically need replace {} in xml file variables in csv file, i'm getting string format error.
edit: okay have found problem, there other codes {} , made them double, got new error:
to type "system.xml.xmldocument". error: "'ahmett' unexpected token. expecting white space. line 5, position 84.
the attribute values in template must in quotes. change this:
<attributelist> <ruleattribute displayvalue="ethernet (15)" value="15" operator="equals" name="nas-port-type" type="radius:ietf"/> <ruleattribute displayvalue="login-user (1), framed-user (2), authenticate-only (8)" value="1,2,8" operator="belongs_to" name="service-type" type="radius:ietf"/> <ruleattribute displayvalue={1} value={1} operator="equals" name="nas-ip-address" type="radius:ietf"/> <ruleattribute displayvalue={2} value={2} operator="equals" name="nas-port-id" type="radius:ietf"/> <ruleattribute displayvalue={3} value={3} operator="equals_ignore_case" name="calling-station-id" type="radius:ietf"/> <ruleattribute displayvalue="teidom\"{0} value="teidom\"{0} operator="equals_ignore_case" name="user-name" type="radius:ietf"/> </attributelist> into this:
<attributelist> <ruleattribute displayvalue="ethernet (15)" value="15" operator="equals" name="nas-port-type" type="radius:ietf"/> <ruleattribute displayvalue="login-user (1), framed-user (2), authenticate-only (8)" value="1,2,8" operator="belongs_to" name="service-type" type="radius:ietf"/> <ruleattribute displayvalue="{1}" value="{1}" operator="equals" name="nas-ip-address" type="radius:ietf"/> <ruleattribute displayvalue="{2}" value="{2}" operator="equals" name="nas-port-id" type="radius:ietf"/> <ruleattribute displayvalue="{3}" value="{3}" operator="equals_ignore_case" name="calling-station-id" type="radius:ietf"/> <ruleattribute displayvalue="teidom\{0}" value="teidom\{0}" operator="equals_ignore_case" name="user-name" type="radius:ietf"/> </attributelist> also, need load template regular text, can fill in values using format operator (-f). once done can either save data text:
$template = get-content 'c:\path\to\template.xml' | out-string import-csv 'c:\path\to\input.csv' | foreach-object { $filename = 'c:\path\to\{0}.xml' -f $_.username $template -f $_.username, $_.switchip, $_.switchport, $_.macaddress | set-content $filename } or parse filled-in template xml object , save that:
$template = get-content 'c:\path\to\template.xml' | out-string import-csv 'c:\path\to\input.csv' | foreach-object { $filename = 'c:\path\to\{0}.xml' -f $_.username [xml]$xml = $template -f $_.username, $_.switchip, $_.switchport, $_.macaddress $xml.save($filename) } the latter has advantage parser validate xml data , throw error in case invalid.
Comments
Post a Comment